Extract parsing logic of "key=val" as `parseKeyValue`

Signed-off-by: Yuichiro Kaneko <spiketeika@gmail.com>
docker-18.09
Yuichiro Kaneko 2018-07-19 21:36:07 +09:00
parent db8b382274
commit 83dc829222
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
for _, env := range d.image.Config.Env {
parts := strings.SplitN(env, "=", 2)
v := ""
if len(parts) > 1 {
v = parts[1]
}
d.state = d.state.AddEnv(parts[0], v)
k, v := parseKeyValue(env)
d.state = d.state.AddEnv(k, v)
}
if d.image.Config.WorkingDir != "" {
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 {
gotOne := false
for i, envVar := range env {
envParts := strings.SplitN(envVar, "=", 2)
compareFrom := envParts[0]
if shell.EqualEnvKeys(compareFrom, k) {
key, _ := parseKeyValue(envVar)
if shell.EqualEnvKeys(key, k) {
if override {
env[i] = k + "=" + v
}
@ -842,6 +837,16 @@ func addEnv(env []string, k, v string, override bool) []string {
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 {
if v, ok := values[kvpo.Key]; ok {
kvpo.Value = &v

View File

@ -112,3 +112,17 @@ func TestAddEnv(t *testing.T) {
result = addEnv(env, "key3", "val3", true)
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)
}