yq/yaml.go

155 lines
3.5 KiB
Go
Raw Normal View History

2015-09-26 22:15:49 +00:00
package main
import (
2015-09-28 02:00:38 +00:00
"fmt"
2015-10-06 05:07:09 +00:00
"github.com/spf13/cobra"
2015-09-28 02:00:38 +00:00
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
2015-09-26 22:15:49 +00:00
)
2015-10-06 05:07:09 +00:00
var trimOutput = true
var writeInplace = false
2015-09-26 22:15:49 +00:00
func main() {
2015-10-06 05:07:09 +00:00
var cmdRead = &cobra.Command{
Use: "read [yaml_file] [path]",
Aliases: []string{"r"},
Short: "yaml r sample.yaml a.b.c",
Example: `
yaml read things.yaml a.b.c
yaml r - a.b.c (reads from stdin)
yaml r things.yaml a.*.c
yaml r things.yaml a.array[0].blah
yaml r things.yaml a.array[*].blah
`,
Long: "Outputs the value of the given path in the yaml file to STDOUT",
Run: readProperty,
2015-09-28 02:00:38 +00:00
}
2015-09-26 23:38:44 +00:00
2015-10-06 05:07:09 +00:00
var cmdWrite = &cobra.Command{
Use: "write [yaml_file] [path] [value]",
Aliases: []string{"w"},
Short: "yaml w [--inplace/-i] sample.yaml a.b.c newValueForC",
Example: `
yaml write things.yaml a.b.c cat
yaml write --inplace things.yaml a.b.c cat
yaml w -i things.yaml a.b.c cat
`,
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.`,
Run: writeProperty,
}
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
var rootCmd = &cobra.Command{Use: "yaml"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.AddCommand(cmdRead, cmdWrite)
rootCmd.Execute()
}
2015-09-29 06:29:32 +00:00
2015-10-06 05:07:09 +00:00
func readProperty(cmd *cobra.Command, args []string) {
var parsedData map[interface{}]interface{}
2015-09-29 06:29:32 +00:00
2015-10-06 05:07:09 +00:00
readYaml(args, &parsedData)
2015-09-27 03:18:33 +00:00
2015-10-06 05:07:09 +00:00
if len(args) == 1 {
printYaml(parsedData)
2015-09-29 06:29:32 +00:00
os.Exit(0)
}
2015-10-06 05:07:09 +00:00
var paths = parsePath(args[1])
2015-09-27 03:18:33 +00:00
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]))
2015-09-29 00:56:12 +00:00
}
2015-10-06 05:07:09 +00:00
func writeProperty(cmd *cobra.Command, args []string) {
if len(args) < 3 {
2015-09-29 06:29:32 +00:00
log.Fatalf("Must provide <filename> <path_to_update> <value>")
}
2015-10-06 05:07:09 +00:00
var parsedData map[interface{}]interface{}
readYaml(args, &parsedData)
2015-10-06 05:07:09 +00:00
var paths = parsePath(args[1])
2015-10-06 05:07:09 +00:00
write(parsedData, paths[0], paths[1:len(paths)], getValue(args[2]))
2015-10-06 05:07:09 +00:00
if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
} else {
printYaml(parsedData)
}
}
2015-10-03 07:25:13 +00:00
func getValue(argument string) interface{} {
var value, err interface{}
2015-10-03 07:25:13 +00:00
var inQuotes = argument[0] == '"'
if !inQuotes {
value, err = strconv.ParseFloat(argument, 64)
if err == nil {
return value
}
value, err = strconv.ParseBool(argument)
if err == nil {
return value
}
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 printYaml(context interface{}) {
fmt.Println(yamlToString(context))
}
func yamlToString(context interface{}) string {
2015-09-29 01:05:28 +00:00
out, err := yaml.Marshal(context)
if err != nil {
log.Fatalf("error printing yaml: %v", err)
}
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 ")
}
return outStr
2015-09-27 03:18:33 +00:00
}
2015-10-06 05:07:09 +00:00
func readYaml(args []string, parsedData *map[interface{}]interface{}) {
if len(args) == 0 {
2015-09-28 02:00:38 +00:00
log.Fatalf("Must provide filename")
}
2015-10-05 04:48:34 +00:00
var rawData []byte
2015-10-06 05:07:09 +00:00
if args[0] == "-" {
2015-10-05 04:48:34 +00:00
rawData = readStdin()
} else {
2015-10-06 05:07:09 +00:00
rawData = readFile(args[0])
2015-10-05 04:48:34 +00:00
}
2015-09-26 22:20:42 +00:00
err := yaml.Unmarshal([]byte(rawData), &parsedData)
2015-09-28 02:00:38 +00:00
if err != nil {
log.Fatalf("error: %v", err)
}
2015-09-27 03:18:33 +00:00
}
2015-09-26 22:15:49 +00:00
2015-10-05 04:48:34 +00:00
func readStdin() []byte {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading stdin", err)
}
return bytes
}
func readFile(filename string) []byte {
var rawData, readError = ioutil.ReadFile(filename)
if readError != nil {
log.Fatalf("error: %v", readError)
2015-09-28 02:00:38 +00:00
}
return rawData
2015-09-27 03:11:10 +00:00
}