Skip to content

Commit

Permalink
feat: parse envars in reference path
Browse files Browse the repository at this point in the history
  • Loading branch information
TurbulentCupcake authored and geofffranks committed Sep 15, 2023
1 parent 3828850 commit d44852c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (e *Expr) Resolve(tree map[interface{}]interface{}) (*Expr, error) {
return &Expr{Type: Literal, Literal: val}, nil

case Reference:
e.Reference.Nodes = ResolveEnv(e.Reference.Nodes)
if _, err := e.Reference.Resolve(tree); err != nil {
return nil, ansi.Errorf("@R{Unable to resolve `}@c{%s}@R{`: %s}", e.Reference, err)
}
Expand All @@ -211,6 +212,18 @@ func (e *Expr) Resolve(tree map[interface{}]interface{}) (*Expr, error) {
return nil, ansi.Errorf("@R{unknown expression operand type (}@c{%d}@R{)}", e.Type)
}

func ResolveEnv(nodes []string) []string {
var resolved []string
for _, node := range nodes {
if len(node) > 0 && node[0] == '$' {
resolved = append(resolved, os.Getenv(node[1:]))
} else {
resolved = append(resolved, node)
}
}
return resolved
}

// Evaluate ...
func (e *Expr) Evaluate(tree map[interface{}]interface{}) (interface{}, error) {
final, err := e.Resolve(tree)
Expand Down
12 changes: 12 additions & 0 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ meta:
So(r.Value.(string), ShouldEqual, "found it")
})

Convey("can grab a single value using an environment variable in the reference", func() {
os.Setenv("SUB_KEY", "subkey")
r, err := op.Run(ev, []*Expr{
ref("key.$SUB_KEY.value"),
})
So(err, ShouldBeNil)
So(r, ShouldNotBeNil)

So(r.Type, ShouldEqual, Replace)
So(r.Value.(string), ShouldEqual, "found it")
})

Convey("can grab a single list value", func() {
r, err := op.Run(ev, []*Expr{
ref("key.lonely"),
Expand Down

0 comments on commit d44852c

Please sign in to comment.