From 0587c6aec539cd6c7adb9fab4b3edddadf56c870 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Sun, 10 May 2020 18:27:05 -0600 Subject: [PATCH 1/2] feat: implement `agoric cosmos ...` --- packages/agoric-cli/lib/cosmos.js | 65 +++++++++++++++++++++++++++++++ packages/agoric-cli/lib/main.js | 15 +++++++ 2 files changed, 80 insertions(+) create mode 100644 packages/agoric-cli/lib/cosmos.js diff --git a/packages/agoric-cli/lib/cosmos.js b/packages/agoric-cli/lib/cosmos.js new file mode 100644 index 00000000000..af02f4ae38b --- /dev/null +++ b/packages/agoric-cli/lib/cosmos.js @@ -0,0 +1,65 @@ +import chalk from 'chalk'; + +export default async function cosmosMain(progname, rawArgs, powers, opts) { + const IMAGE = `agoric/agoric-sdk`; + const { anylogger, spawn, process } = powers; + const log = anylogger('agoric:cosmos'); + + const popts = opts; + + const pspawnEnv = { ...process.env }; + if (popts.verbose > 1) { + // Enable verbose logs. + pspawnEnv.DEBUG = 'agoric'; + } else if (!popts.verbose) { + // Disable more logs. + pspawnEnv.DEBUG = ''; + } + + const pspawn = ( + cmd, + cargs, + { stdio = 'inherit', env = pspawnEnv, ...rest } = {}, + ) => { + log.debug(chalk.blueBright(cmd, ...cargs)); + const cp = spawn(cmd, cargs, { stdio, env, ...rest }); + const pr = new Promise((resolve, _reject) => { + cp.on('exit', resolve); + cp.on('error', () => resolve(-1)); + }); + pr.cp = cp; + return pr; + }; + + function helper(args, hopts = undefined) { + if (opts.sdk) { + return pspawn('ag-cosmos-helper', args, hopts); + } + + // Don't allocate a TTY if we're not talking to one. + const ttyFlag = process.stdin.isTTY && process.stdout.isTTY ? '-it' : '-i'; + + return pspawn( + 'docker', + [ + 'run', + `--volume=ag-cosmos-helper-state:/root/.ag-cosmos-helper`, + '--rm', + ttyFlag, + '--entrypoint=ag-cosmos-helper', + IMAGE, + ...args, + ], + hopts, + ); + } + + if (popts.pull) { + const status = await pspawn('docker', ['pull', IMAGE]); + if (status) { + return status; + } + } + + return helper(rawArgs.slice(1)); +} diff --git a/packages/agoric-cli/lib/main.js b/packages/agoric-cli/lib/main.js index 0bbcb39d9fa..07ab5a4866a 100644 --- a/packages/agoric-cli/lib/main.js +++ b/packages/agoric-cli/lib/main.js @@ -47,6 +47,15 @@ const main = async (progname, rawArgs, powers) => { ); // Add each of the commands. + program + .command('cosmos ') + .description('client for an Agoric Cosmos chain') + .action(async (command, cmd) => { + const opts = {...program.opts(), ...cmd.opts()}; + const mod = await import('./cosmos'); + return subMain(mod.default, ['cosmos', ...command], opts); + }); + program .command('init ') .description('create a new Dapp directory named ') @@ -114,6 +123,12 @@ const main = async (progname, rawArgs, powers) => { // Throw an error instead of exiting directly. program.exitOverride(); + // Hack: cosmos arguments are always unparsed. + const cosmosIndex = rawArgs.indexOf('cosmos'); + if (cosmosIndex >= 0) { + rawArgs.splice(cosmosIndex + 1, 0, '--'); + } + try { await program.parseAsync(rawArgs, { from: 'user' }); } catch (e) { From deb2a56d40f48ef15962290804e436b67f4c0cc6 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Thu, 18 Jun 2020 09:54:34 -0600 Subject: [PATCH 2/2] chore: lint fix --- packages/agoric-cli/lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/agoric-cli/lib/main.js b/packages/agoric-cli/lib/main.js index 07ab5a4866a..b1938e2b5bc 100644 --- a/packages/agoric-cli/lib/main.js +++ b/packages/agoric-cli/lib/main.js @@ -1,5 +1,6 @@ import { Command } from 'commander'; +import cosmosMain from './cosmos'; import deployMain from './deploy'; import initMain from './init'; import installMain from './install'; @@ -51,9 +52,8 @@ const main = async (progname, rawArgs, powers) => { .command('cosmos ') .description('client for an Agoric Cosmos chain') .action(async (command, cmd) => { - const opts = {...program.opts(), ...cmd.opts()}; - const mod = await import('./cosmos'); - return subMain(mod.default, ['cosmos', ...command], opts); + const opts = { ...program.opts(), ...cmd.opts() }; + return subMain(cosmosMain, ['cosmos', ...command], opts); }); program