mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
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, 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,
|
|
}
|
|
}
|
|
|
|
func (l *lib) Get(rootNode *yaml.Node, path string) (*yaml.Node, error) {
|
|
if path == "" {
|
|
return rootNode, nil
|
|
}
|
|
var paths = l.parser.ParsePath(path)
|
|
return l.navigator.Get(rootNode, paths)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|