Merge pull request #519 from yui-knk/extract_parseKeyValue

Extract parsing logic of "key=val" as `parseKeyValue`
docker-18.09
Tõnis Tiigi 2018-07-19 15:59:58 -07:00 committed by GitHub
commit a890def252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 9 deletions

View File

@ -247,12 +247,8 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
// initialize base metadata from image conf // initialize base metadata from image conf
for _, env := range d.image.Config.Env { for _, env := range d.image.Config.Env {
parts := strings.SplitN(env, "=", 2) k, v := parseKeyValue(env)
v := "" d.state = d.state.AddEnv(k, v)
if len(parts) > 1 {
v = parts[1]
}
d.state = d.state.AddEnv(parts[0], v)
} }
if d.image.Config.WorkingDir != "" { if d.image.Config.WorkingDir != "" {
if err = dispatchWorkdir(d, &instructions.WorkdirCommand{Path: d.image.Config.WorkingDir}, false); err != nil { if err = dispatchWorkdir(d, &instructions.WorkdirCommand{Path: d.image.Config.WorkingDir}, false); err != nil {
@ -826,9 +822,8 @@ func splitWildcards(name string) (string, string) {
func addEnv(env []string, k, v string, override bool) []string { func addEnv(env []string, k, v string, override bool) []string {
gotOne := false gotOne := false
for i, envVar := range env { for i, envVar := range env {
envParts := strings.SplitN(envVar, "=", 2) key, _ := parseKeyValue(envVar)
compareFrom := envParts[0] if shell.EqualEnvKeys(key, k) {
if shell.EqualEnvKeys(compareFrom, k) {
if override { if override {
env[i] = k + "=" + v env[i] = k + "=" + v
} }
@ -842,6 +837,16 @@ func addEnv(env []string, k, v string, override bool) []string {
return env return env
} }
func parseKeyValue(env string) (string, string) {
parts := strings.SplitN(env, "=", 2)
v := ""
if len(parts) > 1 {
v = parts[1]
}
return parts[0], v
}
func setKVValue(kvpo instructions.KeyValuePairOptional, values map[string]string) instructions.KeyValuePairOptional { func setKVValue(kvpo instructions.KeyValuePairOptional, values map[string]string) instructions.KeyValuePairOptional {
if v, ok := values[kvpo.Key]; ok { if v, ok := values[kvpo.Key]; ok {
kvpo.Value = &v kvpo.Value = &v

View File

@ -112,3 +112,17 @@ func TestAddEnv(t *testing.T) {
result = addEnv(env, "key3", "val3", true) result = addEnv(env, "key3", "val3", true)
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result) assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
} }
func TestParseKeyValue(t *testing.T) {
k, v := parseKeyValue("key=val")
assert.Equal(t, "key", k)
assert.Equal(t, "val", v)
k, v = parseKeyValue("key=")
assert.Equal(t, "key", k)
assert.Equal(t, "", v)
k, v = parseKeyValue("key")
assert.Equal(t, "key", k)
assert.Equal(t, "", v)
}