diff --git a/core/src/commands/exec.ts b/core/src/commands/exec.ts index 10bbca56e5..f39436bf33 100644 --- a/core/src/commands/exec.ts +++ b/core/src/commands/exec.ts @@ -12,13 +12,14 @@ import { ExecInServiceResult, execInServiceResultSchema } from "../types/plugin/ import { printHeader } from "../logger/util" import { Command, CommandResult, CommandParams } from "./base" import dedent = require("dedent") -import { StringParameter, BooleanParameter } from "../cli/params" +import { StringParameter, BooleanParameter, ParameterValues } from "../cli/params" const execArgs = { service: new StringParameter({ help: "The service to exec the command in.", required: true, }), + // TODO: make this variadic command: new StringParameter({ help: "The command to run.", required: true, @@ -65,7 +66,7 @@ export class ExecCommand extends Command { printHeader({ headerLog, args }) { const serviceName = args.service - const command = args.command || [] + const command = this.getCommand(args) printHeader( headerLog, `Running command ${chalk.cyan(command.join(" "))} in service ${chalk.cyan(serviceName)}`, @@ -75,7 +76,7 @@ export class ExecCommand extends Command { async action({ garden, log, args, opts }: CommandParams): Promise> { const serviceName = args.service - const command = args?.command.split(" ") || [] + const command = this.getCommand(args) const graph = await garden.getConfigGraph(log) const service = graph.getService(serviceName) @@ -89,4 +90,8 @@ export class ExecCommand extends Command { return { result } } + + private getCommand(args: ParameterValues) { + return args.command.split(" ") || [] + } } diff --git a/core/test/helpers.ts b/core/test/helpers.ts index 9c435cd12e..ce18b538d4 100644 --- a/core/test/helpers.ts +++ b/core/test/helpers.ts @@ -46,6 +46,7 @@ import { AnalyticsGlobalConfig } from "../src/config-store" import { TestGarden, EventLogEntry } from "../src/util/testing" import { Logger } from "../src/logger/logger" import { LogLevel } from "../src/logger/log-node" +import { ExecInServiceParams, ExecInServiceResult } from "../src/types/plugin/service/execInService" export { TempDirectory, makeTempDir } from "../src/util/fs" export { TestGarden, TestError, TestEventBus } from "../src/util/testing" @@ -215,6 +216,10 @@ export const testPlugin = () => return { state: "ready", detail: {} } }, + async execInService({ command }: ExecInServiceParams): Promise { + return { code: 0, output: "Ran command: " + command.join(" ") } + }, + async runService({ ctx, service, diff --git a/core/test/unit/src/commands/exec.ts b/core/test/unit/src/commands/exec.ts new file mode 100644 index 0000000000..13f79802b5 --- /dev/null +++ b/core/test/unit/src/commands/exec.ts @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2018-2020 Garden Technologies, Inc. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import { expect } from "chai" +import { ExecCommand } from "../../../../src/commands/exec" +import { makeTestGardenA, withDefaultGlobalOpts } from "../../../helpers" + +describe("ExecCommand", () => { + const command = new ExecCommand() + + it("should exec a command in a running service", async () => { + const garden = await makeTestGardenA() + const log = garden.log + + const args = { service: "service-a", command: "echo ok" } + + command.printHeader({ headerLog: log, args }) + + const { result, errors } = await command.action({ + garden, + log, + headerLog: log, + footerLog: log, + args, + opts: withDefaultGlobalOpts({ + interactive: false, + }), + }) + + if (errors) { + throw errors[0] + } + + expect(result?.output).to.equal("Ran command: echo ok") + }) +})