Skip to content

Commit

Permalink
Revert "Allow build --env option to be comma separated list."
Browse files Browse the repository at this point in the history
This reverts commit 50b1ee2.

Signed-off-by: Javier Romero <rjavier@vmware.com>
  • Loading branch information
jromero committed Sep 17, 2021
1 parent a9c294a commit e09e397
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 79 deletions.
40 changes: 2 additions & 38 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
41 changes: 0 additions & 41 deletions internal/commands/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down

0 comments on commit e09e397

Please sign in to comment.