mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-05 09:04:58 +08:00
Env Ops!
This commit is contained in:
@@ -3,13 +3,14 @@ package yqlib
|
||||
import (
|
||||
"container/list"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// type EnvOpPreferences struct {
|
||||
// StringValue bool
|
||||
// }
|
||||
type EnvOpPreferences struct {
|
||||
StringValue bool
|
||||
}
|
||||
|
||||
func EnvOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
||||
envName := pathNode.Operation.CandidateNode.Node.Value
|
||||
@@ -17,15 +18,34 @@ func EnvOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNo
|
||||
|
||||
rawValue := os.Getenv(envName)
|
||||
|
||||
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)
|
||||
|
||||
target := &CandidateNode{
|
||||
Path: make([]interface{}, 0),
|
||||
Document: 0,
|
||||
Filename: "",
|
||||
Node: &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: rawValue,
|
||||
},
|
||||
Node: node,
|
||||
}
|
||||
|
||||
return nodeToMap(target), nil
|
||||
|
||||
Reference in New Issue
Block a user