# HG changeset patch # User Mikael Berthe # Date 1494110222 -7200 # Node ID 7f3e2577ed8513e9ae55dbebfbd04f0e86c7ad5c # Parent bca27c55be9fa47001840a251e6945256b2c0a01 Add cmd/utils.go diff -r bca27c55be9f -r 7f3e2577ed85 cmd/root.go --- a/cmd/root.go Sun May 07 00:26:04 2017 +0200 +++ b/cmd/root.go Sun May 07 00:37:02 2017 +0200 @@ -6,16 +6,12 @@ package cmd import ( - "fmt" - "io/ioutil" "os" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/McKael/madon" - "github.com/McKael/madonctl/printer" ) // AppName is the CLI application name @@ -159,15 +155,6 @@ RootCmd.PersistentFlags().Lookup("color").Annotations = annotationColor } -func checkOutputFormat(cmd *cobra.Command, args []string) error { - of := viper.GetString("output") - switch of { - case "", "plain", "json", "yaml", "template": - return nil // Accepted - } - return errors.Errorf("output format '%s' not supported", of) -} - // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { // enable ability to specify config file via flag @@ -193,48 +180,3 @@ errPrint("Using config file: %s", viper.ConfigFileUsed()) } } - -// getOutputFormat return the requested output format, defaulting to "plain". -func getOutputFormat() string { - of := viper.GetString("output") - if of == "" { - of = "plain" - } - // Override format if a template is provided - if of == "plain" && (outputTemplate != "" || outputTemplateFile != "") { - // If the format is plain and there is a template option, - // set the format to "template". - of = "template" - } - return of -} - -// getPrinter returns a resource printer for the requested output format. -func getPrinter() (printer.ResourcePrinter, error) { - var opt string - of := getOutputFormat() - - // Initialize color mode - switch viper.GetString("color") { - case "on", "yes", "force": - printer.ColorMode = 1 - case "off", "no": - printer.ColorMode = 2 - } - - if of == "template" { - opt = outputTemplate - if outputTemplateFile != "" { - tmpl, err := ioutil.ReadFile(outputTemplateFile) - if err != nil { - return nil, err - } - opt = string(tmpl) - } - } - return printer.NewPrinter(of, opt) -} - -func errPrint(format string, a ...interface{}) (n int, err error) { - return fmt.Fprintf(os.Stderr, format+"\n", a...) -} diff -r bca27c55be9f -r 7f3e2577ed85 cmd/utils.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd/utils.go Sun May 07 00:37:02 2017 +0200 @@ -0,0 +1,72 @@ +// Copyright © 2017 Mikael Berthe +// +// Licensed under the MIT license. +// Please see the LICENSE file is this directory. + +package cmd + +import ( + "fmt" + "io/ioutil" + "os" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/McKael/madonctl/printer" +) + +func checkOutputFormat(cmd *cobra.Command, args []string) error { + of := viper.GetString("output") + switch of { + case "", "plain", "json", "yaml", "template": + return nil // Accepted + } + return errors.Errorf("output format '%s' not supported", of) +} + +// getOutputFormat return the requested output format, defaulting to "plain". +func getOutputFormat() string { + of := viper.GetString("output") + if of == "" { + of = "plain" + } + // Override format if a template is provided + if of == "plain" && (outputTemplate != "" || outputTemplateFile != "") { + // If the format is plain and there is a template option, + // set the format to "template". + of = "template" + } + return of +} + +// getPrinter returns a resource printer for the requested output format. +func getPrinter() (printer.ResourcePrinter, error) { + var opt string + of := getOutputFormat() + + // Initialize color mode + switch viper.GetString("color") { + case "on", "yes", "force": + printer.ColorMode = 1 + case "off", "no": + printer.ColorMode = 2 + } + + if of == "template" { + opt = outputTemplate + if outputTemplateFile != "" { + tmpl, err := ioutil.ReadFile(outputTemplateFile) + if err != nil { + return nil, err + } + opt = string(tmpl) + } + } + return printer.NewPrinter(of, opt) +} + +func errPrint(format string, a ...interface{}) (n int, err error) { + return fmt.Fprintf(os.Stderr, format+"\n", a...) +}