Add cmd/utils.go
authorMikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 00:37:02 +0200
changeset 80 7f3e2577ed85
parent 79 bca27c55be9f
child 81 b1671f83e91b
Add cmd/utils.go
cmd/root.go
cmd/utils.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...)
-}
--- /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 <mikael@lilotux.net>
+//
+// 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...)
+}