/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-explicit-any */ import yargs from 'yargs'; import sdk from '@nsoft/seven-sdk'; import { runServer } from '@nsoft/seven-cli-ui'; import auth from '../auth'; import devices from '../devices'; import tenants from '../tenants'; import { getTenantListForCompanyAdmin } from '../tenants/tenantsHandler'; import { showNotification } from '../helpers/notificationHandler'; import SevenNotification from '../enums/SevenNotification'; import { SevenIamAuthorizationResponse } from '@nsoft/seven-sdk/dist/Common/Interfaces'; import { ResponseErrorMessage } from '@nsoft/seven-sdk/dist/Common'; const sevenEnvironment = sdk.Common.Environment; const loginHandler = (options: any) => { auth.login(options.keyId, options.keySecret, options.env, null).then((response: SevenIamAuthorizationResponse | ResponseErrorMessage | null) => { const errResponse = (response as ResponseErrorMessage); if (!errResponse || errResponse?.httpCode && errResponse?.code) { getTenantListForCompanyAdmin(options.keyId, options.keySecret, options.env) .then((data) => { // Login is per user and not per company // Check against first available tenant if (data && data[0]?.uuid) { auth.login(options.keyId, options.keySecret, options.env, data[0].uuid).catch((err) => { showNotification(SevenNotification.ERROR, err?.message || 'Token not valid'); }); return; } showNotification(SevenNotification.ERROR, 'Token not valid'); }).catch(() => { showNotification(SevenNotification.ERROR, 'Token not valid'); }); } }).catch((err) => { showNotification(SevenNotification.ERROR, err || 'Token not valid'); }); }; const parseArgs = (): any => { return yargs.command({ command: 'devices:emulate', describe: 'Emulate devices', builder: (yargs) => { return yargs .option('device-id', { describe: 'Device ID', type: 'number', demandOption: false }) .option('app-name', { describe: 'App name', type: 'string', demandOption: false }) .option('include', { type: 'array', desc: 'Include services you want to start', demandOption: false }) .option('exclude', { type: 'array', desc: 'Exclude services you don\'t want to start', demandOption: false, }) .choices( 'include', devices.emulatorChoices ) .choices( 'exclude', devices.emulatorChoices ) .alias('deviceId', 'device-id') .alias('appName', 'app-name'); }, handler: ({ include, exclude, deviceId, appName }) => { devices.emulate({ include, exclude, deviceId, appName }); } }) .demand(1, 'must provide a valid command') .command({ command: 'ui', describe: 'Start UI', handler: () => { runServer(); } }) .command({ command: 'auth:login', describe: 'Authorize on Seven Platform', builder: (yargs) => { return yargs.option('key-id', { describe: 'Key Id', type: 'string', demandOption: true }) .option('key-secret', { describe: 'Key Secret', type: 'string', demandOption: true }) .option('env', { describe: 'Environment to be used', type: 'string', demandOption: true }) .choices( 'env', Object.values(sevenEnvironment), ) .alias('keyId', 'key-id') .alias('keySecret', 'key-secret') .alias('environment', 'env'); }, handler: loginHandler }) .command({ command: 'auth:list', describe: 'Gets list of authorized users/accounts', handler: () => { auth.loadAuthorizedUsers(); } }) .command({ command: 'auth:use', describe: 'Switches to user/account from the list of authorized users', builder: (yargs) => { return yargs.option('index', { describe: 'Ordinal number of desired user from table of authorized users', type: 'number', demandOption: false }); }, handler: ({ index }) => { auth.setCurrentUser(index); } }) .command({ command: 'auth:logout', describe: 'Removes user/account from the list of authorizes users', builder: (yargs) => { return yargs.option('index', { describe: 'Ordinal number of desired user from table of authorized users', type: 'number', demandOption: false }).option('keyId', { describe: 'KeyId of user whose credentials needs to be removed', type: 'string', demandOption: false }) .alias('keyId', 'key-id'); }, handler: ({ index, keyId }) => { auth.logoutUser(index, keyId); } }) .command({ command: 'tenants:list', describe: 'Gets list of tenants for current user', handler: () => { tenants.loadTenants(); } }) .command({ command: 'tenants:use', describe: 'Selects tenant from the list of tenants', builder: (yargs) => { return yargs.option('index', { describe: 'Ordinal number of desired tenant from the list of tenants', type: 'number', demandOption: true }); }, handler: ({ index }) => { tenants.selectTenant(index); } }).argv; }; export { parseArgs, };