2019-11-23 03:52:29 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2019-12-06 05:36:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
2019-12-06 04:57:46 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2019-11-23 03:52:29 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 05:36:42 +00:00
|
|
|
type UpdateCommand struct {
|
|
|
|
Command string
|
|
|
|
Path string
|
|
|
|
Value yaml.Node
|
|
|
|
}
|
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
type YqLib interface {
|
2019-12-09 02:44:53 +00:00
|
|
|
DebugNode(node *yaml.Node)
|
2019-12-06 04:57:46 +00:00
|
|
|
Get(rootNode *yaml.Node, path string) (*yaml.Node, error)
|
2019-12-06 05:36:42 +00:00
|
|
|
Update(rootNode *yaml.Node, updateCommand UpdateCommand) error
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lib struct {
|
|
|
|
navigator DataNavigator
|
2019-12-01 20:10:42 +00:00
|
|
|
parser PathParser
|
2019-12-06 05:36:42 +00:00
|
|
|
log *logging.Logger
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
2019-11-23 03:52:29 +00:00
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
func NewYqLib(l *logging.Logger) YqLib {
|
2019-12-01 20:10:42 +00:00
|
|
|
return &lib{
|
2019-12-01 19:44:44 +00:00
|
|
|
navigator: NewDataNavigator(l),
|
2019-12-01 20:10:42 +00:00
|
|
|
parser: NewPathParser(),
|
2019-12-06 05:36:42 +00:00
|
|
|
log: l,
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
2019-11-23 03:52:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 02:44:53 +00:00
|
|
|
func (l *lib) DebugNode(node *yaml.Node) {
|
|
|
|
l.navigator.DebugNode(node)
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
func (l *lib) Get(rootNode *yaml.Node, path string) (*yaml.Node, error) {
|
2019-12-01 19:44:44 +00:00
|
|
|
var paths = l.parser.ParsePath(path)
|
2019-12-06 04:57:46 +00:00
|
|
|
return l.navigator.Get(rootNode, paths)
|
2019-11-23 03:52:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 05:36:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-01 20:10:42 +00:00
|
|
|
}
|