2021-01-08 10:07:46 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2021-01-11 03:38:53 +00:00
|
|
|
"fmt"
|
2021-01-08 10:07:46 +00:00
|
|
|
"os"
|
2021-01-09 01:06:19 +00:00
|
|
|
"strings"
|
2021-01-08 10:07:46 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
type envOpPreferences struct {
|
2021-01-09 01:06:19 +00:00
|
|
|
StringValue bool
|
|
|
|
}
|
2021-01-08 10:07:46 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func envOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-01-12 23:18:53 +00:00
|
|
|
envName := expressionNode.Operation.CandidateNode.Node.Value
|
2021-01-08 10:07:46 +00:00
|
|
|
log.Debug("EnvOperator, env name:", envName)
|
|
|
|
|
|
|
|
rawValue := os.Getenv(envName)
|
|
|
|
|
2021-01-13 06:00:53 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(envOpPreferences)
|
2021-01-09 01:06:19 +00:00
|
|
|
|
|
|
|
var node *yaml.Node
|
|
|
|
if preferences.StringValue {
|
|
|
|
node = &yaml.Node{
|
|
|
|
Kind: yaml.ScalarNode,
|
|
|
|
Tag: "!!str",
|
|
|
|
Value: rawValue,
|
|
|
|
}
|
2021-01-11 03:38:53 +00:00
|
|
|
} else if rawValue == "" {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, fmt.Errorf("Value for env variable '%v' not provided in env()", envName)
|
2021-01-09 01:06:19 +00:00
|
|
|
} else {
|
|
|
|
var dataBucket yaml.Node
|
|
|
|
decoder := yaml.NewDecoder(strings.NewReader(rawValue))
|
|
|
|
errorReading := decoder.Decode(&dataBucket)
|
|
|
|
if errorReading != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, errorReading
|
2021-01-09 01:06:19 +00:00
|
|
|
}
|
|
|
|
//first node is a doc
|
2021-01-12 23:00:51 +00:00
|
|
|
node = unwrapDoc(&dataBucket)
|
2021-01-09 01:06:19 +00:00
|
|
|
}
|
|
|
|
log.Debug("ENV tag", node.Tag)
|
|
|
|
log.Debug("ENV value", node.Value)
|
|
|
|
log.Debug("ENV Kind", node.Kind)
|
|
|
|
|
2021-01-12 08:36:28 +00:00
|
|
|
target := &CandidateNode{Node: node}
|
2021-01-08 10:07:46 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.SingleChildContext(target), nil
|
2021-01-08 10:07:46 +00:00
|
|
|
}
|