mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-11 09:42:30 +00:00
wip - Parsing envsubst parameters
This commit is contained in:
parent
0ffee92175
commit
d2a2830393
@ -15,6 +15,26 @@ var pathTests = []struct {
|
|||||||
expectedTokens []interface{}
|
expectedTokens []interface{}
|
||||||
expectedPostFix []interface{}
|
expectedPostFix []interface{}
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
`envsubst(ne)`,
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`envsubst(nu)`,
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`envsubst(nu, ne)`,
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`envsubst(ne, nu)`,
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
append(make([]interface{}, 0), "ENVSUBST"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
`[.a, .b]`,
|
`[.a, .b]`,
|
||||||
append(make([]interface{}, 0), "[", "a", "UNION", "b", "]"),
|
append(make([]interface{}, 0), "[", "a", "UNION", "b", "]"),
|
||||||
|
|||||||
@ -507,6 +507,7 @@ func initLexer() (*lex.Lexer, error) {
|
|||||||
lexer.Add([]byte(`strenv\([^\)]+\)`), envOp(true))
|
lexer.Add([]byte(`strenv\([^\)]+\)`), envOp(true))
|
||||||
lexer.Add([]byte(`env\([^\)]+\)`), envOp(false))
|
lexer.Add([]byte(`env\([^\)]+\)`), envOp(false))
|
||||||
|
|
||||||
|
lexer.Add([]byte(`envsubst\((ne|nu| |,)+\)`), opToken(envsubstOpType))
|
||||||
lexer.Add([]byte(`envsubst`), opToken(envsubstOpType))
|
lexer.Add([]byte(`envsubst`), opToken(envsubstOpType))
|
||||||
|
|
||||||
lexer.Add([]byte(`\[`), literalToken(openCollect, false))
|
lexer.Add([]byte(`\[`), literalToken(openCollect, false))
|
||||||
|
|||||||
@ -12,6 +12,8 @@ import (
|
|||||||
|
|
||||||
type envOpPreferences struct {
|
type envOpPreferences struct {
|
||||||
StringValue bool
|
StringValue bool
|
||||||
|
NoUnset bool
|
||||||
|
NoEmpty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func envOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func envOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
@ -52,6 +54,7 @@ func envOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
|
|||||||
|
|
||||||
func envsubstOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func envsubstOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
var results = list.New()
|
var results = list.New()
|
||||||
|
preferences := expressionNode.Operation.Preferences.(envOpPreferences)
|
||||||
|
|
||||||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
candidate := el.Value.(*CandidateNode)
|
candidate := el.Value.(*CandidateNode)
|
||||||
@ -61,7 +64,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)
|
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 := envsubst.StringRestricted(node.Value, preferences.NoUnset, preferences.NoEmpty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,6 +88,29 @@ var envOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!str)::the meows\n",
|
"D0, P[], (!!str)::the meows\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Replace strings with envsubst(nu), missing variables",
|
||||||
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
|
expression: `"the ${myenvnonexisting} meows" | envsubst`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!str)::the meows\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Replace strings with envsubst(ne), empty variable",
|
||||||
|
environmentVariables: map[string]string{"myenv": ""},
|
||||||
|
expression: `"the ${myenv} meows" | envsubst`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!str)::the meows\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Replace strings with envsubst(ne), missing variable",
|
||||||
|
expression: `"the ${myenv} meows" | envsubst`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!str)::the meows\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Replace strings with envsubst, missing variables with defaults",
|
description: "Replace strings with envsubst, missing variables with defaults",
|
||||||
environmentVariables: map[string]string{"myenv": "cat"},
|
environmentVariables: map[string]string{"myenv": "cat"},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user