Skip to content

Commit

Permalink
chore: keep optional command arg to avoid breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvemo committed Nov 22, 2023
1 parent 60c476e commit 26fb76a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
11 changes: 9 additions & 2 deletions core/src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const execArgs = {
return Object.keys(configDump.actionConfigs.Deploy)
},
}),
command: new StringParameter({
help: "The command to run.",
required: false,
}),
}

const execOpts = {
Expand Down Expand Up @@ -57,12 +61,15 @@ export class ExecCommand extends Command<Args, Opts> {
override description = dedent`
Finds an active container for a deployed Deploy and executes the given command within the container.
Supports interactive shells.
You can specify the command to run as a parameter, or pass it after a \`--\` separator. For commands
with arguments or quoted substrings, use the \`--\` separator.
_NOTE: This command may not be supported for all action types._
Examples:
garden exec my-service -- /bin/sh # runs a shell in the my-service Deploy's container
garden exec my-service /bin/sh # runs an interactive shell in the my-service Deploy's container
garden exec my-service -- /bin/sh -c echo "hello world" # prints "hello world" in the my-service Deploy's container and exits
`

override arguments = execArgs
Expand Down Expand Up @@ -160,6 +167,6 @@ export class ExecCommand extends Command<Args, Opts> {
}

private getCommand(args: ParameterValues<Args>) {
return args["--"] || []
return args.command ? args.command.split(" ") : args["--"] || []
}
}
26 changes: 23 additions & 3 deletions core/test/integ/src/plugins/kubernetes/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe("runExecCommand", () => {
}
})

it("should exec a command in a running service", async () => {
it("should exec a command in a running service using the -- separator", async () => {
const execCommand = new ExecCommand()
const args = { deploy: "simple-service" }
const args = { deploy: "simple-service", command: "" }
args["--"] = ["echo", "ok, lots of text"]

const { result, errors } = await execCommand.action({
Expand All @@ -66,9 +66,29 @@ describe("runExecCommand", () => {
expect(result?.output).to.equal("ok, lots of text")
})

it("should exec a command in a running service without the -- separator", async () => {
const execCommand = new ExecCommand()
const args = { deploy: "simple-service", command: "echo hello" }

const { result, errors } = await execCommand.action({
garden,
log: garden.log,
args,
opts: withDefaultGlobalOpts({
interactive: false,
target: "",
}),
})

if (errors) {
throw errors[0]
}
expect(result?.output).to.equal("hello")
})

it("should throw if no command was specified", async () => {
const execCommand = new ExecCommand()
const args = { deploy: "simple-service" }
const args = { deploy: "simple-service", command: "" }
await expectError(
() =>
execCommand.action({
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("ExecCommand", () => {
const garden = await makeTestGardenA()
const log = garden.log

const args = { deploy: "service-a" }
const args = { deploy: "service-a", command: "" }
args["--"] = ["echo", "ok"]

command.printHeader({ log, args })
Expand Down
8 changes: 6 additions & 2 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1346,22 +1346,26 @@ run:
Finds an active container for a deployed Deploy and executes the given command within the container.
Supports interactive shells.
You can specify the command to run as a parameter, or pass it after a `--` separator. For commands
with arguments, use the `--` separator.

_NOTE: This command may not be supported for all action types._

Examples:

garden exec my-service -- /bin/sh # runs a shell in the my-service Deploy's container
garden exec my-service /bin/sh # runs an interactive shell in the my-service Deploy's container
garden exec my-service -- /bin/sh -c echo "hello world" # prints "hello world" in the my-service Deploy's container and exits

#### Usage

garden exec <deploy> [options]
garden exec <deploy> [command] [options]

#### Arguments

| Argument | Required | Description |
| -------- | -------- | ----------- |
| `deploy` | Yes | The running Deploy action to exec the command in.
| `command` | No | The command to run.

#### Options

Expand Down

0 comments on commit 26fb76a

Please sign in to comment.