From 14c1ea0e110003ae26b5184a8cf5dca58e25a4ca Mon Sep 17 00:00:00 2001 From: Kohei Tokunaga Date: Wed, 24 May 2023 10:43:27 +0900 Subject: [PATCH] invoke: Allow JSON array for long form flags This commit allows specifying a JSON array to the long-form arg, entrypoint and envvars. Non-JSON-array value can still be specified. Buildx treats the value as a JSON array only when it can be parsed as a JSON array. Signed-off-by: Kohei Tokunaga --- commands/build.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/commands/build.go b/commands/build.go index b8348f7b..15355ce7 100644 --- a/commands/build.go +++ b/commands/build.go @@ -704,6 +704,7 @@ func parseInvokeConfig(invoke string) (cfg invokeConfig, err error) { } csvReader := csv.NewReader(strings.NewReader(invoke)) + csvReader.LazyQuotes = true fields, err := csvReader.Read() if err != nil { return cfg, err @@ -723,14 +724,14 @@ func parseInvokeConfig(invoke string) (cfg invokeConfig, err error) { value := parts[1] switch key { case "args": - cfg.Cmd = append(cfg.Cmd, value) // TODO: support JSON + cfg.Cmd = append(cfg.Cmd, maybeJSONArray(value)...) case "entrypoint": - cfg.Entrypoint = append(cfg.Entrypoint, value) // TODO: support JSON + cfg.Entrypoint = append(cfg.Entrypoint, maybeJSONArray(value)...) if cfg.Cmd == nil { cfg.Cmd = []string{} } case "env": - cfg.Env = append(cfg.Env, value) + cfg.Env = append(cfg.Env, maybeJSONArray(value)...) case "user": cfg.User = value cfg.NoUser = false @@ -749,6 +750,14 @@ func parseInvokeConfig(invoke string) (cfg invokeConfig, err error) { return cfg, nil } +func maybeJSONArray(v string) []string { + var list []string + if err := json.Unmarshal([]byte(v), &list); err == nil { + return list + } + return []string{v} +} + func listToMap(values []string, defaultEnv bool) map[string]string { result := make(map[string]string, len(values)) for _, value := range values {