fix extra args handling in sg (#57758)

When running `sg test <target> extra-arg1 extra-arg2 ...`, the extra args are generally appended to the `<target>` command on a new line, which means the extra args are interpreted by the shell as their own command. The user's intent is for them to be appended to the last command in the `<target>` command string, which this commit achieves by trimming trailing whitespace.
This commit is contained in:
Quinn Slack 2023-10-19 21:11:59 -07:00 committed by GitHub
parent 7fd4554d91
commit d1ae5ab487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ package sgconf
import (
"io"
"os"
"strings"
"gopkg.in/yaml.v2"
@ -38,6 +39,7 @@ func parseConfig(data []byte) (*Config, error) {
for name, cmd := range conf.Commands {
cmd.Name = name
normalizeCmd(&cmd)
conf.Commands[name] = cmd
}
@ -48,12 +50,19 @@ func parseConfig(data []byte) (*Config, error) {
for name, cmd := range conf.Tests {
cmd.Name = name
normalizeCmd(&cmd)
conf.Tests[name] = cmd
}
return &conf, nil
}
func normalizeCmd(cmd *run.Command) {
// Trim trailing whitespace so extra args apply to last command (instead of being interpreted as
// a new shell command on a separate line).
cmd.Cmd = strings.TrimSpace(cmd.Cmd)
}
type Commandset struct {
Name string `yaml:"-"`
Commands []string `yaml:"commands"`