From 41c5f0102096d948e7e15902cd38248344153270 Mon Sep 17 00:00:00 2001 From: Thomas Hahn <Thahn@tcbtech.com> Date: Thu, 29 Jun 2017 00:02:16 -0700 Subject: [PATCH] Top level arguments. Not yet implemented in go-jsonnet --- cmd/root.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index ef08eb86..38b5c88f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,6 +27,8 @@ const ( flagJpath = "jpath" flagExtVar = "ext-str" flagExtVarFile = "ext-str-file" + flagTlaVar = "tla-str" + flagTlaVarFile = "tla-str-file" flagResolver = "resolve-images" flagResolvFail = "resolve-images-error" ) @@ -37,6 +39,8 @@ func init() { RootCmd.PersistentFlags().StringP(flagJpath, "J", "", "Additional jsonnet library search path") RootCmd.PersistentFlags().StringSliceP(flagExtVar, "V", nil, "Values of external variables") RootCmd.PersistentFlags().StringSlice(flagExtVarFile, nil, "Read external variable from a file") + RootCmd.PersistentFlags().StringSliceP(flagTlaVar, "A", nil, "Values of top level arguments") + RootCmd.PersistentFlags().StringSlice(flagTlaVarFile, nil, "Read top level argument from a file") RootCmd.PersistentFlags().String(flagResolver, "noop", "Change implementation of resolveImage native function. One of: noop, registry") RootCmd.PersistentFlags().String(flagResolvFail, "warn", "Action when resolveImage fails. One of ignore,warn,error") @@ -122,6 +126,41 @@ func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) { vm.ExtVar(kv[0], string(v)) } + tlavars, err := flags.GetStringSlice(flagTlaVar) + if err != nil { + return nil, err + } + for _, tlavar := range tlavars { + kv := strings.SplitN(tlavar, "=", 2) + switch len(kv) { + case 1: + v, present := os.LookupEnv(kv[0]) + if present { + vm.TlaVar(kv[0], v) + } else { + return nil, fmt.Errorf("Missing environment variable: %s", kv[0]) + } + case 2: + vm.TlaVar(kv[0], kv[1]) + } + } + + tlavarfiles, err := flags.GetStringSlice(flagTlaVarFile) + if err != nil { + return nil, err + } + for _, tlavar := range tlavarfiles { + kv := strings.SplitN(tlavar, "=", 2) + if len(kv) != 2 { + return nil, fmt.Errorf("Failed to parse tla-str-file: missing '=' in %s", tlavar) + } + v, err := ioutil.ReadFile(kv[1]) + if err != nil { + return nil, err + } + vm.TlaVar(kv[0], string(v)) + } + resolver, err := buildResolver(cmd) if err != nil { return nil, err -- GitLab