Skip to content

Commit

Permalink
Quote command arguments if they have a space (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Sep 25, 2022
1 parent f5549db commit 1571b10
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
16 changes: 15 additions & 1 deletion v2/pkg/commands/build/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ func (b *BaseBuilder) CleanUp() {
})
}

func commandPrettifier(args []string) string {
// If we have a single argument, just return it
if len(args) == 1 {
return args[0]
}
// If an argument contains a space, quote it
for i, arg := range args {
if strings.Contains(arg, " ") {
args[i] = fmt.Sprintf("\"%s\"", arg)
}
}
return strings.Join(args, " ")
}

func (b *BaseBuilder) OutputFilename(options *Options) string {
outputFile := options.OutputFile
if outputFile == "" {
Expand Down Expand Up @@ -270,7 +284,7 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
cmd := exec.Command(compiler, commands.AsSlice()...)
cmd.Stderr = os.Stderr
if verbose {
println(" Build command:", compiler, commands.Join(" "))
println(" Build command:", compiler, commandPrettifier(commands.AsSlice()))
cmd.Stdout = os.Stdout
}
// Set the directory
Expand Down
31 changes: 31 additions & 0 deletions v2/pkg/commands/build/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ func TestUpdateEnv(t *testing.T) {
}

}

func Test_commandPrettifier(t *testing.T) {
tests := []struct {
name string
input []string
want string
}{
{
name: "empty",
input: []string{},
want: "",
},
{
name: "one arg",
input: []string{"one"},
want: "one",
},
{
name: "args where one has spaces",
input: []string{"one", "two three"},
want: `one "two three"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := commandPrettifier(tt.input); got != tt.want {
t.Errorf("commandPrettifier() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1571b10

Please sign in to comment.