import * as yargs from 'yargs'; import * as chalk from 'chalk'; import * as merge from 'lodash/merge'; import { Cli } from '../app/cli'; import { Auth, AuthMethod, Remote } from '../app/auth'; import { log } from '../app/log'; import { Questions, out } from '../app/cli_utils'; var cmd = 'set'; var cmdArgs = ' [property] [value]'; export var command: string = cmd + ' ' + cmdArgs; export var desc: string = 'Modify a registerd authorisation'; var properties = ['remote', 'method', 'key', 'secret', 'extra', 'default']; export function builder( args: yargs.Argv ) { return args .usage(` ${desc} ${chalk.bold('Usage:')}: ${cmd} ${cmdArgs}`) } var auth; export function handler( argv ) { if ( Cli.handleGlobals(argv) ) { return; } if ( !Auth.has(argv.name) ) { log.fatal(`Could not find auth [${argv.name}]`); } auth = Auth.get(argv.name) if ( !argv.property ) Questions.askChoice('Property', properties).then(( property ) => { askValue(property) }); else if ( !argv.value ) askValue(argv.property) else modifyProperty(argv.property, argv.value); } function askValue( property ) { var type = property === 'secret' ? 'password' : 'input'; var o = { type : type, message : 'value' }; if ( property === 'remote' ) merge(o, { type : 'list', choices : Remote.all() }) if ( property === 'method' ) merge(o, { type : 'list', choices : AuthMethod.all() }) return Questions.ask(o, ( value ) => modifyProperty(property, value)) } function modifyProperty( property, value ) { if ( property === 'default' ) { return auth.setDefault(auth.default === false); } out(`modifyProperty ${property} to ${value} for ${auth.name}`) auth[property] = value; auth.save(); out(chalk.green('done')); }