yq/pkg/yqlib/operator_env.go

53 lines
1.1 KiB
Go
Raw Normal View History

2021-01-08 10:07:46 +00:00
package yqlib
import (
"container/list"
"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-09 01:06:19 +00:00
type EnvOpPreferences struct {
StringValue bool
}
2021-01-08 10:07:46 +00:00
func EnvOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
envName := pathNode.Operation.CandidateNode.Node.Value
log.Debug("EnvOperator, env name:", envName)
rawValue := os.Getenv(envName)
2021-01-09 01:06:19 +00:00
preferences := pathNode.Operation.Preferences.(*EnvOpPreferences)
var node *yaml.Node
if preferences.StringValue {
node = &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: rawValue,
}
} else {
var dataBucket yaml.Node
decoder := yaml.NewDecoder(strings.NewReader(rawValue))
errorReading := decoder.Decode(&dataBucket)
if errorReading != nil {
return nil, errorReading
}
//first node is a doc
node = UnwrapDoc(&dataBucket)
}
log.Debug("ENV tag", node.Tag)
log.Debug("ENV value", node.Value)
log.Debug("ENV Kind", node.Kind)
2021-01-08 10:07:46 +00:00
target := &CandidateNode{
Path: make([]interface{}, 0),
Document: 0,
Filename: "",
2021-01-09 01:06:19 +00:00
Node: node,
2021-01-08 10:07:46 +00:00
}
return nodeToMap(target), nil
}