Skip to content

Commit

Permalink
Add get command and Get function for string input
Browse files Browse the repository at this point in the history
Resolves #100

Signed-off-by: Yusuke Kuoka <[email protected]>
  • Loading branch information
mumoshu committed Feb 4, 2023
1 parent 86bccbe commit 6111a73
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ Usage:
vals [command]
Available Commands:
eval Evaluate a JSON/YAML document and replace any template expressions in it and prints the result
exec Populates the environment variables and executes the command
env Renders environment variables to be consumed by eval or a tool like direnv
ksdecode Decode YAML document(s) by converting Secret resources' "data" to "stringData" for use with "vals eval"
Use "vals [command] --help" for more information about a command
eval Evaluate a JSON/YAML document and replace any template expressions in it and prints the result
exec Populates the environment variables and executes the command
env Renders environment variables to be consumed by eval or a tool like direnv
get Evaluate a string value passed as the first argument and replace any expressiosn in it and prints the result
ksdecode Decode YAML document(s) by converting Secret resources' "data" to "stringData" for use with "vals eval"
version Print vals version
Use "vals [command] --help" for more information about a comman
```

`vals` has a collection of providers that each an be referred with a URI scheme looks `ref+<TYPE>`.
Expand Down
18 changes: 18 additions & 0 deletions cmd/vals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Available Commands:
eval Evaluate a JSON/YAML document and replace any template expressions in it and prints the result
exec Populates the environment variables and executes the command
env Renders environment variables to be consumed by eval or a tool like direnv
get Evaluate a string value passed as the first argument and replace any expressiosn in it and prints the result
ksdecode Decode YAML document(s) by converting Secret resources' "data" to "stringData" for use with "vals eval"
version Print vals version
Expand Down Expand Up @@ -72,6 +73,7 @@ func main() {
flag.Usage = flagUsage

CmdEval := "eval"
CmdGet := "get"
CmdExec := "exec"
CmdEnv := "env"
CmdKsDecode := "ksdecode"
Expand Down Expand Up @@ -112,6 +114,22 @@ func main() {
}

writeOrFail(o, res)
case CmdGet:
getCmd := flag.NewFlagSet(CmdGet, flag.ExitOnError)
getCmd.BoolVar(&log.Silent, "s", false, "Silent mode")
getCmd.Parse(os.Args[2:])

code := getCmd.Arg(0)
if code == "" {
fatal("The first argument of the get command is required")
}

v, err := vals.Get(code)
if err != nil {
fatal("%v", err)
}

os.Stdout.WriteString(v)
case CmdExec:
execCmd := flag.NewFlagSet(CmdExec, flag.ExitOnError)
f := execCmd.String("f", "", "YAML/JSON file to be loaded to set envvars")
Expand Down
35 changes: 33 additions & 2 deletions vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ func New(opts Options) (*Runtime, error) {
return r, nil
}

// Eval replaces 'ref+<provider>://xxxxx' entries by their actual values
func (r *Runtime) Eval(template map[string]interface{}) (map[string]interface{}, error) {
func (r *Runtime) prepare() (*expansion.ExpandRegexMatch, error) {
var err error

uriToProviderHash := func(uri *url.URL) string {
Expand Down Expand Up @@ -360,6 +359,16 @@ func (r *Runtime) Eval(template map[string]interface{}) (map[string]interface{},
},
}

return &expand, nil
}

// Eval replaces 'ref+<provider>://xxxxx' entries by their actual values
func (r *Runtime) Eval(template map[string]interface{}) (map[string]interface{}, error) {
expand, err := r.prepare()
if err != nil {
return nil, err
}

ret, err := expand.InMap(template)
if err != nil {
return nil, err
Expand All @@ -368,6 +377,20 @@ func (r *Runtime) Eval(template map[string]interface{}) (map[string]interface{},
return ret, nil
}

// Get replaces every occurrence of 'ref+<provider>://xxxxx' within a string with the fetched value
func (r *Runtime) Get(code string) (string, error) {
expand, err := r.prepare()
if err != nil {
return "", err
}

ret, err := expand.InString(code)
if err != nil {
return "", err
}

return ret, nil
}
func cloneMap(m map[string]interface{}) map[string]interface{} {
bs, err := yaml.Marshal(m)
if err != nil {
Expand Down Expand Up @@ -456,6 +479,14 @@ func Eval(template map[string]interface{}, o ...Options) (map[string]interface{}
return runtime.Eval(template)
}

func Get(code string) (string, error) {
runtime, err := New(Options{})
if err != nil {
return "", err
}
return runtime.Get(code)
}

func Load(conf api.StaticConfig, opt ...Option) (map[string]interface{}, error) {
ctx := &ctx{}
for _, o := range opt {
Expand Down

0 comments on commit 6111a73

Please sign in to comment.