2015-09-26 22:15:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-28 02:00:38 +00:00
|
|
|
"fmt"
|
2017-04-11 23:16:54 +00:00
|
|
|
"github.com/op/go-logging"
|
2017-05-02 23:19:55 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v2"
|
2015-09-28 02:00:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"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-10-07 23:31:31 +00:00
|
|
|
var writeScript = ""
|
2015-10-11 06:06:52 +00:00
|
|
|
var inputJSON = false
|
2015-10-10 23:00:22 +00:00
|
|
|
var outputToJSON = false
|
2017-04-11 23:16:54 +00:00
|
|
|
var verbose = false
|
|
|
|
var log = logging.MustGetLogger("yaml")
|
|
|
|
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))
|
2015-10-06 05:07:09 +00:00
|
|
|
|
2015-09-26 22:15:49 +00:00
|
|
|
func main() {
|
2017-04-11 23:16:54 +00:00
|
|
|
backend.SetLevel(logging.ERROR, "")
|
|
|
|
logging.SetBackend(backend)
|
|
|
|
|
2015-10-13 10:42:36 +00:00
|
|
|
var cmdRead = createReadCmd()
|
|
|
|
var cmdWrite = createWriteCmd()
|
2017-04-12 11:10:00 +00:00
|
|
|
var cmdNew = createNewCmd()
|
2015-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{Use: "yaml"}
|
|
|
|
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")
|
2017-04-12 11:10:00 +00:00
|
|
|
rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
|
2015-10-13 10:42:36 +00:00
|
|
|
rootCmd.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createReadCmd() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2015-10-06 05:07:09 +00:00
|
|
|
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
|
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",
|
|
|
|
Run: readProperty,
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
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"},
|
2015-10-09 10:30:15 +00:00
|
|
|
Short: "yaml w [--inplace/-i] [--script/-s script_file] sample.yaml a.b.c newValueForC",
|
2015-10-06 05:07:09 +00:00
|
|
|
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
|
2015-10-09 10:30:15 +00:00
|
|
|
yaml w --script update_script.yaml things.yaml
|
|
|
|
yaml w -i -s update_script.yaml things.yaml
|
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.
|
2015-10-09 10:30:15 +00:00
|
|
|
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
|
|
|
|
|
|
|
|
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
|
|
|
|
`,
|
2015-10-06 05:07:09 +00:00
|
|
|
Run: writeProperty,
|
|
|
|
}
|
|
|
|
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
2015-10-07 23:31:31 +00:00
|
|
|
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
|
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
|
|
|
|
2017-04-12 11:10:00 +00:00
|
|
|
func createNewCmd() *cobra.Command {
|
|
|
|
var cmdNew = &cobra.Command{
|
|
|
|
Use: "new [path] [value]",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Short: "yaml n [--script/-s script_file] a.b.c newValueForC",
|
|
|
|
Example: `
|
|
|
|
yaml new a.b.c cat
|
|
|
|
yaml n a.b.c cat
|
|
|
|
yaml 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.
|
|
|
|
`,
|
|
|
|
Run: newProperty,
|
|
|
|
}
|
|
|
|
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
|
|
|
|
return cmdNew
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:07:09 +00:00
|
|
|
func readProperty(cmd *cobra.Command, args []string) {
|
2017-04-11 23:16:54 +00:00
|
|
|
if verbose {
|
|
|
|
backend.SetLevel(logging.DEBUG, "")
|
|
|
|
}
|
2015-10-10 23:00:22 +00:00
|
|
|
print(read(args))
|
2015-10-09 10:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func read(args []string) interface{} {
|
2017-02-26 22:01:52 +00:00
|
|
|
var parsedData yaml.MapSlice
|
2015-09-29 06:29:32 +00:00
|
|
|
|
2015-10-11 06:06:52 +00:00
|
|
|
readData(args[0], &parsedData, inputJSON)
|
2015-09-27 03:18:33 +00:00
|
|
|
|
2015-10-06 05:07:09 +00:00
|
|
|
if len(args) == 1 {
|
2015-10-09 10:24:42 +00:00
|
|
|
return parsedData
|
2015-09-29 06:29:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 05:07:09 +00:00
|
|
|
var paths = parsePath(args[1])
|
2015-09-27 03:18:33 +00:00
|
|
|
|
2015-10-09 10:24:42 +00:00
|
|
|
return readMap(parsedData, paths[0], paths[1:len(paths)])
|
2015-09-29 00:56:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 11:10:00 +00:00
|
|
|
func newProperty(cmd *cobra.Command, args []string) {
|
|
|
|
if verbose {
|
|
|
|
backend.SetLevel(logging.DEBUG, "")
|
|
|
|
}
|
|
|
|
updatedData := newYaml(args)
|
|
|
|
print(updatedData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newYaml(args []string) interface{} {
|
2017-04-18 05:55:43 +00:00
|
|
|
var writeCommands yaml.MapSlice
|
2017-04-12 11:10:00 +00:00
|
|
|
if writeScript != "" {
|
|
|
|
readData(writeScript, &writeCommands, false)
|
|
|
|
} else if len(args) < 2 {
|
|
|
|
die("Must provide <path_to_update> <value>")
|
|
|
|
} else {
|
2017-04-18 05:55:43 +00:00
|
|
|
writeCommands = make(yaml.MapSlice, 1)
|
|
|
|
writeCommands[0] = yaml.MapItem{Key: args[0], Value: parseValue(args[1])}
|
2017-04-12 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsedData := make(yaml.MapSlice, 0)
|
|
|
|
|
2017-04-18 05:55:43 +00:00
|
|
|
return updateParsedData(parsedData, writeCommands)
|
2017-04-12 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 05:07:09 +00:00
|
|
|
func writeProperty(cmd *cobra.Command, args []string) {
|
2017-04-11 23:16:54 +00:00
|
|
|
if verbose {
|
|
|
|
backend.SetLevel(logging.DEBUG, "")
|
|
|
|
}
|
2015-10-09 10:24:42 +00:00
|
|
|
updatedData := updateYaml(args)
|
|
|
|
if writeInplace {
|
|
|
|
ioutil.WriteFile(args[0], []byte(yamlToString(updatedData)), 0644)
|
|
|
|
} else {
|
2015-10-10 23:00:22 +00:00
|
|
|
print(updatedData)
|
2015-10-09 10:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 05:55:43 +00:00
|
|
|
func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice) yaml.MapSlice {
|
|
|
|
for _, entry := range writeCommands {
|
|
|
|
path := entry.Key
|
|
|
|
value := entry.Value
|
|
|
|
var paths = parsePath(path.(string))
|
|
|
|
parsedData = writeMap(parsedData, paths, value)
|
|
|
|
}
|
|
|
|
return parsedData
|
|
|
|
}
|
|
|
|
|
2015-10-09 10:24:42 +00:00
|
|
|
func updateYaml(args []string) interface{} {
|
2017-04-18 05:55:43 +00:00
|
|
|
var writeCommands yaml.MapSlice
|
2015-10-07 23:31:31 +00:00
|
|
|
if writeScript != "" {
|
2015-10-11 06:06:52 +00:00
|
|
|
readData(writeScript, &writeCommands, false)
|
2015-10-07 23:31:31 +00:00
|
|
|
} else if len(args) < 3 {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("Must provide <filename> <path_to_update> <value>")
|
2015-10-07 23:31:31 +00:00
|
|
|
} else {
|
2017-04-18 05:55:43 +00:00
|
|
|
writeCommands = make(yaml.MapSlice, 1)
|
|
|
|
writeCommands[0] = yaml.MapItem{Key: args[1], Value: parseValue(args[2])}
|
2015-09-29 06:29:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:01:52 +00:00
|
|
|
var parsedData yaml.MapSlice
|
2015-10-11 06:06:52 +00:00
|
|
|
readData(args[0], &parsedData, inputJSON)
|
2015-09-29 00:56:28 +00:00
|
|
|
|
2017-04-18 05:55:43 +00:00
|
|
|
return updateParsedData(parsedData, writeCommands)
|
2015-10-06 05:07:09 +00:00
|
|
|
}
|
2015-10-09 10:24:42 +00:00
|
|
|
|
2015-10-07 23:31:31 +00:00
|
|
|
func parseValue(argument string) interface{} {
|
2015-10-01 00:41:43 +00:00
|
|
|
var value, err interface{}
|
2015-10-03 07:25:13 +00:00
|
|
|
var inQuotes = argument[0] == '"'
|
|
|
|
if !inQuotes {
|
2015-10-01 00:41:43 +00:00
|
|
|
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-01 00:41:43 +00:00
|
|
|
}
|
2015-10-03 07:25:13 +00:00
|
|
|
return argument[1 : len(argument)-1]
|
2015-10-01 00:41:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 23:00:22 +00:00
|
|
|
func print(context interface{}) {
|
|
|
|
var out string
|
|
|
|
if outputToJSON {
|
|
|
|
out = jsonToString(context)
|
|
|
|
} else {
|
|
|
|
out = yamlToString(context)
|
|
|
|
}
|
|
|
|
fmt.Println(out)
|
2015-10-03 08:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func yamlToString(context interface{}) string {
|
2015-09-29 01:05:28 +00:00
|
|
|
out, err := yaml.Marshal(context)
|
|
|
|
if err != nil {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("error printing yaml: %v", err)
|
2015-09-29 01:05:28 +00:00
|
|
|
}
|
2015-09-29 06:29:32 +00:00
|
|
|
outStr := string(out)
|
2015-10-03 07:48:54 +00:00
|
|
|
// 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-11 06:06:52 +00:00
|
|
|
func readData(filename string, parsedData interface{}, readAsJSON bool) {
|
2015-10-07 23:31:31 +00:00
|
|
|
if filename == "" {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("Must provide filename")
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
2015-10-05 04:48:34 +00:00
|
|
|
|
|
|
|
var rawData []byte
|
2015-10-07 23:31:31 +00:00
|
|
|
if filename == "-" {
|
2015-10-05 04:48:34 +00:00
|
|
|
rawData = readStdin()
|
|
|
|
} else {
|
2015-10-07 23:31:31 +00:00
|
|
|
rawData = readFile(filename)
|
2015-10-05 04:48:34 +00:00
|
|
|
}
|
2015-09-26 22:20:42 +00:00
|
|
|
|
2017-04-19 05:45:45 +00:00
|
|
|
err := yaml.Unmarshal([]byte(rawData), parsedData)
|
2015-09-28 02:00:38 +00:00
|
|
|
if err != nil {
|
2015-10-11 06:06:52 +00:00
|
|
|
die("error parsing data: ", err)
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
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 {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("error reading stdin", err)
|
2015-10-05 04:48:34 +00:00
|
|
|
}
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readFile(filename string) []byte {
|
|
|
|
var rawData, readError = ioutil.ReadFile(filename)
|
|
|
|
if readError != nil {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("error: %v", readError)
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
return rawData
|
2015-09-27 03:11:10 +00:00
|
|
|
}
|
2015-10-06 05:39:19 +00:00
|
|
|
|
|
|
|
func die(message ...interface{}) {
|
|
|
|
fmt.Println(message)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|