yq/yq.go

553 lines
15 KiB
Go
Raw Normal View History

2015-09-26 22:15:49 +00:00
package main
import (
2018-06-12 05:33:59 +00:00
"bufio"
2015-09-28 02:00:38 +00:00
"fmt"
2018-06-12 05:33:59 +00:00
"io"
2015-09-28 02:00:38 +00:00
"io/ioutil"
"os"
"reflect"
2015-09-28 02:00:38 +00:00
"strconv"
"strings"
2018-06-15 06:11:13 +00:00
errors "github.com/pkg/errors"
logging "github.com/op/go-logging"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
2015-09-26 22:15:49 +00:00
)
2015-10-06 05:07:09 +00:00
var trimOutput = true
var writeInplace = false
var writeScript = ""
2015-10-10 23:00:22 +00:00
var outputToJSON = false
var overwriteFlag = false
2017-04-11 23:16:54 +00:00
var verbose = false
var version = false
2018-06-12 05:33:59 +00:00
var docIndex = 0
2017-12-17 22:11:08 +00:00
var log = logging.MustGetLogger("yq")
2015-10-06 05:07:09 +00:00
2015-09-26 22:15:49 +00:00
func main() {
cmd := newCommandCLI()
if err := cmd.Execute(); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
2017-04-11 23:16:54 +00:00
func newCommandCLI() *cobra.Command {
yaml.DefaultMapType = reflect.TypeOf(yaml.MapSlice{})
var rootCmd = &cobra.Command{
2017-12-17 22:11:08 +00:00
Use: "yq",
RunE: func(cmd *cobra.Command, args []string) error {
if version {
cmd.Print(GetVersionDisplay())
return nil
}
cmd.Println(cmd.UsageString())
return nil
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05} %{shortfunc} [%{level:.4s}]%{color:reset} %{message}`,
)
var backend = logging.AddModuleLevel(
logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), format))
if verbose {
backend.SetLevel(logging.DEBUG, "")
} else {
backend.SetLevel(logging.ERROR, "")
}
logging.SetBackend(backend)
},
}
2015-10-13 10:42:36 +00:00
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
2017-04-11 23:16:54 +00:00
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit")
rootCmd.AddCommand(
createReadCmd(),
createWriteCmd(),
createDeleteCmd(),
createNewCmd(),
createMergeCmd(),
)
rootCmd.SetOutput(os.Stdout)
return rootCmd
2015-10-13 10:42:36 +00:00
}
func createReadCmd() *cobra.Command {
2018-06-12 05:33:59 +00:00
var cmdRead = &cobra.Command{
2015-10-06 05:07:09 +00:00
Use: "read [yaml_file] [path]",
Aliases: []string{"r"},
2018-06-12 05:33:59 +00:00
Short: "yq r [--doc/-d document_index] sample.yaml a.b.c",
2015-10-06 05:07:09 +00:00
Example: `
2017-12-17 22:11:08 +00:00
yq read things.yaml a.b.c
yq r - a.b.c (reads from stdin)
yq r things.yaml a.*.c
2018-06-12 23:13:58 +00:00
yq r -d1 things.yaml a.array[0].blah
2017-12-17 22:11:08 +00:00
yq r things.yaml a.array[*].blah
2015-10-13 10:42:36 +00:00
`,
2015-10-06 05:07:09 +00:00
Long: "Outputs the value of the given path in the yaml file to STDOUT",
RunE: readProperty,
2015-09-28 02:00:38 +00:00
}
2018-06-12 05:33:59 +00:00
cmdRead.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
return cmdRead
2015-10-13 10:42:36 +00:00
}
2015-09-26 23:38:44 +00:00
2015-10-13 10:42:36 +00:00
func createWriteCmd() *cobra.Command {
2015-10-06 05:07:09 +00:00
var cmdWrite = &cobra.Command{
Use: "write [yaml_file] [path] [value]",
Aliases: []string{"w"},
Short: "yq w [--inplace/-i] [--script/-s script_file] [--doc/-d document_index] sample.yaml a.b.c newValueForC",
2015-10-06 05:07:09 +00:00
Example: `
2017-12-17 22:11:08 +00:00
yq write things.yaml a.b.c cat
yq write --inplace things.yaml a.b.c cat
yq w -i things.yaml a.b.c cat
yq w --script update_script.yaml things.yaml
yq w -i -s update_script.yaml things.yaml
yq w --doc 2 things.yaml a.b.d[+] foo
yq w -d2 things.yaml a.b.d[+] foo
2015-10-13 10:42:36 +00:00
`,
2015-10-06 05:07:09 +00:00
Long: `Updates the yaml file w.r.t the given path and value.
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
Append value to array adds the value to the end of array.
Update Scripts:
Note that you can give an update script to perform more sophisticated updated. Update script
format is a yaml map where the key is the path and the value is..well the value. e.g.:
---
a.b.c: true,
a.b.e:
- name: bob
`,
RunE: writeProperty,
2015-10-06 05:07:09 +00:00
}
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
cmdWrite.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
2015-10-13 10:42:36 +00:00
return cmdWrite
2015-10-06 05:07:09 +00:00
}
2015-09-29 06:29:32 +00:00
func createDeleteCmd() *cobra.Command {
var cmdDelete = &cobra.Command{
Use: "delete [yaml_file] [path]",
Aliases: []string{"d"},
Short: "yq d [--inplace/-i] sample.yaml a.b.c",
Example: `
yq delete things.yaml a.b.c
yq delete --inplace things.yaml a.b.c
yq d -i things.yaml a.b.c
yq d things.yaml a.b.c
`,
Long: `Deletes the given path from the YAML file.
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
`,
RunE: deleteProperty,
}
cmdDelete.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
2018-06-14 23:43:20 +00:00
cmdDelete.PersistentFlags().IntVarP(&docIndex, "doc", "d", 0, "process document index number (0 based)")
return cmdDelete
}
func createNewCmd() *cobra.Command {
var cmdNew = &cobra.Command{
Use: "new [path] [value]",
Aliases: []string{"n"},
2017-12-17 22:11:08 +00:00
Short: "yq n [--script/-s script_file] a.b.c newValueForC",
Example: `
2017-12-17 22:11:08 +00:00
yq new a.b.c cat
yq n a.b.c cat
yq n --script create_script.yaml
`,
Long: `Creates a new yaml w.r.t the given path and value.
Outputs to STDOUT
Create Scripts:
Note that you can give a create script to perform more sophisticated yaml. This follows the same format as the update script.
`,
RunE: newProperty,
}
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
return cmdNew
}
func createMergeCmd() *cobra.Command {
var cmdMerge = &cobra.Command{
Use: "merge [initial_yaml_file] [additional_yaml_file]...",
Aliases: []string{"m"},
2017-12-17 22:11:08 +00:00
Short: "yq m [--inplace/-i] [--overwrite/-x] sample.yaml sample2.yaml",
Example: `
2017-12-17 22:11:08 +00:00
yq merge things.yaml other.yaml
yq merge --inplace things.yaml other.yaml
yq m -i things.yaml other.yaml
yq m --overwrite things.yaml other.yaml
yq m -i -x things.yaml other.yaml
`,
Long: `Updates the yaml file by adding/updating the path(s) and value(s) from additional yaml file(s).
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
If overwrite flag is set then existing values will be overwritten using the values from each additional yaml file.
`,
RunE: mergeProperties,
}
cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values")
return cmdMerge
}
func readProperty(cmd *cobra.Command, args []string) error {
data, err := read(args)
if err != nil {
return err
}
dataStr, err := toString(data)
if err != nil {
return err
2017-04-11 23:16:54 +00:00
}
cmd.Println(dataStr)
return nil
}
func read(args []string) (interface{}, error) {
2017-08-03 07:30:07 +00:00
var path = ""
if len(args) < 1 {
return nil, errors.New("Must provide filename")
} else if len(args) > 1 {
2017-08-03 07:30:07 +00:00
path = args[1]
}
var generalData interface{}
if err := readData(args[0], docIndex, &generalData); err != nil {
return nil, err
}
2017-08-03 07:30:07 +00:00
if path == "" {
return generalData, nil
2015-09-29 06:29:32 +00:00
}
2017-08-03 07:30:07 +00:00
var paths = parsePath(path)
value, err := recurse(generalData, paths[0], paths[1:])
return value, err
}
func newProperty(cmd *cobra.Command, args []string) error {
updatedData, err := newYaml(args)
if err != nil {
return err
}
dataStr, err := toString(updatedData)
if err != nil {
return err
}
cmd.Println(dataStr)
return nil
}
func newYaml(args []string) (interface{}, error) {
2018-06-12 23:13:58 +00:00
var writeCommands, writeCommandsError = readWriteCommands(args, 2, "Must provide <path_to_update> <value>")
if writeCommandsError != nil {
return nil, writeCommandsError
}
2017-08-08 07:04:30 +00:00
var parsedData yaml.MapSlice
var prependCommand = ""
var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[")
if isArray {
item := yaml.MapItem{Key: "thing", Value: make(yaml.MapSlice, 0)}
parsedData = yaml.MapSlice{item}
prependCommand = "thing"
} else {
parsedData = make(yaml.MapSlice, 0)
}
2017-08-08 07:04:30 +00:00
return updateParsedData(parsedData, writeCommands, prependCommand)
}
2018-06-14 23:43:20 +00:00
type updateDataFn func(dataBucket interface{}, currentIndex int) interface{}
func mapYamlDecoder(updateData updateDataFn, encoder *yaml.Encoder) yamlDecoderFn {
return func(decoder *yaml.Decoder) error {
var dataBucket interface{}
var errorReading error
var errorWriting error
var currentIndex = 0
for {
log.Debugf("Read doc %v", currentIndex)
errorReading = decoder.Decode(&dataBucket)
if errorReading == io.EOF {
if currentIndex < docIndex {
return fmt.Errorf("Asked to process document %v but there are only %v document(s)", docIndex, currentIndex)
}
return nil
} else if errorReading != nil {
2018-06-15 06:11:13 +00:00
return errors.Wrapf(errorReading, "Error reading document at index %v, %v", currentIndex, errorReading)
}
2018-06-14 23:43:20 +00:00
dataBucket = updateData(dataBucket, currentIndex)
errorWriting = encoder.Encode(dataBucket)
if errorWriting != nil {
2018-06-15 06:11:13 +00:00
return errors.Wrapf(errorWriting, "Error writing document at index %v, %v", currentIndex, errorWriting)
}
currentIndex = currentIndex + 1
}
}
}
func writeProperty(cmd *cobra.Command, args []string) error {
var writeCommands, writeCommandsError = readWriteCommands(args, 3, "Must provide <filename> <path_to_update> <value>")
if writeCommandsError != nil {
return writeCommandsError
2017-04-11 23:16:54 +00:00
}
2018-06-14 23:43:20 +00:00
var updateData = func(dataBucket interface{}, currentIndex int) interface{} {
if currentIndex == docIndex {
log.Debugf("Updating doc %v", currentIndex)
for _, entry := range writeCommands {
path := entry.Key.(string)
value := entry.Value
log.Debugf("setting %v to %v", path, value)
var paths = parsePath(path)
dataBucket = updatedChildValue(dataBucket, paths, value)
}
}
return dataBucket
}
return readAndUpdate(cmd.OutOrStdout(), args[0], updateData)
}
func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) error {
var destination io.Writer
var destinationName string
if writeInplace {
var tempFile, err = ioutil.TempFile("", "temp")
if err != nil {
return err
}
destinationName = tempFile.Name()
destination = tempFile
defer func() {
safelyCloseFile(tempFile)
safelyRenameFile(tempFile.Name(), inputFile)
}()
} else {
2018-06-14 23:43:20 +00:00
var writer = bufio.NewWriter(stdOut)
destination = writer
destinationName = "Stdout"
defer safelyFlush(writer)
}
var encoder = yaml.NewEncoder(destination)
log.Debugf("Writing to %v from %v", destinationName, inputFile)
2018-06-14 23:43:20 +00:00
return readStream(inputFile, mapYamlDecoder(updateData, encoder))
}
func write(cmd *cobra.Command, filename string, updatedData interface{}) error {
if writeInplace {
dataStr, err := yamlToString(updatedData)
if err != nil {
return err
}
return ioutil.WriteFile(filename, []byte(dataStr), 0644)
}
dataStr, err := toString(updatedData)
if err != nil {
return err
}
cmd.Println(dataStr)
return nil
}
func deleteProperty(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
2018-06-14 23:43:20 +00:00
return errors.New("Must provide <filename> <path_to_delete>")
}
2018-06-14 23:43:20 +00:00
var deletePath = args[1]
var paths = parsePath(deletePath)
var updateData = func(dataBucket interface{}, currentIndex int) interface{} {
if currentIndex == docIndex {
log.Debugf("Updating doc %v", currentIndex)
return deleteChildValue(dataBucket, paths)
}
return dataBucket
}
2018-06-14 23:43:20 +00:00
return readAndUpdate(cmd.OutOrStdout(), args[0], updateData)
}
func mergeProperties(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("Must provide at least 2 yaml files")
}
updatedData, err := mergeYaml(args)
if err != nil {
return err
}
return write(cmd, args[0], updatedData)
}
func mergeYaml(args []string) (interface{}, error) {
var updatedData map[interface{}]interface{}
for _, f := range args {
var parsedData map[interface{}]interface{}
if err := readData(f, 0, &parsedData); err != nil {
return nil, err
}
if err := merge(&updatedData, parsedData, overwriteFlag); err != nil {
return nil, err
}
}
return mapToMapSlice(updatedData), nil
}
func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice, prependCommand string) (interface{}, error) {
2017-08-08 06:55:57 +00:00
var prefix = ""
if prependCommand != "" {
prefix = prependCommand + "."
}
for _, entry := range writeCommands {
2017-08-08 06:55:57 +00:00
path := prefix + entry.Key.(string)
value := entry.Value
2017-08-08 06:55:57 +00:00
var paths = parsePath(path)
parsedData = writeMap(parsedData, paths, value)
}
2017-08-08 06:55:57 +00:00
if prependCommand != "" {
return readMap(parsedData, prependCommand, make([]string, 0))
}
return parsedData, nil
}
2018-06-12 23:13:58 +00:00
func readWriteCommands(args []string, expectedArgs int, badArgsMessage string) (yaml.MapSlice, error) {
var writeCommands yaml.MapSlice
if writeScript != "" {
if err := readData(writeScript, 0, &writeCommands); err != nil {
return nil, err
}
2018-06-12 23:13:58 +00:00
} else if len(args) < expectedArgs {
return nil, errors.New(badArgsMessage)
} else {
writeCommands = make(yaml.MapSlice, 1)
2018-06-12 23:13:58 +00:00
writeCommands[0] = yaml.MapItem{Key: args[expectedArgs-2], Value: parseValue(args[expectedArgs-1])}
2015-09-29 06:29:32 +00:00
}
2018-06-12 23:13:58 +00:00
return writeCommands, nil
}
2015-09-29 06:29:32 +00:00
func parseValue(argument string) interface{} {
var value, err interface{}
var inQuotes = len(argument) > 0 && argument[0] == '"'
2015-10-03 07:25:13 +00:00
if !inQuotes {
value, err = strconv.ParseFloat(argument, 64)
if err == nil {
return value
}
value, err = strconv.ParseBool(argument)
if err == nil {
return value
}
if argument == "[]" {
return make([]interface{}, 0)
}
2015-10-03 07:25:13 +00:00
return argument
}
2015-10-03 07:25:13 +00:00
return argument[1 : len(argument)-1]
}
func toString(context interface{}) (string, error) {
2015-10-10 23:00:22 +00:00
if outputToJSON {
return jsonToString(context)
2015-10-10 23:00:22 +00:00
}
return yamlToString(context)
}
func yamlToString(context interface{}) (string, error) {
switch context.(type) {
case string:
return context.(string), nil
default:
return marshalContext(context)
}
}
func marshalContext(context interface{}) (string, error) {
2015-09-29 01:05:28 +00:00
out, err := yaml.Marshal(context)
2015-09-29 01:05:28 +00:00
if err != nil {
2018-06-15 06:11:13 +00:00
return "", errors.Wrap(err, "error printing yaml")
2015-09-29 01:05:28 +00:00
}
2015-09-29 06:29:32 +00:00
outStr := string(out)
// trim the trailing new line as it's easier for a script to add
// it in if required than to remove it
2015-10-06 05:07:09 +00:00
if trimOutput {
return strings.Trim(outStr, "\n "), nil
2017-08-03 07:30:07 +00:00
}
return outStr, nil
2017-08-03 07:30:07 +00:00
}
func safelyRenameFile(from string, to string) {
if err := os.Rename(from, to); err != nil {
log.Errorf("Error renaming from %v to %v", from, to)
log.Error(err.Error())
}
}
func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil {
log.Error("Error flushing writer!")
log.Error(err.Error())
}
}
2018-06-12 05:33:59 +00:00
func safelyCloseFile(file *os.File) {
err := file.Close()
if err != nil {
log.Error("Error closing file!")
log.Error(err.Error())
2018-06-12 05:33:59 +00:00
}
}
type yamlDecoderFn func(*yaml.Decoder) error
func readStream(filename string, yamlDecoder yamlDecoderFn) error {
if filename == "" {
return errors.New("Must provide filename")
2015-09-28 02:00:38 +00:00
}
2015-10-05 04:48:34 +00:00
2018-06-12 05:33:59 +00:00
var stream io.Reader
if filename == "-" {
2018-06-12 05:33:59 +00:00
stream = bufio.NewReader(os.Stdin)
2015-10-05 04:48:34 +00:00
} else {
2018-06-12 05:33:59 +00:00
file, err := os.Open(filename)
if err != nil {
return err
}
defer safelyCloseFile(file)
stream = file
2015-10-05 04:48:34 +00:00
}
return yamlDecoder(yaml.NewDecoder(stream))
}
2015-09-26 22:20:42 +00:00
func readData(filename string, indexToRead int, parsedData interface{}) error {
return readStream(filename, func(decoder *yaml.Decoder) error {
// naive implementation of document indexing, decodes all the yaml documents
// before the docIndex and throws them away.
for currentIndex := 0; currentIndex < indexToRead; currentIndex++ {
errorSkipping := decoder.Decode(parsedData)
if errorSkipping != nil {
2018-06-15 06:11:13 +00:00
return errors.Wrapf(errorSkipping, "Error processing document at index %v, %v", currentIndex, errorSkipping)
}
2018-06-12 05:33:59 +00:00
}
return decoder.Decode(parsedData)
})
2015-10-06 05:39:19 +00:00
}