From 814721b7c0c1bac2b75e75b97cc58a7efee8ca06 Mon Sep 17 00:00:00 2001 From: "fox.cpp" Date: Sat, 30 Mar 2019 19:57:24 +0300 Subject: [PATCH] Don't use /bin/sh to wrap commands Reliance on /bin/sh makes program unportable and also introduces issues caused by whitespace splitting and escaping. --- main.go | 2 +- pkg/goweight.go | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 4130586..11a05c1 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/pkg/goweight.go b/pkg/goweight.go index 8cb62ff..71751e3 100644 --- a/pkg/goweight.go +++ b/pkg/goweight.go @@ -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) } @@ -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 {