mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
wip
This commit is contained in:
@@ -7,14 +7,9 @@ import (
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type WriteCommand struct {
|
||||
// Command string TODO
|
||||
Value yaml.Node
|
||||
}
|
||||
|
||||
type DataNavigator interface {
|
||||
Get(rootNode *yaml.Node, remainingPath []string) (*yaml.Node, error)
|
||||
Update(rootNode *yaml.Node, remainingPath []string, writeCommand WriteCommand) error
|
||||
Update(rootNode *yaml.Node, remainingPath []string, changesToApply yaml.Node) error
|
||||
}
|
||||
|
||||
type navigator struct {
|
||||
@@ -33,7 +28,7 @@ func (n *navigator) Get(value *yaml.Node, path []string) (*yaml.Node, error) {
|
||||
realValue = value.Content[0]
|
||||
}
|
||||
if len(path) > 0 {
|
||||
n.log.Debug("diving into %v", path[0])
|
||||
n.log.Debugf("diving into %v", path[0])
|
||||
return n.recurse(realValue, path[0], path[1:])
|
||||
}
|
||||
return realValue, nil
|
||||
@@ -117,15 +112,11 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string) (*yaml
|
||||
}
|
||||
}
|
||||
|
||||
func (n *navigator) Update(dataBucket *yaml.Node, remainingPath []string, writeCommand WriteCommand) error {
|
||||
func (n *navigator) Update(dataBucket *yaml.Node, remainingPath []string, changesToApply yaml.Node) error {
|
||||
nodeToUpdate, errorRecursing := n.Get(dataBucket, remainingPath)
|
||||
if errorRecursing != nil {
|
||||
return errorRecursing
|
||||
}
|
||||
// later, support ability to execute other commands
|
||||
|
||||
changesToApply := writeCommand.Value
|
||||
|
||||
nodeToUpdate.Value = changesToApply.Value
|
||||
nodeToUpdate.Tag = changesToApply.Tag
|
||||
nodeToUpdate.Kind = changesToApply.Kind
|
||||
|
||||
@@ -1,24 +1,34 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
logging "gopkg.in/op/go-logging.v1"
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type UpdateCommand struct {
|
||||
Command string
|
||||
Path string
|
||||
Value yaml.Node
|
||||
}
|
||||
|
||||
type YqLib interface {
|
||||
Get(rootNode *yaml.Node, path string) (*yaml.Node, error)
|
||||
Update(rootNode *yaml.Node, path string, writeCommand WriteCommand) error
|
||||
Update(rootNode *yaml.Node, updateCommand UpdateCommand) error
|
||||
}
|
||||
|
||||
type lib struct {
|
||||
navigator DataNavigator
|
||||
parser PathParser
|
||||
log *logging.Logger
|
||||
}
|
||||
|
||||
func NewYqLib(l *logging.Logger) YqLib {
|
||||
return &lib{
|
||||
navigator: NewDataNavigator(l),
|
||||
parser: NewPathParser(),
|
||||
log: l,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +40,18 @@ func (l *lib) Get(rootNode *yaml.Node, path string) (*yaml.Node, error) {
|
||||
return l.navigator.Get(rootNode, paths)
|
||||
}
|
||||
|
||||
func (l *lib) Update(rootNode *yaml.Node, path string, writeCommand WriteCommand) error {
|
||||
var paths = l.parser.ParsePath(path)
|
||||
return l.navigator.Update(rootNode, paths, writeCommand)
|
||||
func (l *lib) Update(rootNode *yaml.Node, updateCommand UpdateCommand) error {
|
||||
// later - support other command types
|
||||
l.log.Debugf("%v to %v", updateCommand.Command, updateCommand.Path)
|
||||
switch updateCommand.Command {
|
||||
case "update":
|
||||
var paths = l.parser.ParsePath(updateCommand.Path)
|
||||
return l.navigator.Update(rootNode, paths, updateCommand.Value)
|
||||
case "delete":
|
||||
l.log.Debugf("need to implement delete")
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("Unknown command %v", updateCommand.Command)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user