Refactored code

This commit is contained in:
Mike Farah 2015-10-13 21:42:36 +11:00
parent 001925a4d4
commit 6b566fd983

30
yaml.go
View File

@ -17,7 +17,20 @@ var inputJSON = false
var outputToJSON = false var outputToJSON = false
func main() { func main() {
var cmdRead = &cobra.Command{ var cmdRead = createReadCmd()
var cmdWrite = createWriteCmd()
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")
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
rootCmd.AddCommand(cmdRead, cmdWrite)
rootCmd.Execute()
}
func createReadCmd() *cobra.Command {
return &cobra.Command{
Use: "read [yaml_file] [path]", Use: "read [yaml_file] [path]",
Aliases: []string{"r"}, Aliases: []string{"r"},
Short: "yaml r sample.yaml a.b.c", Short: "yaml r sample.yaml a.b.c",
@ -27,11 +40,13 @@ yaml r - a.b.c (reads from stdin)
yaml r things.yaml a.*.c yaml r things.yaml a.*.c
yaml r things.yaml a.array[0].blah yaml r things.yaml a.array[0].blah
yaml r things.yaml a.array[*].blah yaml r things.yaml a.array[*].blah
`, `,
Long: "Outputs the value of the given path in the yaml file to STDOUT", Long: "Outputs the value of the given path in the yaml file to STDOUT",
Run: readProperty, Run: readProperty,
} }
}
func createWriteCmd() *cobra.Command {
var cmdWrite = &cobra.Command{ var cmdWrite = &cobra.Command{
Use: "write [yaml_file] [path] [value]", Use: "write [yaml_file] [path] [value]",
Aliases: []string{"w"}, Aliases: []string{"w"},
@ -42,7 +57,7 @@ yaml write --inplace things.yaml a.b.c cat
yaml w -i things.yaml a.b.c cat yaml w -i things.yaml a.b.c cat
yaml w --script update_script.yaml things.yaml yaml w --script update_script.yaml things.yaml
yaml w -i -s update_script.yaml things.yaml yaml w -i -s update_script.yaml things.yaml
`, `,
Long: `Updates the yaml file w.r.t the given path and value. 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. Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
@ -58,14 +73,7 @@ a.b.e:
} }
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") 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().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
return cmdWrite
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")
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
rootCmd.AddCommand(cmdRead, cmdWrite)
rootCmd.Execute()
} }
func readProperty(cmd *cobra.Command, args []string) { func readProperty(cmd *cobra.Command, args []string) {