UNPKG

@pradyumn-el/pollycli

Version:

pollycli lets users access the functionalities of Polly over a command line interface

1,343 lines (1,298 loc) 40 kB
import arg from 'arg'; import inquirer from 'inquirer'; import chalk from 'chalk'; import { readFileSync, existsSync } from 'fs'; import { isAlreadyLoggedIn } from './auth'; const pollyEnv = require('./env.json'); import { organizationDetails } from './organization'; const axios = require('axios'); import { getAllDockerRepos } from './docker'; import { getAppInfo, getAppsList } from './apps'; const pollymsg = require("./message") let isNotWindows = true; if (process.platform === "win32" ) { isNotWindows = false; } /** * This function is used to parse the argument that ths user is inputting * @param {*} rawArgs */ export async function parseArgumentsIntoOptions(rawArgs) { let mainArgs = null; try { mainArgs = arg( { '--job-file': String, '--workspace-id': String, '--workspace-name': String, '--workspace-description': String, '--run-id': String, '--job-id': String, '--log-id': String, '--help': Boolean, '-h': '--help', '--all': Boolean, '--latest': Number, '--oldest': Number, '--version': Boolean, '-v': '--version', '--yes': Boolean, '-y': '--yes', '--source': String, '-s': '--source', '--destination': String, '--name': String, '--description': String, '-d': '--destination', '--id': String, '--workspace-path': String, '--auto': Boolean, '--public': Boolean, '--own': Boolean, '--component-path': String, '--docker-image': String, '--type': String, '--path': String, '--check': Boolean, '--tail': Boolean, '--json': String, '--display-name': String, '--cpu': String, '--memory': String, '--port': String, '--group': String, '--access': String, '--app-file': String, '--file': String, '-f': '--file', '--env-name': String, '--docker-image': String, '--kernel': String, '--base-env': String, '--access': String, '--tag': String, '--pipeline': String, '--main-script': String, '--container': String, '--verbose': Boolean, '--is-report': String, '--attempt-no': Number }, { argv: rawArgs.slice(2), } ); } catch (error) { if (error.code === 'ARG_UNKNOWN_OPTION') { pollymsg.pollyError(error.message); } else { pollymsg.pollyError(`Get to know the options on Polly with: ${chalk.green.italic('polly --help')}`); } } let latestCliVersion = null; try { let pollyCliData = await axios.get(`https://registry.npmjs.org/@elucidatainc/pollycli`); pollyCliData = pollyCliData.data; latestCliVersion = pollyCliData['dist-tags']['latest']; const pjson = require('./../package.json'); if (pjson.version != latestCliVersion) { pollymsg.pollyMessage(`A new version of Polly CLI is available. To update, execute the command npm update -g @elucidatainc/pollycli`); }; } catch (error) { } if (mainArgs['--version']) { const pjson = require('./../package.json'); pollymsg.pollyMessage(pjson.version); process.exit(0); } global.pollyUserConfirmation = false; if (mainArgs['--yes']) { global.pollyUserConfirmation = true; } global.pollyAuto = false; if (mainArgs['--auto']) { global.pollyAuto = true; } global.checkData = false; if (mainArgs['--check']) { global.checkData = true; } global.getTailData = false; if (mainArgs['--tail']) { global.getTailData = true; } if (mainArgs['--json']) { global.outputFormat = { 'format': 'json', 'path': mainArgs['--json'] } } global.verbose = false; if (mainArgs['--verbose']) { global.verbose = true } let returnObject = {}; // keywords like login compute are templates // which can be accessed like mainArgs._[0] // Other arguments are parsed as above // arguments to be defined here // With the below if statement for a given keyword we can chose // options to be required seperately if (mainArgs._[0] == 'login') { returnObject = { type: 'user-management', action: 'login' } } else if (mainArgs._[0] == 'logout') { returnObject = { type: 'user-management', action: 'logout' } } else if (mainArgs._[0] == 'whoami') { returnObject = { type: 'user-management', action: 'whoami' } } else if (mainArgs._[0] == 'jobs' && isNotWindows) { if (mainArgs._[1] == 'submit') { returnObject = { type: 'jobs', action: 'submit', workspace_id: mainArgs['--workspace-id'], job_file: mainArgs['--job-file'] } } else if (mainArgs._[1] == 'status') { returnObject = { type: 'jobs', action: 'status', workspace_id: mainArgs['--workspace-id'], job_id: mainArgs['--job-id'], } } else if (mainArgs._[1] == 'logs') { returnObject = { type: 'jobs', action: 'logs', workspace_id: mainArgs['--workspace-id'], job_id: mainArgs['--job-id'], attempt_num: mainArgs['--attempt-no'], } } else if (mainArgs._[1] == 'cancel') { returnObject = { type: 'jobs', action: 'cancel', workspace_id: mainArgs['--workspace-id'], job_id: mainArgs['--job-id'], } } else { returnObject = { type: 'jobs' } } } else if (mainArgs._[0] == 'workspaces') { if (mainArgs._[1] == 'list') { returnObject = { type: 'workspaces', action: 'list', all: mainArgs['--all'], latest: mainArgs['--latest'], oldest: mainArgs['--oldest'] } } else if (mainArgs._[1] == 'create') { returnObject = { type: 'workspaces', action: 'create', workspace_name: mainArgs['--workspace-name'], workspace_description: mainArgs['--workspace-description'] } } else { returnObject = { type: 'workspaces' } } } else if (mainArgs._[0] == 'files') { if (mainArgs._[1] == 'sync') { returnObject = { type: 'files', action: 'sync', source_path: mainArgs['--source'], destination_path: mainArgs['--destination'], workspace_id: mainArgs['--workspace-id'], is_report: mainArgs['--is-report'] == "true" } } else if (mainArgs._[1] == 'list') { returnObject = { type: 'files', action: 'list', workspace_id: mainArgs['--workspace-id'], workspace_path: mainArgs['--workspace-path'] } } else if (mainArgs._[1] == 'copy') { returnObject = { type: 'files', action: 'copy', source_path: mainArgs['--source'], destination_path: mainArgs['--destination'], workspace_id: mainArgs['--workspace-id'], is_report: mainArgs['--is-report'] == "true" } } else { returnObject = { type: 'files' } } } else if (mainArgs._[0] == 'dockers' && isNotWindows) { if (mainArgs._[1] == 'login') { returnObject = { type: 'dockers', action: 'login' } } else if (mainArgs._[1] == 'logout') { returnObject = { type: 'dockers', action: 'logout' } } else if (mainArgs._[1] == 'list') { returnObject = { type: 'dockers', action: 'list', own: mainArgs['--own'], public: mainArgs['--public'], all: mainArgs['--all'] } } else if (mainArgs._[1] == 'create') { returnObject = { type: 'dockers', action: 'create', name: mainArgs['--name'], description: mainArgs['--description'], public: mainArgs['--public'] } } else if (mainArgs._[1] == 'commit-list') { returnObject = { type: 'dockers', action: 'commit-list', name: mainArgs['--name'], all: mainArgs['--all'] } } else if (mainArgs._[1] == 'update') { returnObject = { type: 'dockers', action: 'update', name: mainArgs['--name'], public: mainArgs['--public'] } } else if (mainArgs._[1] == 'build') { returnObject = { type: 'dockers', action: 'build', name: mainArgs['--name'], path: mainArgs['--path'] } } else if (mainArgs._[1] == 'build-status') { returnObject = { type: 'dockers', action: 'build-status', id: mainArgs['--id'] } } else { returnObject = { type: 'dockers' } } } else if (mainArgs._[0] == 'apps' && isNotWindows) { if (mainArgs._[1] == 'add') { returnObject = { type: 'apps', action: 'add' } } else if (mainArgs._[1] == 'list') { returnObject = { type: 'apps', action: 'list' } } else if (mainArgs._[1] == 'create') { returnObject = { type: 'apps', action: 'create', app_file: mainArgs['--app-file'] || mainArgs['-f'], app_info: { name: mainArgs['--name'], display_name: mainArgs['--display-name'], description: mainArgs['--description'], docker_image: mainArgs['--docker-image'], cpu: mainArgs['--cpu'], memory: mainArgs['--memory'], port: mainArgs['--port'], group: mainArgs['--group'], resource_access: mainArgs['--access'] } } } else if (mainArgs._[1] == 'update') { returnObject = { type: 'apps', action: 'update', name: mainArgs['--name'], app_file: mainArgs['--app-file'] || mainArgs['-f'], app_info: { docker_image: mainArgs['--docker-image'], cpu: mainArgs['--cpu'], memory: mainArgs['--memory'], port: mainArgs['--port'] } } } else if (mainArgs._[1] == 'logs') { returnObject = { type: 'apps', action: 'logs', run_id: mainArgs['--run-id'], log_id: mainArgs['--log-id'] } } else if (mainArgs._[1] == 'sample') { returnObject = { type: 'apps', action: 'sample', } } else { returnObject = { type: 'apps' } } } else if (mainArgs._[0] == 'notebooks' && isNotWindows) { if (mainArgs._[1] == 'new-environment') { returnObject = { type: 'notebooks', action: 'new-environment', env_type: mainArgs['--type'], name: mainArgs['--name'] } } else if (mainArgs._[1] == 'build') { returnObject = { type: 'notebooks', action: 'build', } } else if (mainArgs._[1] == 'build-status') { returnObject = { type: 'notebooks', action: 'build-status', id: mainArgs['--id'] } } else if (mainArgs._[1] == 'test') { returnObject = { type: 'notebooks', action: 'test', } } else { returnObject = { type: 'notebooks' } } } else if (mainArgs._[0] == 'environment' && isNotWindows) { if (mainArgs._[1] == 'create') { returnObject = { type: 'environment', action: 'create', env_file: mainArgs['--file'] || mainArgs['-f'], env_info: { name: mainArgs["--name"], docker_image: mainArgs["--docker-image"], description: mainArgs["--description"], kernel: mainArgs["--kernel"], resource_access: mainArgs["--public"] || mainArgs["--access"] || "private", base_env: mainArgs["--base-env"] } } } else if (mainArgs._[1] == 'update') { returnObject = { type: 'environment', action: 'update', env_name: mainArgs['--env-name'], env_info: { description: mainArgs['--description'], resource_access: mainArgs['--access'], tag: mainArgs['--tag'] } } } else if (mainArgs._[1] == 'remove') { returnObject = { type: 'environment', action: 'remove', env_name: mainArgs['--env-name'] } } else if (mainArgs._[1] == 'sample') { returnObject = { type: 'environment', action: 'sample', } } } else if (mainArgs._[0] == 'workflows' || mainArgs._[0] == 'workflow') { if (mainArgs._[1] == 'submit') { returnObject = { type: 'workflows', action: 'submit', workspace_id: mainArgs['--workspace-id'], config: { pipeline: mainArgs['--pipeline'], main_script: mainArgs['--main-script'], container: mainArgs['--container'] } } } else if (mainArgs._[1] == 'cancel') { returnObject = { type: 'workflows', action: 'cancel', run_id: mainArgs['--run-id'] } } else if (mainArgs._[1] == 'status') { returnObject = { type: 'workflows', action: 'status', run_id: mainArgs['--run-id'] } } else if (mainArgs._[1] == 'logs') { returnObject = { type: 'workflows', action: 'logs', run_id: mainArgs['--run-id'] } } } // else if (mainArgs._[0] == 'studio') { // if (mainArgs._[1] == 'new-component') { // returnObject = { // type: 'studio', // action: 'new-component', // item_type: mainArgs['--type'], // name: mainArgs['--name'], // } // } else if (mainArgs._[1] == 'build-component') { // returnObject = { // type: 'studio', // action: 'build-component', // } // } else if (mainArgs._[1] == 'component-build-status') { // returnObject = { // type: 'studio', // action: 'component-build-status', // id: mainArgs['--id'] // } // } else { // returnObject = { // type: 'studio' // } // } // } else if (mainArgs._.length == 0) { returnObject = {} } else { returnObject = { unknown: mainArgs._[0] } } // This is used to show help for users at any point in user asks if (mainArgs['--help']) { returnObject['help'] = true; } return returnObject; } async function initalQuestion() { const returnObject = {}; let pollyOptions = ['login', 'logout', 'workspaces', 'files', 'jobs', 'dockers', 'miscellaneous']; if (!isNotWindows) { pollyOptions = ['login', 'logout', 'workspaces', 'files']; } const initAnswers = await inquirer.prompt({ type: 'list', name: 'type', message: 'What would you like to do?', choices: pollyOptions, }); if (initAnswers.type == 'login') { returnObject['type'] = 'user-management'; returnObject['action'] = 'login'; } else if (initAnswers.type == 'logout') { returnObject['type'] = 'user-management'; returnObject['action'] = 'logout'; } else if (initAnswers.type == 'whoami') { returnObject['type'] = 'user-management'; returnObject['action'] = 'whoami'; } else if (initAnswers.type == 'jobs' && isNotWindows) { returnObject['type'] = 'jobs'; } else if (initAnswers.type == 'workflows' && isNotWindows) { returnObject['type'] = 'workflows'; } else if (initAnswers.type == 'workspaces') { returnObject['type'] = 'workspaces'; } else if (initAnswers.type == 'files') { returnObject['type'] = 'files'; } else if (initAnswers.type == 'dockers' && isNotWindows) { returnObject['type'] = 'dockers'; } else if (initAnswers.type == 'notebooks' && isNotWindows) { returnObject['type'] = 'notebooks'; } else if (initAnswers.type == 'apps' && isNotWindows) { returnObject['type'] = 'apps'; } else if (initAnswers.type == 'studio' && isNotWindows) { returnObject['type'] = 'studio'; } else if (initAnswers.type == 'miscellaneous' && isNotWindows) { returnObject['type'] = 'miscellaneous'; } else { pollymsg.pollyError(`Option ${initAnswers.init} is not valid`); } return returnObject } /** * This function is used to ask for missing values that user didnt * enter * @param {*} options */ export async function promptForMissingValues(options) { let questions = []; if (Object.keys(options).indexOf('unknown') > -1) { pollymsg.pollyErrorNoExit(`The following ${options.unknown} option(s) are not valid`) pollymsg.pollyMessage(`Please try the following options!\n`) options = {} } // This is the case use done know what to do and just writes polly in the terminal if (Object.keys(options).length < 1) { options = await initalQuestion(); } const projectId = process.env.POLLY_WORKSPACE_ID ? process.env.POLLY_WORKSPACE_ID : process.env.POLLY_PROJECT_ID let workspaceSkip = true; if (projectId && pollyUserConfirmation) { workspaceSkip = false; if (!options.workspace_id) { options['workspace_id'] = projectId; } } // If the user wants to login asking for the user name and password if (options.type == 'user-management' && options.action == 'login') { if (pollyAuto) { return { ...options, creds: { username: 'not_required', password: 'not_required' } }; } else if (!await isAlreadyLoggedIn(true)) { questions = [{ type: 'input', name: 'pollyusername', message: 'Enter user name:' }, { type: 'password', name: 'pollypassword', message: 'Enter password:', }] const answers = await inquirer.prompt(questions); return { ...options, creds: { username: answers.pollyusername, password: answers.pollypassword } }; } else { pollymsg.pollyMessage('Already logged in'); process.exit(0); } } else if (options.type == 'jobs' && isNotWindows) { if (!options.action) { questions = [{ type: 'list', name: 'action', message: 'What would you like to access?', choices: ['submit', 'status', 'logs', 'cancel'], }]; const answers = await inquirer.prompt(questions); options.action = answers.action; } questions = []; // If the user wats to run a job then asking for the json file with the job discription if (options.action == 'submit') { if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID:', default: projectId }); } if (!options.job_file) { questions.push({ type: 'input', name: 'job_file', message: 'Enter the path to the job description file (.json):' }); } } else if (options.action == 'status') { if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID:', default: projectId }); } if (!options.job_id && !pollyUserConfirmation) { questions.push({ type: 'input', name: 'job_id', message: 'Enter the job ID:' }); } } else if (options.action == 'logs') { if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID:', default: projectId }); } if (!options.job_id) { questions.push({ type: 'input', name: 'job_id', message: 'Enter the job ID:' }); } } else if (options.action == 'cancel') { if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID:', default: projectId }); } if (!options.job_id) { questions.push({ type: 'input', name: 'job_id', message: 'Enter the job ID:' }); } } const answers = await inquirer.prompt(questions); if (Object.keys(answers).length > 0) { return { ...options, ...answers }; } } else if (options.type == 'workspaces') { questions = []; if (!options.action) { questions.push({ type: 'list', name: 'action', message: 'What would you like to do with Polly workspaces?', choices: ['list', 'create'], }); const answers = await inquirer.prompt(questions); options.action = answers.action; } if (options.action == 'list') { if (!options.all && !options.latest && !options.oldest) { questions = [{ type: 'list', name: 'intermediate', message: 'Which workspaces do you want to list?', choices: ['all', 'latest', 'oldest'], }]; const answers = await inquirer.prompt(questions); if (answers.intermediate == 'latest') { questions = [{ type: 'input', name: 'intermediate', message: 'How many latest workspaces you would like to list?' }]; const answers = await inquirer.prompt(questions); options['latest'] = parseInt(answers.intermediate) } else if (answers.intermediate == 'oldest') { questions = [{ type: 'input', name: 'intermediate', message: 'How many oldest workspaces you would like to list?' }]; const answers = await inquirer.prompt(questions); options['oldest'] = parseInt(answers.intermediate) } else { options['all'] = true; } } return { ...options }; } else if (options.action == 'create') { questions = []; if (!options.workspace_name) { questions.push({ type: 'input', name: 'workspace_name', message: 'Enter the name of the workspace you would like to create:' }); } if (!options.workspace_description) { questions.push({ type: 'input', name: 'workspace_description', message: 'Enter the description of the workspace you would like to create:' }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } } else if (options.type == 'files') { questions = []; if (!options.action) { questions.push({ type: 'list', name: 'action', message: 'What would you like to do with Polly files?', choices: ['sync', 'list', 'copy'], }); const answers = await inquirer.prompt(questions); options.action = answers.action; } if (options.action == 'sync') { questions = [] if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID?', default: projectId }); } if (!options.source_path) { questions.push({ type: 'input', name: 'source_path', message: 'Enter the source path?' }); } if (!options.destination_path) { questions.push({ type: 'input', name: 'destination_path', message: 'Enter the destination path?' }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'list') { questions = [] if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID?', default: projectId }); } if (!options.workspace_path) { questions.push({ type: 'input', name: 'workspace_path', message: 'Enter the workspace Path?' }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'copy') { questions = [] if (!options.workspace_id && workspaceSkip) { questions.push({ type: 'input', name: 'workspace_id', message: 'Enter the workspace ID?', default: projectId }); } if (!options.source_path) { questions.push({ type: 'input', name: 'source_path', message: 'Enter the source path?' }); } if (!options.destination_path) { questions.push({ type: 'input', name: 'destination_path', message: 'Enter the destination path?' }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } } else if (options.type == 'apps' && isNotWindows) { questions = []; if (options.action == 'help') { questions = [] console.log('polly apps create --app-file="path/to/app.json"\n' + 'polly apps sample (for app.json format)\n' + 'or polly apps update --name="" --docker-image="" --cpu="" --memory="" ...'); process.exit(0); } else if (options.action == 'list') { questions = [] const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'logs') { questions = [] if (!options.run_id) { questions = [{ type: 'input', name: 'run_id', message: 'Enter the run ID for the application?', }]; } if (!options.log_id) { questions.push({ type: 'input', name: 'log_id', message: 'Enter the log ID for the application?', }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'commit-list') { questions = [] if (!options.name) { if (!await isAlreadyLoggedIn()) { process.exit(1); } const organizationInfo = await organizationDetails(); const orgSlug = organizationInfo.slug; questions = [{ type: 'input', name: 'name', message: `Enter the name of the docker repository? ${pollyEnv.dockerDomain}/${orgSlug}/` }]; } if (!options.all) { questions.push({ type: 'list', name: 'all', message: 'How many docker repository you want to list?', choices: ['all'], }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } } else if (options.type == 'dockers' && isNotWindows) { questions = []; if (!options.action) { questions.push({ type: 'list', name: 'action', message: 'What would you like to do with Polly dockers?', choices: ['login', 'logout', 'create', 'list', 'commit-list', 'update', 'build', 'build-status'], }); const answers = await inquirer.prompt(questions); options.action = answers.action; } if (options.action == 'login') { questions = [] const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'logout') { questions = [] const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'create') { questions = [] if (!options.name) { if (!await isAlreadyLoggedIn()) { process.exit(1); } const organizationInfo = await organizationDetails(); const orgSlug = organizationInfo.slug; if (!orgSlug) { pollymsg.pollyError("Please contact customer success team to get docker repository feature"); } questions.push({ type: 'input', name: 'name', message: `Enter the name of the docker repository? ${pollyEnv.dockerDomain}/${orgSlug}/` }); } if (!options.description) { questions.push({ type: 'input', name: 'description', message: 'Enter the description of the docker repository?' }); } if (!options.public) { if (global.pollyUserConfirmation) { options.public = false; } else { questions.push({ type: 'confirm', name: 'public', message: 'Should the docker repository be public?' }); } } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'list') { questions = [] let answers = {}; if (!options.own && !options.public && !options.all) { questions = [{ type: 'list', name: 'interData', message: 'How many docker repository you want to list?', choices: ['own', 'public', 'all'], }]; answers = await inquirer.prompt(questions); if (answers.interData == 'own') { answers = { 'own': true } } else if (answers.interData == 'public') { answers = { 'public': true } } else { answers = { 'all': true } } } if (options.own && options.public && options.all) { pollymsg.pollyError('Only one option is possible either --own or --public, --all'); } return { ...options, ...answers }; } else if (options.action == 'build-status') { questions = [] let answers = {}; if (!options.id && !pollyUserConfirmation) { questions = [{ type: 'input', name: 'id', message: 'Enter the id for the docker build', }]; answers = await inquirer.prompt(questions); } return { ...options, ...answers }; } else if (options.action == 'build') { questions = [] let answers = {}; if (!options.name) { questions.push({ type: 'input', name: 'name', message: 'Enter the docker name with tag', }); } if (!options.path) { questions.push({ type: 'input', name: 'path', message: 'Enter the Dockerfile path or directory', }); } answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'commit-list') { questions = [] if (!options.name) { if (!await isAlreadyLoggedIn()) { process.exit(1); } questions = [{ type: 'input', name: 'name', message: `Enter the name of the docker repository? ${pollyEnv.dockerDomain}/` }]; } if (!options.all) { questions.push({ type: 'list', name: 'all', message: 'How many docker repository you want to list?', choices: ['all'], }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'update') { let organizationInfo = null; if (!options.name) { if (!await isAlreadyLoggedIn()) { process.exit(1); } organizationInfo = await organizationDetails(); const orgSlug = organizationInfo.slug; questions = [{ type: 'input', name: 'name', message: `Enter the name of the docker repository? ${pollyEnv.dockerDomain}/${orgSlug}/` }]; let answers = await inquirer.prompt(questions); options.name = answers.name; } questions = [] let accessStatus = 'private'; if (!options.public) { if (!organizationInfo) { if (!await isAlreadyLoggedIn()) { process.exit(1); } organizationInfo = await organizationDetails(); } const dockerRepoList = await getAllDockerRepos(organizationInfo.org_id, 'own'); let modifiedDocker = null; for (const eachDocker of dockerRepoList) { if (eachDocker.attributes.resource_name == `${organizationInfo.slug}/${options.name}`) { modifiedDocker = eachDocker; break; } } if (!modifiedDocker) { pollymsg.pollyError('Given repository does not exist'); } accessStatus = Object.keys(modifiedDocker.attributes).indexOf('resource_access') > -1 && modifiedDocker.attributes.resource_access == 'public' ? 'public' : 'private' let askedQuestion = ''; if (accessStatus == 'public') { askedQuestion = 'Currently the repository is public would you like to make it private?' } else { askedQuestion = 'Currently the repository is private would you like to make it public?' } questions.push({ type: 'confirm', name: 'public', message: askedQuestion }); } let answers = await inquirer.prompt(questions); if (accessStatus == 'public') { answers.public = !answers.public } return { ...options, ...answers }; } } else if (options.type == 'studio' && isNotWindows) { questions = []; if (!options.action) { questions.push({ type: 'list', name: 'action', message: 'What would you like to do with studio?', choices: ['new-component', 'build-component', 'component-build-status'], }); const answers = await inquirer.prompt(questions); options.action = answers.action; } if (options.action == 'new-component') { questions = [] if (!options.item_type) { let gitBranches = await axios.get('https://api.github.com/repos/ElucidataInc/studio-templates/branches'); const allowedTemplate = [] for (const eachBranches of gitBranches.data) { if (eachBranches.name != 'master') { allowedTemplate.push(eachBranches.name) } } questions.push({ type: 'list', name: 'item_type', message: 'What type of template would you like to generate?', choices: allowedTemplate, }); } if (!options.name) { questions.push({ type: 'input', name: 'name', message: 'Enter the name of the studio component?', }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'build-component') { questions = [] const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'component-build-status') { questions = [] if (!options.id) { questions.push({ type: 'input', name: 'docker_name', message: `Enter the build ID` }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } } else if (options.type == 'notebooks' && isNotWindows) { questions = []; if (!options.action) { questions.push({ type: 'list', name: 'action', message: 'What would you like to do with Polly notebooks?', choices: ['new-environment','build', 'build-status'], }); const answers = await inquirer.prompt(questions); options.action = answers.action; } if (options.action == 'build') { questions = [] // if (!options.docker_name) { // if (!await isAlreadyLoggedIn()) { // process.exit(1); // } // // const organizationInfo = await organizationDetails(); // // const orgSlug = organizationInfo.slug; // // questions.push({ // // type: 'input', // // name: 'docker_name', // // message: `Enter the name of the docker repository with tag? ${pollyEnv.dockerDomain}/${orgSlug}/` // // }); // } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'build-status') { questions = [] if (!options.id) { questions.push({ type: 'input', name: 'id', message: `Enter the ID to get the build status ?` }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'new-environment') { questions = [] if (!options.env_type) { let gitBranches = await axios.get('https://api.github.com/repos/ElucidataInc/polly-notebook-templates/branches'); const allowedTemplate = [] for (const eachBranches of gitBranches.data) { if(eachBranches.name != 'master'){ allowedTemplate.push(eachBranches.name) } } questions.push({ type: 'list', name: 'env_type', message: `Enter the type of notebook environment ?`, choices: allowedTemplate }); } if (!options.name) { questions.push({ type: 'input', name: 'name', message: `Enter the name of the new notebook environment ?`, }); } const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } else if (options.action == 'test') { questions = [] const answers = await inquirer.prompt(questions); return { ...options, ...answers }; } } else if (options.type == 'miscellaneous' && isNotWindows) { let answers = await inquirer.prompt({ type: 'list', name: 'action', message: 'Which option would you like to use?', choices: ['create secret for docker'], }); if (answers.action == 'create secret for docker') { answers = await inquirer.prompt({ type: 'input', name: 'docker_config', message: 'Path to the docker config file:' }); if (existsSync(answers.docker_config)) { // extracting file let dockerConfig = readFileSync(answers.docker_config) dockerConfig = JSON.parse(dockerConfig); if ('auths' in dockerConfig) { let dockerAccounts = Object.keys(dockerConfig.auths); answers = await inquirer.prompt({ type: 'list', name: 'docker_account', message: 'Account for which you would like to make a docker secret:', choices: dockerAccounts }); let copyDockerConfig = { ...dockerConfig } copyDockerConfig['auths'] = { [answers.docker_account]: { auth: dockerConfig['auths'][answers.docker_account]['auth'] } } copyDockerConfig = JSON.stringify(copyDockerConfig, null, '\t'); console.log("\n\n") console.log(Buffer.from(copyDockerConfig).toString('base64')) pollymsg.pollyMessage('\n\ncopy the above secret and paste it in the jobs/workflow description file under the secret key.\nWe do not store the secret in any database. It is just used to pull the docker image.') process.exit(0); } else { pollymsg.pollyError("Not a valid docker config file"); } } else { pollymsg.pollyError("Give a valid docker config path"); } } } return { ...options } }