Skip to content

Commit

Permalink
Don't use /bin/sh to wrap commands
Browse files Browse the repository at this point in the history
Reliance on /bin/sh makes program unportable and also introduces issues
caused by whitespace splitting and escaping.
  • Loading branch information
foxcpp committed Mar 30, 2019
1 parent 9eedfc3 commit 814721b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
kingpin.Parse()
weight := pkg.NewGoWeight()
if *buildTags != "" {
weight.BuildArgs = "-tags " + *buildTags
weight.BuildCmd = append(weight.BuildCmd, "-tags", *buildTags)
}

work := weight.BuildCurrent()
Expand Down
12 changes: 7 additions & 5 deletions pkg/goweight.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

var moduleRegex = regexp.MustCompile("packagefile (.*)=(.*)")

func run(cmd string) string {
out, err := exec.Command("sh", "-c", cmd).Output()
func run(cmd []string) string {
out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -48,15 +48,17 @@ type ModuleEntry struct {
SizeHuman string `json:"size_human"`
}
type GoWeight struct {
BuildArgs string
BuildCmd []string
}

func NewGoWeight() *GoWeight {
return &GoWeight{}
return &GoWeight{
BuildCmd: []string{"go", "build", "-work", "-a"},
}
}

func (g *GoWeight) BuildCurrent() string {
return strings.Split(strings.TrimSpace(run("go build "+g.BuildArgs+" -work -a 2>&1")), "=")[1]
return strings.Split(strings.TrimSpace(run(g.BuildCmd)), "=")[1]
}
func (g *GoWeight) Process(work string) []*ModuleEntry {

Expand Down

0 comments on commit 814721b

Please sign in to comment.