Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement agoric cosmos ... and agoric cosmos provision #1086

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/agoric-cli/lib/cosmos.js
Original file line number Diff line number Diff line change
@@ -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));
}
15 changes: 15 additions & 0 deletions packages/agoric-cli/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from 'commander';

import cosmosMain from './cosmos';
import deployMain from './deploy';
import initMain from './init';
import installMain from './install';
Expand Down Expand Up @@ -47,6 +48,14 @@ const main = async (progname, rawArgs, powers) => {
);

// Add each of the commands.
program
.command('cosmos <command...>')
.description('client for an Agoric Cosmos chain')
.action(async (command, cmd) => {
const opts = { ...program.opts(), ...cmd.opts() };
return subMain(cosmosMain, ['cosmos', ...command], opts);
});

program
.command('init <project>')
.description('create a new Dapp directory named <project>')
Expand Down Expand Up @@ -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) {
Expand Down