Skip to content

Commit

Permalink
fix(cli): regression in exec command parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald authored and thsig committed Feb 5, 2021
1 parent 7591cb1 commit bea46ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,7 +66,7 @@ export class ExecCommand extends Command<Args> {

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)}`,
Expand All @@ -75,7 +76,7 @@ export class ExecCommand extends Command<Args> {

async action({ garden, log, args, opts }: CommandParams<Args, Opts>): Promise<CommandResult<ExecInServiceResult>> {
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)
Expand All @@ -89,4 +90,8 @@ export class ExecCommand extends Command<Args> {

return { result }
}

private getCommand(args: ParameterValues<Args>) {
return args.command.split(" ") || []
}
}
5 changes: 5 additions & 0 deletions core/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -215,6 +216,10 @@ export const testPlugin = () =>
return { state: "ready", detail: {} }
},

async execInService({ command }: ExecInServiceParams): Promise<ExecInServiceResult> {
return { code: 0, output: "Ran command: " + command.join(" ") }
},

async runService({
ctx,
service,
Expand Down
41 changes: 41 additions & 0 deletions core/test/unit/src/commands/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <[email protected]>
*
* 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")
})
})

0 comments on commit bea46ed

Please sign in to comment.