Use a map for ResourcePrinter options
authorMikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 13:06:47 +0200
changeset 83 57afac822019
parent 82 9ac2281c07d7
child 84 5a894a10304c
Use a map for ResourcePrinter options
cmd/config.go
cmd/utils.go
cmd/version.go
printer/json.go
printer/plain.go
printer/printer.go
printer/templateprinter.go
printer/yaml.go
--- a/cmd/config.go	Sun May 07 11:56:37 2017 +0200
+++ b/cmd/config.go	Sun May 07 13:06:47 2017 +0200
@@ -83,7 +83,8 @@
 		}
 		errPrint("You can copy the following lines into a configuration file.")
 		errPrint("E.g. %s -i INSTANCE -L USERNAME -P PASS config dump > %s\n", AppName, cfile)
-		p, err = printer.NewPrinterTemplate(configurationTemplate)
+		pOptions := printer.Options{"template": configurationTemplate}
+		p, err = printer.NewPrinterTemplate(pOptions)
 	} else {
 		p, err = getPrinter()
 	}
--- a/cmd/utils.go	Sun May 07 11:56:37 2017 +0200
+++ b/cmd/utils.go	Sun May 07 13:06:47 2017 +0200
@@ -45,7 +45,7 @@
 
 // getPrinter returns a resource printer for the requested output format.
 func getPrinter() (printer.ResourcePrinter, error) {
-	var opt string
+	opt := make(printer.Options)
 	of := getOutputFormat()
 
 	// Initialize color mode
@@ -57,13 +57,13 @@
 	}
 
 	if of == "template" {
-		opt = outputTemplate
+		opt["template"] = outputTemplate
 		if outputTemplateFile != "" {
 			tmpl, err := readTemplate(outputTemplateFile, viper.GetString("template_directory"))
 			if err != nil {
 				return nil, err
 			}
-			opt = string(tmpl)
+			opt["template"] = string(tmpl)
 		}
 	}
 	return printer.NewPrinter(of, opt)
--- a/cmd/version.go	Sun May 07 11:56:37 2017 +0200
+++ b/cmd/version.go	Sun May 07 13:06:47 2017 +0200
@@ -40,7 +40,8 @@
 		var p printer.ResourcePrinter
 		var err error
 		if getOutputFormat() == "plain" {
-			p, err = printer.NewPrinterTemplate(versionTemplate)
+			pOptions := printer.Options{"template": versionTemplate}
+			p, err = printer.NewPrinterTemplate(pOptions)
 		} else {
 			p, err = getPrinter()
 		}
--- a/printer/json.go	Sun May 07 11:56:37 2017 +0200
+++ b/printer/json.go	Sun May 07 13:06:47 2017 +0200
@@ -16,7 +16,7 @@
 }
 
 // NewPrinterJSON returns a JSON ResourcePrinter
-func NewPrinterJSON(option string) (*JSONPrinter, error) {
+func NewPrinterJSON(options Options) (*JSONPrinter, error) {
 	return &JSONPrinter{}, nil
 }
 
--- a/printer/plain.go	Sun May 07 11:56:37 2017 +0200
+++ b/printer/plain.go	Sun May 07 13:06:47 2017 +0200
@@ -26,10 +26,10 @@
 
 // NewPrinterPlain returns a plaintext ResourcePrinter
 // For PlainPrinter, the option parameter contains the indent prefix.
-func NewPrinterPlain(option string) (*PlainPrinter, error) {
+func NewPrinterPlain(options Options) (*PlainPrinter, error) {
 	indentInc := "  "
-	if option != "" {
-		indentInc = option
+	if i, ok := options["indent"]; ok {
+		indentInc = i
 	}
 	return &PlainPrinter{Indent: indentInc}, nil
 }
--- a/printer/printer.go	Sun May 07 11:56:37 2017 +0200
+++ b/printer/printer.go	Sun May 07 13:06:47 2017 +0200
@@ -10,6 +10,9 @@
 	"io"
 )
 
+// Options contains options used when creating a ResourcePrinter
+type Options map[string]string
+
 // ResourcePrinter is an interface used to print objects.
 type ResourcePrinter interface {
 	// PrintObj receives a runtime object, formats it and prints it to a writer.
@@ -18,16 +21,16 @@
 
 // NewPrinter returns a ResourcePrinter for the specified kind of output.
 // It returns nil if the output is not supported.
-func NewPrinter(output, option string) (ResourcePrinter, error) {
+func NewPrinter(output string, options Options) (ResourcePrinter, error) {
 	switch output {
 	case "", "plain":
-		return NewPrinterPlain(option)
+		return NewPrinterPlain(options)
 	case "json":
-		return NewPrinterJSON(option)
+		return NewPrinterJSON(options)
 	case "yaml":
-		return NewPrinterYAML(option)
+		return NewPrinterYAML(options)
 	case "template":
-		return NewPrinterTemplate(option)
+		return NewPrinterTemplate(options)
 	}
 	return nil, fmt.Errorf("unhandled output format")
 }
--- a/printer/templateprinter.go	Sun May 07 11:56:37 2017 +0200
+++ b/printer/templateprinter.go	Sun May 07 13:06:47 2017 +0200
@@ -33,9 +33,9 @@
 }
 
 // NewPrinterTemplate returns a Template ResourcePrinter
-// For TemplatePrinter, the option parameter contains the template string.
-func NewPrinterTemplate(option string) (*TemplatePrinter, error) {
-	tmpl := option
+// For TemplatePrinter, the options parameter contains the template string.
+func NewPrinterTemplate(options Options) (*TemplatePrinter, error) {
+	tmpl := options["template"]
 	t, err := template.New("output").Funcs(template.FuncMap{
 		"fromhtml": html2string,
 		"fromunix": unix2string,
--- a/printer/yaml.go	Sun May 07 11:56:37 2017 +0200
+++ b/printer/yaml.go	Sun May 07 13:06:47 2017 +0200
@@ -18,7 +18,7 @@
 }
 
 // NewPrinterYAML returns a YAML ResourcePrinter
-func NewPrinterYAML(option string) (*YAMLPrinter, error) {
+func NewPrinterYAML(options Options) (*YAMLPrinter, error) {
 	return &YAMLPrinter{}, nil
 }