yq/pkg/yqlib/lib.go

60 lines
1.3 KiB
Go
Raw Normal View History

package yqlib
import (
2019-12-06 05:36:42 +00:00
"fmt"
logging "gopkg.in/op/go-logging.v1"
2019-12-06 04:57:46 +00:00
yaml "gopkg.in/yaml.v3"
)
2019-12-06 05:36:42 +00:00
type UpdateCommand struct {
Command string
Path string
Value yaml.Node
}
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
}
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
}
func NewYqLib(l *logging.Logger) YqLib {
2019-12-01 20:10:42 +00:00
return &lib{
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-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) {
var paths = l.parser.ParsePath(path)
2019-12-06 04:57:46 +00:00
return l.navigator.Get(rootNode, paths)
}
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
}