From 1ef979a7bb195c5c8d72b86b1c11a7a9372f876f Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 1 Jul 2019 15:32:04 -0700 Subject: [PATCH] llb: fix excessive formatting Signed-off-by: Tonis Tiigi --- client/client_test.go | 2 +- client/llb/exec.go | 14 ++++++++++---- client/llb/meta.go | 22 +++++++++++++++------- client/llb/state.go | 8 ++++---- client/llb/state_test.go | 22 ++++++++++++++++++++++ 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 788213b2..1de618f6 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -713,7 +713,7 @@ func testSecretMounts(t *testing.T, sb integration.Sandbox) { // test id,perm,uid st = llb.Image("busybox:latest"). - Run(llb.Shlex(`sh -c '[ "$(stat -c "%%u %%g %%f" /run/secrets/mysecret4)" = "1 1 81ff" ]' `), llb.AddSecret("/run/secrets/mysecret4", llb.SecretID("mysecret"), llb.SecretFileOpt(1, 1, 0777))) + Run(llb.Shlex(`sh -c '[ "$(stat -c "%u %g %f" /run/secrets/mysecret4)" = "1 1 81ff" ]' `), llb.AddSecret("/run/secrets/mysecret4", llb.SecretID("mysecret"), llb.SecretFileOpt(1, 1, 0777))) def, err = st.Marshal() require.NoError(t, err) diff --git a/client/llb/exec.go b/client/llb/exec.go index 8e2d1d4c..ade99278 100644 --- a/client/llb/exec.go +++ b/client/llb/exec.go @@ -427,11 +427,13 @@ func Security(s pb.SecurityMode) RunOption { } func Shlex(str string) RunOption { - return Shlexf(str) + return runOptionFunc(func(ei *ExecInfo) { + ei.State = shlexf(str, false)(ei.State) + }) } func Shlexf(str string, v ...interface{}) RunOption { return runOptionFunc(func(ei *ExecInfo) { - ei.State = shlexf(str, v...)(ei.State) + ei.State = shlexf(str, true, v...)(ei.State) }) } @@ -442,7 +444,9 @@ func Args(a []string) RunOption { } func AddEnv(key, value string) RunOption { - return AddEnvf(key, value) + return runOptionFunc(func(ei *ExecInfo) { + ei.State = ei.State.AddEnv(key, value) + }) } func AddEnvf(key, value string, v ...interface{}) RunOption { @@ -458,7 +462,9 @@ func User(str string) RunOption { } func Dir(str string) RunOption { - return Dirf(str) + return runOptionFunc(func(ei *ExecInfo) { + ei.State = ei.State.Dir(str) + }) } func Dirf(str string, v ...interface{}) RunOption { return runOptionFunc(func(ei *ExecInfo) { diff --git a/client/llb/meta.go b/client/llb/meta.go index 78a24730..54b14c4c 100644 --- a/client/llb/meta.go +++ b/client/llb/meta.go @@ -24,19 +24,24 @@ var ( keySecurity = contextKeyT("llb.security") ) -func addEnvf(key, value string, v ...interface{}) StateOption { +func addEnvf(key, value string, replace bool, v ...interface{}) StateOption { + if replace { + value = fmt.Sprintf(value, v...) + } return func(s State) State { - return s.WithValue(keyEnv, getEnv(s).AddOrReplace(key, fmt.Sprintf(value, v...))) + return s.WithValue(keyEnv, getEnv(s).AddOrReplace(key, value)) } } func dir(str string) StateOption { - return dirf(str) + return dirf(str, false) } -func dirf(str string, v ...interface{}) StateOption { +func dirf(value string, replace bool, v ...interface{}) StateOption { + if replace { + value = fmt.Sprintf(value, v...) + } return func(s State) State { - value := fmt.Sprintf(str, v...) if !path.IsAbs(value) { prev := getDir(s) if prev == "" { @@ -100,9 +105,12 @@ func args(args ...string) StateOption { } } -func shlexf(str string, v ...interface{}) StateOption { +func shlexf(str string, replace bool, v ...interface{}) StateOption { + if replace { + str = fmt.Sprintf(str, v...) + } return func(s State) State { - arg, err := shlex.Split(fmt.Sprintf(str, v...)) + arg, err := shlex.Split(str) if err != nil { // TODO: handle error } diff --git a/client/llb/state.go b/client/llb/state.go index 928242af..ba8845e0 100644 --- a/client/llb/state.go +++ b/client/llb/state.go @@ -240,18 +240,18 @@ func (s State) File(a *FileAction, opts ...ConstraintsOpt) State { } func (s State) AddEnv(key, value string) State { - return s.AddEnvf(key, value) + return addEnvf(key, value, false)(s) } func (s State) AddEnvf(key, value string, v ...interface{}) State { - return addEnvf(key, value, v...)(s) + return addEnvf(key, value, true, v...)(s) } func (s State) Dir(str string) State { - return s.Dirf(str) + return dirf(str, false)(s) } func (s State) Dirf(str string, v ...interface{}) State { - return dirf(str, v...)(s) + return dirf(str, true, v...)(s) } func (s State) GetEnv(key string) (string, bool) { diff --git a/client/llb/state_test.go b/client/llb/state_test.go index aea7598b..c08aa91b 100644 --- a/client/llb/state_test.go +++ b/client/llb/state_test.go @@ -28,3 +28,25 @@ func TestStateMeta(t *testing.T) { assert.True(t, ok) assert.Equal(t, "abc", v) } + +func TestFormattingPatterns(t *testing.T) { + t.Parallel() + + s := Image("foo") + s = s.AddEnv("FOO", "ab%sc").Dir("/foo/bar%d") + + v, ok := s.GetEnv("FOO") + assert.True(t, ok) + assert.Equal(t, "ab%sc", v) + + assert.Equal(t, "/foo/bar%d", s.GetDir()) + + s2 := Image("foo") + s2 = s2.AddEnvf("FOO", "ab%sc", "__").Dirf("/foo/bar%d", 1) + + v, ok = s2.GetEnv("FOO") + assert.True(t, ok) + assert.Equal(t, "ab__c", v) + + assert.Equal(t, "/foo/bar1", s2.GetDir()) +}