Skip to content

Commit

Permalink
Merge pull request #397 from CircleCI-Public/arguments
Browse files Browse the repository at this point in the history
Pass string array arguments to docker correctly.
  • Loading branch information
marcomorain authored Apr 23, 2020
2 parents faffb2f + 6ab236e commit 6da924f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 24 additions & 1 deletion local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func buildAgentArguments(flags *pflag.FlagSet) ([]string, string) {
// build a list of all supplied flags, that we will pass on to build-agent
flags.Visit(func(flag *pflag.Flag) {
if flag.Name != "config" && flag.Name != "debug" {
result = append(result, "--"+flag.Name, flag.Value.String())
result = append(result, unparseFlag(flags, flag)...)
}
})
result = append(result, flags.Args()...)
Expand Down Expand Up @@ -297,3 +297,26 @@ func generateDockerCommand(configPath, image, pwd string, arguments ...string) [
image, "circleci", "build", "--config", configPathInsideContainer}
return append(core, arguments...)
}

// Convert the given flag back into a list of strings suitable to be passed on
// the command line to run docker.
// https://github.com/CircleCI-Public/circleci-cli/issues/391
func unparseFlag(flags *pflag.FlagSet, flag *pflag.Flag) []string {
flagName := "--" + flag.Name
result := []string{}
switch flag.Value.Type() {
// A stringArray type argument is collapsed into a single flag:
// `--foo 1 --foo 2` will result in a single `foo` flag with an array of values.
case "stringArray":
vals, err := flags.GetStringArray(flag.Name)
if err != nil {
panic("Failed reading string array from flag that must be a string array")
}
for _, val := range vals {
result = append(result, flagName, val)
}
default:
result = append(result, flagName, flag.Value.String())
}
return result
}
6 changes: 6 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ var _ = Describe("build", func() {
expectedArgs: []string{"--index", "9", "--job", "horse", "d"},
}),

Entry("many args, multiple envs", TestCase{
input: []string{"--env", "foo", "--env", "bar", "--env", "baz"},
expectedConfigPath: ".circleci/config.yml",
expectedArgs: []string{"--env", "foo", "--env", "bar", "--env", "baz"},
}),

Entry("args that are not flags", TestCase{
input: []string{"a", "--debug", "b", "--config", "foo", "d"},
expectedConfigPath: "foo",
Expand Down

0 comments on commit 6da924f

Please sign in to comment.