run-in-container
Version:
CLI to assist running commands inside containers
78 lines (77 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const child_process_1 = require("child_process");
const configuration_1 = tslib_1.__importDefault(require("../configuration"));
const defaults_1 = require("../configuration/defaults");
const cli_1 = tslib_1.__importDefault(require("../containerRuntimes/cli"));
const resolveConfigurationToCliFlags = (options, resolvedFlags, // TODO can this be made more specific?
subCommand) => {
let parsedArgv = subCommand ? [subCommand] : [];
for (const [flagKey, flagValue] of Object.entries(resolvedFlags)) {
if (flagValue) {
if (typeof flagValue === "boolean") {
parsedArgv = parsedArgv.concat([
`${options[flagKey]}`,
]);
}
else if (typeof flagValue === "string") {
parsedArgv = parsedArgv
.concat([`${options[flagKey]}`])
.concat(flagValue);
}
}
}
return parsedArgv;
};
class Run extends command_1.Command {
async run() {
const parseOutput = this.parse(Run);
this.debug(JSON.stringify(parseOutput));
const { argv, flags } = parseOutput;
const containerRuntime = configuration_1.default.get("containerRuntime" /* containerRuntime */);
const { executable, subCommand, options } = cli_1.default[containerRuntime];
const resolvedFlags = Object.assign(Object.assign({}, defaults_1.getDefaults(configuration_1.default, containerRuntime, "")), flags);
const parsedArgv = resolveConfigurationToCliFlags(options, resolvedFlags, subCommand);
const processArgv = parsedArgv.concat(argv);
this.debug(`parsedArgv: ${parsedArgv}`);
this.debug(`executable: ${executable}`);
this.debug(`subCommand: ${subCommand}`);
this.debug(`argv: ${argv}`);
this.debug(`processArgv: ${processArgv}`);
const containerProcess = child_process_1.spawnSync(executable, processArgv, {
stdio: "inherit",
});
if (containerProcess.error) {
this.log(JSON.stringify(containerProcess.error));
this.exit(1);
}
}
}
exports.default = Run;
Run.description = `Run a container. Similar to 'docker run'.
Run a container. Similar to 'docker run'.
Any unrecognized arguments will be passed directly to the underlying CLI`;
Run.strict = false;
Run.flags = {
interactive: command_1.flags.boolean({
char: "i",
default: false,
allowNo: true,
}),
tty: command_1.flags.boolean({
char: "t",
default: false,
allowNo: true,
}),
volume: command_1.flags.string({ char: "v", multiple: true }),
};
// static args = [{ name: "file" }]
Run.usage = `run [CONTAINER OPTIONS]`;
Run.examples = [
`$ run-in-container run alpine echo "Hello world"`,
`$ run-in-container run --interactive --tty alpine sh`,
`$ run-in-container run --no-interactive --no-tty alpine sh`,
`$ run-in-container run -it alpine`,
];