Skip to content

Commit a93627a

Browse files
authored
feat: support --{tla,ext}-{code,str}-file flag in "tk eval" (#1238)
Convert the flag values to `import/importstr @"verbatim ""quoted"" string"`, pretty much like go-jsonnet does it. Implements #848
1 parent feac755 commit a93627a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

cmd/tk/flags.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strings"
67

78
"github.com/rs/zerolog/log"
@@ -78,12 +79,16 @@ func cliCodeParser(fs *pflag.FlagSet) (func() map[string]string, func() map[stri
7879
// need to use StringArray instead of StringSlice, because pflag attempts to
7980
// parse StringSlice using the csv parser, which breaks when passing objects
8081
extCode := fs.StringArray("ext-code", nil, "Set code value of extVar (Format: key=<code>)")
82+
extCodeFile := fs.StringArray("ext-code-file", nil, "Set code value of extVar from file (Format: key=filename)")
8183
extStr := fs.StringArrayP("ext-str", "V", nil, "Set string value of extVar (Format: key=value)")
84+
extStrFile := fs.StringArray("ext-str-file", nil, "Set string value of extVar from file (Format: key=filename)")
8285

8386
tlaCode := fs.StringArray("tla-code", nil, "Set code value of top level function (Format: key=<code>)")
87+
tlaCodeFile := fs.StringArray("tla-code-file", nil, "Set code value of top level function from file (Format: key=filename)")
8488
tlaStr := fs.StringArrayP("tla-str", "A", nil, "Set string value of top level function (Format: key=value)")
89+
tlaStrFile := fs.StringArray("tla-str-file", nil, "Set string value of top level function from file (Format: key=filename)")
8590

86-
newParser := func(kind string, code, str *[]string) func() map[string]string {
91+
newParser := func(kind string, code, str, codeFile, strFile *[]string) func() map[string]string {
8792
return func() map[string]string {
8893
m := make(map[string]string)
8994
for _, s := range *code {
@@ -107,12 +112,28 @@ func cliCodeParser(fs *pflag.FlagSet) (func() map[string]string, func() map[stri
107112
}
108113
m[split[0]] = string(js)
109114
}
115+
116+
for _, x := range []struct {
117+
arg *[]string
118+
kind2, imp string
119+
}{
120+
{arg: codeFile, kind2: "code", imp: "import"},
121+
{arg: strFile, kind2: "str", imp: "importstr"},
122+
} {
123+
for _, s := range *x.arg {
124+
split := strings.SplitN(s, "=", 2)
125+
if len(split) != 2 {
126+
log.Fatal().Msgf("%s-%s-file argument has wrong format: `%s`. Expected `key=filename`", kind, x.kind2, s)
127+
}
128+
m[split[0]] = fmt.Sprintf(`%s @"%s"`, x.imp, strings.ReplaceAll(split[1], `"`, `""`))
129+
}
130+
}
110131
return m
111132
}
112133
}
113134

114-
return newParser("ext", extCode, extStr),
115-
newParser("tla", tlaCode, tlaStr)
135+
return newParser("ext", extCode, extStr, extCodeFile, extStrFile),
136+
newParser("tla", tlaCode, tlaStr, tlaCodeFile, tlaStrFile)
116137
}
117138

118139
func envSettingsFlags(env *v1alpha1.Environment, fs *pflag.FlagSet) {

cmd/tk/flags_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,26 @@ func TestCliCodeParser(t *testing.T) {
1717
"--tla-code", "tc=2+3",
1818
"-A", "ts2=ts2", // tla-str
1919
"-V", "es2=es2", // ext-str
20+
"--ext-str-file", `esf=e"sf.txt`,
21+
"--tla-str-file", `tsf=t"s"f.txt`,
22+
"--ext-code-file", `ecf=e"cf.json`,
23+
"--tla-code-file", `tcf=t"c"f.json`,
2024
})
2125
assert.NoError(t, err)
2226
ext := parseExt()
2327
assert.Equal(t, map[string]string{
2428
"es": `"1a \" ` + "\U0001f605" + ` ' b\nc` + "\u010f" + `"`,
2529
"ec": "1+2",
2630
"es2": `"es2"`,
31+
"esf": `importstr @"e""sf.txt"`,
32+
"ecf": `import @"e""cf.json"`,
2733
}, ext)
2834
tla := parseTLA()
2935
assert.Equal(t, map[string]string{
3036
"ts": `"2a \" ` + "\U0001f605" + ` ' b\nc` + "\u010f" + `"`,
3137
"tc": "2+3",
3238
"ts2": `"ts2"`,
39+
"tsf": `importstr @"t""s""f.txt"`,
40+
"tcf": `import @"t""c""f.json"`,
3341
}, tla)
3442
}

0 commit comments

Comments
 (0)