mirror of
https://github.com/mikefarah/yq.git
synced 2026-06-27 15:37:47 +00:00
* Initial plan * Replace gopkg.in/op/go-logging.v1 with log/slog Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com> Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/aa9c12f4-21b9-4633-9868-6b56585b247f --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"container/list"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
parse "github.com/a8m/envsubst/parse"
|
|
)
|
|
|
|
type envOpPreferences struct {
|
|
StringValue bool
|
|
NoUnset bool
|
|
NoEmpty bool
|
|
FailFast bool
|
|
}
|
|
|
|
func envOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
if ConfiguredSecurityPreferences.DisableEnvOps {
|
|
return Context{}, fmt.Errorf("env operations have been disabled")
|
|
}
|
|
envName := expressionNode.Operation.CandidateNode.Value
|
|
log.Debugf("EnvOperator, env name: %v", envName)
|
|
|
|
rawValue := os.Getenv(envName)
|
|
|
|
preferences := expressionNode.Operation.Preferences.(envOpPreferences)
|
|
|
|
var node *CandidateNode
|
|
if preferences.StringValue {
|
|
node = &CandidateNode{
|
|
Kind: ScalarNode,
|
|
Tag: "!!str",
|
|
Value: rawValue,
|
|
}
|
|
} else if rawValue == "" {
|
|
return Context{}, fmt.Errorf("value for env variable '%v' not provided in env()", envName)
|
|
} else {
|
|
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
|
|
if err := decoder.Init(strings.NewReader(rawValue)); err != nil {
|
|
return Context{}, err
|
|
}
|
|
var err error
|
|
node, err = decoder.Decode()
|
|
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
|
|
}
|
|
log.Debugf("ENV tag: %v", node.Tag)
|
|
log.Debugf("ENV value: %v", node.Value)
|
|
log.Debugf("ENV Kind: %v", node.Kind)
|
|
|
|
return context.SingleChildContext(node), nil
|
|
}
|
|
|
|
func envsubstOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
if ConfiguredSecurityPreferences.DisableEnvOps {
|
|
return Context{}, fmt.Errorf("env operations have been disabled")
|
|
}
|
|
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() {
|
|
node := el.Value.(*CandidateNode)
|
|
if node.Tag != "!!str" {
|
|
log.Warningf("EnvSubstOperator, env name: %v %v", node.Tag, node.Value)
|
|
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 := parser.Parse(node.Value)
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
result := node.CreateReplacement(ScalarNode, "!!str", value)
|
|
results.PushBack(result)
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
}
|