Skip to content

Commit

Permalink
Handles build flags that contain whitespace (#26)
Browse files Browse the repository at this point in the history
Resolves #23
  • Loading branch information
ryanmoran authored Jul 29, 2020
1 parent 5d6f092 commit b0c8297
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
15 changes: 14 additions & 1 deletion build_configuration_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (p BuildConfigurationParser) Parse(path string) ([]string, []string, error)

var buildFlags []string
for _, flag := range config.Go.Build.Flags {
buildFlags = append(buildFlags, strings.SplitN(flag, "=", 2)...)
buildFlags = append(buildFlags, splitFlags(flag)...)
}
config.Go.Build.Flags = buildFlags

Expand All @@ -72,3 +72,16 @@ func (p BuildConfigurationParser) Parse(path string) ([]string, []string, error)

return config.Go.Targets, config.Go.Build.Flags, nil
}

func splitFlags(flag string) []string {
parts := strings.SplitN(flag, "=", 2)
if len(parts) == 2 {
if len(parts[1]) >= 2 {
if c := parts[1][len(parts[1])-1]; parts[1][0] == c && (c == '"' || c == '\'') {
parts[1] = parts[1][1 : len(parts[1])-1]
}
}
}

return parts
}
16 changes: 14 additions & 2 deletions build_configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ go:
- -first
- value
- -second=value
- -third="value"
- -fourth='value'
`)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -52,7 +54,12 @@ go:
targets, flags, err := parser.Parse(path)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"./first", "./second"}))
Expect(flags).To(Equal([]string{"-first", "value", "-second", "value"}))
Expect(flags).To(Equal([]string{
"-first", "value",
"-second", "value",
"-third", "value",
"-fourth", "value",
}))
})

context("when there is no buildpack.yml file", func() {
Expand Down Expand Up @@ -110,7 +117,12 @@ go:
targets, flags, err := parser.Parse(path)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"./some/target1", "./some/target2"}))
Expect(flags).To(Equal([]string{"-first", "value", "-second", "value"}))
Expect(flags).To(Equal([]string{
"-first", "value",
"-second", "value",
"-third", "value",
"-fourth", "value",
}))
})
})

Expand Down
18 changes: 17 additions & 1 deletion go_build_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"unicode"

"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/pexec"
Expand Down Expand Up @@ -79,7 +81,11 @@ func (p GoBuildProcess) Execute(config GoBuildConfiguration) (string, error) {
env = append(env, fmt.Sprintf("GOPATH=%s", config.GoPath))
}

p.logs.Subprocess("Running '%s'", strings.Join(append([]string{"go"}, args...), " "))
printedArgs := []string{"go"}
for _, arg := range args {
printedArgs = append(printedArgs, formatArg(arg))
}
p.logs.Subprocess("Running '%s'", strings.Join(printedArgs, " "))

buffer := bytes.NewBuffer(nil)
duration, err := p.clock.Measure(func() error {
Expand Down Expand Up @@ -112,3 +118,13 @@ func (p GoBuildProcess) Execute(config GoBuildConfiguration) (string, error) {

return paths[0], nil
}

func formatArg(arg string) string {
for _, r := range arg {
if unicode.IsSpace(r) {
return strconv.Quote(arg)
}
}

return arg
}
6 changes: 3 additions & 3 deletions go_build_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {
Output: filepath.Join(layerPath, "bin"),
GoCache: goCache,
Targets: []string{"."},
Flags: []string{"-buildmode", "default", "-tags", "paketo", "-mod", "mod"},
Flags: []string{"-buildmode", "default", "-ldflags", "-X main.variable=some-value", "-mod", "mod"},
})
Expect(err).NotTo(HaveOccurred())
Expect(command).To(Equal(filepath.Join(layerPath, "bin", "a_command")))
Expand All @@ -172,15 +172,15 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {
"build",
"-o", filepath.Join(layerPath, "bin"),
"-buildmode", "default",
"-tags", "paketo",
"-ldflags", "-X main.variable=some-value",
"-mod", "mod",
".",
}))
Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workspacePath))
Expect(executable.ExecuteCall.Receives.Execution.Env).To(ContainElement(fmt.Sprintf("GOCACHE=%s", goCache)))

Expect(logs.String()).To(ContainSubstring(" Executing build process"))
Expect(logs.String()).To(ContainSubstring(fmt.Sprintf(" Running 'go build -o %s -buildmode default -tags paketo -mod mod .'", filepath.Join(layerPath, "bin"))))
Expect(logs.String()).To(ContainSubstring(fmt.Sprintf(` Running 'go build -o %s -buildmode default -ldflags "-X main.variable=some-value" -mod mod .'`, filepath.Join(layerPath, "bin"))))
Expect(logs.String()).To(ContainSubstring(" Completed in 1s"))
})
})
Expand Down
3 changes: 2 additions & 1 deletion integration/build_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ func testBuildFlags(t *testing.T, context spec.G, it spec.S) {
content, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("go1.14"))
Expect(string(content)).To(ContainSubstring(`variable value: "some-value"`))
Expect(string(content)).To(ContainSubstring("/workspace contents: []"))

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Executing build process",
fmt.Sprintf(" Running 'go build -o /layers/%s/targets/bin -buildmode default -tags paketo .'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
fmt.Sprintf(" Running 'go build -o /layers/%s/targets/bin -buildmode default -tags paketo -ldflags \"-X main.variable=some-value\" .'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
MatchRegexp(` Completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
"",
" Assigning launch processes",
Expand Down
1 change: 1 addition & 0 deletions integration/testdata/build_flags/buildpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ go:
flags:
- -buildmode=default
- -tags=paketo
- -ldflags="-X main.variable=some-value"
3 changes: 3 additions & 0 deletions integration/testdata/build_flags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"runtime"
)

var variable string

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, runtime.Version())
fmt.Fprintf(w, "variable value: %q\n", variable)

paths, _ := filepath.Glob("/workspace/*")
fmt.Fprintf(w, "/workspace contents: %v\n", paths)
Expand Down

0 comments on commit b0c8297

Please sign in to comment.