Envsubst params (#1147)

* Can give envsubst optional arguments
This commit is contained in:
Mike Farah
2022-03-20 10:56:50 +11:00
committed by GitHub
parent 1a964c5055
commit 78e9cc7998
7 changed files with 238 additions and 15 deletions
+18 -2
View File
@@ -6,12 +6,15 @@ import (
"os"
"strings"
envsubst "github.com/a8m/envsubst"
parse "github.com/a8m/envsubst/parse"
yaml "gopkg.in/yaml.v3"
)
type envOpPreferences struct {
StringValue bool
NoUnset bool
NoEmpty bool
FailFast bool
}
func envOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
@@ -52,6 +55,19 @@ func envOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
func envsubstOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
var results = list.New()
preferences := envOpPreferences{}
if expressionNode.Operation.Preferences != nil {
preferences = expressionNode.Operation.Preferences.(envOpPreferences)
}
parser := parse.New("string", os.Environ(),
&parse.Restrictions{NoUnset: preferences.NoUnset, NoEmpty: preferences.NoEmpty})
if preferences.FailFast {
parser.Mode = parse.Quick
} else {
parser.Mode = parse.AllErrors
}
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
@@ -61,7 +77,7 @@ func envsubstOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
return Context{}, fmt.Errorf("cannot substitute with %v, can only substitute strings. Hint: Most often you'll want to use '|=' over '=' for this operation", node.Tag)
}
value, err := envsubst.String(node.Value)
value, err := parser.Parse(node.Value)
if err != nil {
return Context{}, err
}