From e09e397bc45315fa537fded032e57e678525c0b4 Mon Sep 17 00:00:00 2001 From: Javier Romero Date: Fri, 17 Sep 2021 18:42:49 -0500 Subject: [PATCH] Revert "Allow build --env option to be comma separated list." This reverts commit 50b1ee2bdcf662c39ccda057327d351bdda17372. Signed-off-by: Javier Romero --- internal/commands/build.go | 40 ++------------------------------ internal/commands/build_test.go | 41 --------------------------------- 2 files changed, 2 insertions(+), 79 deletions(-) diff --git a/internal/commands/build.go b/internal/commands/build.go index f5eb668e4..65e06d15d 100644 --- a/internal/commands/build.go +++ b/internal/commands/build.go @@ -4,7 +4,6 @@ import ( "io/ioutil" "os" "path/filepath" - "regexp" "strings" "github.com/google/go-containerregistry/pkg/name" @@ -48,12 +47,6 @@ type BuildFlags struct { PreviousImage string } -// Matches `KEY=VALUE` or `KEY` separated by a coma. -// Any value may be quoted (that is, enclosed within double-quote characters). -// Values with embedded commas or double-quote characters must be quoted. -// Each of the embedded double-quote characters must be represented by a pair of double-quote characters. -var envTokenExp = regexp.MustCompile(`([^=,]+)(?:=(?:([^,"]*)|"((?:[^"]|"")*)"))?(?:,|$)`) - // Build an image from source code func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cobra.Command { var flags BuildFlags @@ -243,14 +236,8 @@ func parseEnv(envFiles []string, envVars []string) (map[string]string, error) { env[k] = v } } - for _, envVarLine := range envVars { - envVarEntries, err := parseEnvLine(envVarLine) - if err != nil { - return nil, errors.Wrapf(err, "failed to parse env line '%s'", envVarLine) - } - for k, v := range envVarEntries { - env[k] = v - } + for _, envVar := range envVars { + env = addEnvVar(env, envVar) } return env, nil } @@ -271,29 +258,6 @@ func parseEnvFile(filename string) (map[string]string, error) { return out, nil } -func parseEnvLine(envLine string) (map[string]string, error) { - out := make(map[string]string) - previousIndex := 0 - for _, matchIndexes := range envTokenExp.FindAllStringSubmatchIndex(envLine, -1) { - if previousIndex != matchIndexes[0] { - return nil, errors.Errorf("invalid syntax between %d and %d", previousIndex, matchIndexes[1]) - } - key := envLine[matchIndexes[2]:matchIndexes[3]] - value := os.Getenv(key) - for _, group := range []int{4, 6} { - if matchIndexes[group] > 0 { - value = envLine[matchIndexes[group]:matchIndexes[group+1]] - } - } - out[key] = strings.ReplaceAll(value, `""`, `"`) - previousIndex = matchIndexes[1] - } - if previousIndex != len(envLine) { - return nil, errors.Errorf("invalid syntax between %d and %d", previousIndex, len(envLine)) - } - return out, nil -} - func addEnvVar(env map[string]string, item string) map[string]string { arr := strings.SplitN(item, "=", 2) if len(arr) > 1 { diff --git a/internal/commands/build_test.go b/internal/commands/build_test.go index b5a6d311c..78f0a28c0 100644 --- a/internal/commands/build_test.go +++ b/internal/commands/build_test.go @@ -427,33 +427,6 @@ func testBuildCommand(t *testing.T, when spec.G, it spec.S) { }) }) - when("env vars allow to use quotes in value", func() { - it("sets flag variables", func() { - mockClient.EXPECT(). - Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{ - "KEY1": `VALUE"`, - })). - Return(nil) - - command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY1="VALUE"""`}) - h.AssertNil(t, command.Execute()) - }) - }) - - when("env vars are passed as coma separated flag", func() { - it("sets flag variables", func() { - mockClient.EXPECT(). - Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{ - "KEY1": "VALUE1", - "KEY2": "VALUE2,VALUE3", - })). - Return(nil) - - command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY1=VALUE1,KEY2="VALUE2,VALUE3"`}) - h.AssertNil(t, command.Execute()) - }) - }) - when("env vars are passed as flags", func() { var ( tmpVar = "tmpVar" @@ -480,20 +453,6 @@ func testBuildCommand(t *testing.T, when spec.G, it spec.S) { h.AssertNil(t, command.Execute()) }) }) - when("env formatting is invalid as regexp does not match from start", func() { - it("returns a parse error", func() { - command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY="VALUE"invalid,KEY2=VALUE`}) - err := command.Execute() - h.AssertError(t, err, `failed to parse env line 'KEY="VALUE"invalid,KEY2=VALUE': invalid syntax between 0 and 19`) - }) - }) - when("env formatting is invalid as regexp does not match at all", func() { - it("returns a parse error", func() { - command.SetArgs([]string{"image", "--builder", "my-builder", "--env", "="}) - err := command.Execute() - h.AssertError(t, err, `failed to parse env line '=': invalid syntax between 0 and 1`) - }) - }) when("build fails", func() { it("should show an error", func() {