|
| 1 | +import yargs from 'yargs'; |
| 2 | + |
| 3 | +import {deploy, migrateDb, downloadDb, dumpDb, restoreDb, release, runShell, shutdown} from './commands'; |
| 4 | +import {configuration} from './configuration'; |
| 5 | + |
| 6 | + |
| 7 | +const |
| 8 | + COMMAND_INSTANCE = (config) => { |
| 9 | + return { |
| 10 | + describe: 'the instance for deployment', |
| 11 | + type: 'string', |
| 12 | + choices: Object.keys(config.deployment.instances) |
| 13 | + }; |
| 14 | + } |
| 15 | +; |
| 16 | + |
| 17 | +export function cli() { |
| 18 | + const |
| 19 | + config = configuration(), |
| 20 | + args = yargs |
| 21 | + .usage('Usage: $0 <command> [options]') |
| 22 | + .command('deploy <instance>', 'Deploy tag to instance', (yargs) => { |
| 23 | + yargs.option('migrate', {alias: 'm', description: 'Run migration after deployment', type: 'boolean'}); |
| 24 | + yargs.option('tag', {alias: 't', description: 'Deploy specific tag', type: 'string'}); |
| 25 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 26 | + }) |
| 27 | + .command('downloaddb <instance>', 'Download Database from given instance', (yargs) => { |
| 28 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 29 | + }) |
| 30 | + .command('dumpdb <instance>', 'Dump Database for given instance', (yargs) => { |
| 31 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 32 | + }) |
| 33 | + .command('migrate <instance>', 'Migrate Database for given instance', (yargs) => { |
| 34 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 35 | + }) |
| 36 | + .command('release', 'Add and push a new tag to remote branch') |
| 37 | + .command('restoredb <instance>', 'Restore Database for given instance', (yargs) => { |
| 38 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 39 | + }) |
| 40 | + .command('shell <instance>', 'Start shell on given instance', (yargs) => { |
| 41 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 42 | + }) |
| 43 | + .command('shutdown <instance>', 'Shutdown container on given instance', (yargs) => { |
| 44 | + yargs.option('remove', {alias: 'rm', description: 'Remove container after shutdown', type: 'boolean'}); |
| 45 | + yargs.positional('instance', COMMAND_INSTANCE(config)); |
| 46 | + }) |
| 47 | + .example([ |
| 48 | + ['$0 release', 'Add and push a new tag to remote branch'] |
| 49 | + ]) |
| 50 | + .recommendCommands() |
| 51 | + .scriptName('moccu-cli') |
| 52 | + .showHelpOnFail(true) |
| 53 | + .demandCommand(1, '') |
| 54 | + .help() |
| 55 | + .alias('help', 'h') |
| 56 | + .strict() |
| 57 | + .argv, |
| 58 | + cmd = args._[0] || 'help' |
| 59 | + ; |
| 60 | + |
| 61 | + if (args.instance) { |
| 62 | + global.instance = args.instance; |
| 63 | + global.config = {...config.deployment.common, ...config.deployment.instances[args.instance]}; |
| 64 | + } |
| 65 | + |
| 66 | + switch (cmd) { |
| 67 | + case 'deploy': |
| 68 | + deploy(args); |
| 69 | + break; |
| 70 | + case 'migrate': |
| 71 | + migrateDb(); |
| 72 | + break; |
| 73 | + case 'release': |
| 74 | + release(); |
| 75 | + break; |
| 76 | + case 'dumpdb': |
| 77 | + dumpDb(); |
| 78 | + break; |
| 79 | + case 'restoredb': |
| 80 | + restoreDb(); |
| 81 | + break; |
| 82 | + case 'downloaddb': |
| 83 | + downloadDb(); |
| 84 | + break; |
| 85 | + case 'shell': |
| 86 | + runShell(); |
| 87 | + break; |
| 88 | + case 'shutdown': |
| 89 | + shutdown(); |
| 90 | + break; |
| 91 | + } |
| 92 | +} |
0 commit comments