Skip to content

Commit e09e397

Browse files
committed
Revert "Allow build --env option to be comma separated list."
This reverts commit 50b1ee2. Signed-off-by: Javier Romero <rjavier@vmware.com>
1 parent a9c294a commit e09e397

2 files changed

Lines changed: 2 additions & 79 deletions

File tree

internal/commands/build.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"io/ioutil"
55
"os"
66
"path/filepath"
7-
"regexp"
87
"strings"
98

109
"github.com/google/go-containerregistry/pkg/name"
@@ -48,12 +47,6 @@ type BuildFlags struct {
4847
PreviousImage string
4948
}
5049

51-
// Matches `KEY=VALUE` or `KEY` separated by a coma.
52-
// Any value may be quoted (that is, enclosed within double-quote characters).
53-
// Values with embedded commas or double-quote characters must be quoted.
54-
// Each of the embedded double-quote characters must be represented by a pair of double-quote characters.
55-
var envTokenExp = regexp.MustCompile(`([^=,]+)(?:=(?:([^,"]*)|"((?:[^"]|"")*)"))?(?:,|$)`)
56-
5750
// Build an image from source code
5851
func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cobra.Command {
5952
var flags BuildFlags
@@ -243,14 +236,8 @@ func parseEnv(envFiles []string, envVars []string) (map[string]string, error) {
243236
env[k] = v
244237
}
245238
}
246-
for _, envVarLine := range envVars {
247-
envVarEntries, err := parseEnvLine(envVarLine)
248-
if err != nil {
249-
return nil, errors.Wrapf(err, "failed to parse env line '%s'", envVarLine)
250-
}
251-
for k, v := range envVarEntries {
252-
env[k] = v
253-
}
239+
for _, envVar := range envVars {
240+
env = addEnvVar(env, envVar)
254241
}
255242
return env, nil
256243
}
@@ -271,29 +258,6 @@ func parseEnvFile(filename string) (map[string]string, error) {
271258
return out, nil
272259
}
273260

274-
func parseEnvLine(envLine string) (map[string]string, error) {
275-
out := make(map[string]string)
276-
previousIndex := 0
277-
for _, matchIndexes := range envTokenExp.FindAllStringSubmatchIndex(envLine, -1) {
278-
if previousIndex != matchIndexes[0] {
279-
return nil, errors.Errorf("invalid syntax between %d and %d", previousIndex, matchIndexes[1])
280-
}
281-
key := envLine[matchIndexes[2]:matchIndexes[3]]
282-
value := os.Getenv(key)
283-
for _, group := range []int{4, 6} {
284-
if matchIndexes[group] > 0 {
285-
value = envLine[matchIndexes[group]:matchIndexes[group+1]]
286-
}
287-
}
288-
out[key] = strings.ReplaceAll(value, `""`, `"`)
289-
previousIndex = matchIndexes[1]
290-
}
291-
if previousIndex != len(envLine) {
292-
return nil, errors.Errorf("invalid syntax between %d and %d", previousIndex, len(envLine))
293-
}
294-
return out, nil
295-
}
296-
297261
func addEnvVar(env map[string]string, item string) map[string]string {
298262
arr := strings.SplitN(item, "=", 2)
299263
if len(arr) > 1 {

internal/commands/build_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -427,33 +427,6 @@ func testBuildCommand(t *testing.T, when spec.G, it spec.S) {
427427
})
428428
})
429429

430-
when("env vars allow to use quotes in value", func() {
431-
it("sets flag variables", func() {
432-
mockClient.EXPECT().
433-
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{
434-
"KEY1": `VALUE"`,
435-
})).
436-
Return(nil)
437-
438-
command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY1="VALUE"""`})
439-
h.AssertNil(t, command.Execute())
440-
})
441-
})
442-
443-
when("env vars are passed as coma separated flag", func() {
444-
it("sets flag variables", func() {
445-
mockClient.EXPECT().
446-
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{
447-
"KEY1": "VALUE1",
448-
"KEY2": "VALUE2,VALUE3",
449-
})).
450-
Return(nil)
451-
452-
command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY1=VALUE1,KEY2="VALUE2,VALUE3"`})
453-
h.AssertNil(t, command.Execute())
454-
})
455-
})
456-
457430
when("env vars are passed as flags", func() {
458431
var (
459432
tmpVar = "tmpVar"
@@ -480,20 +453,6 @@ func testBuildCommand(t *testing.T, when spec.G, it spec.S) {
480453
h.AssertNil(t, command.Execute())
481454
})
482455
})
483-
when("env formatting is invalid as regexp does not match from start", func() {
484-
it("returns a parse error", func() {
485-
command.SetArgs([]string{"image", "--builder", "my-builder", "--env", `KEY="VALUE"invalid,KEY2=VALUE`})
486-
err := command.Execute()
487-
h.AssertError(t, err, `failed to parse env line 'KEY="VALUE"invalid,KEY2=VALUE': invalid syntax between 0 and 19`)
488-
})
489-
})
490-
when("env formatting is invalid as regexp does not match at all", func() {
491-
it("returns a parse error", func() {
492-
command.SetArgs([]string{"image", "--builder", "my-builder", "--env", "="})
493-
err := command.Execute()
494-
h.AssertError(t, err, `failed to parse env line '=': invalid syntax between 0 and 1`)
495-
})
496-
})
497456

498457
when("build fails", func() {
499458
it("should show an error", func() {

0 commit comments

Comments
 (0)