From 8f1643bc0c2eed5e43079e562e489ca555e71660 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Fri, 26 Sep 2014 20:35:12 -0700 Subject: [PATCH] Add the ability to print objects that are parsed in, without sending them anywhere. --- cmd/kubecfg/kubecfg.go | 74 +++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/cmd/kubecfg/kubecfg.go b/cmd/kubecfg/kubecfg.go index a946559a01ed8..b99f26cb1be99 100644 --- a/cmd/kubecfg/kubecfg.go +++ b/cmd/kubecfg/kubecfg.go @@ -266,12 +266,48 @@ func checkStorage(storage string) bool { return false } +func getPrinter() kubecfg.ResourcePrinter { + var printer kubecfg.ResourcePrinter + switch { + case *json: + printer = &kubecfg.IdentityPrinter{} + case *yaml: + printer = &kubecfg.YAMLPrinter{} + case len(*templateFile) > 0 || len(*templateStr) > 0: + var data []byte + if len(*templateFile) > 0 { + var err error + data, err = ioutil.ReadFile(*templateFile) + if err != nil { + glog.Fatalf("Error reading template %s, %v\n", *templateFile, err) + return nil + } + } else { + data = []byte(*templateStr) + } + tmpl, err := template.New("output").Parse(string(data)) + if err != nil { + glog.Fatalf("Error parsing template %s, %v\n", string(data), err) + return nil + } + printer = &kubecfg.TemplatePrinter{ + Template: tmpl, + } + default: + printer = humanReadablePrinter() + } + return printer +} + func executeAPIRequest(method string, c *client.Client) bool { storage, path, hasSuffix := storagePathFromArg(flag.Arg(1)) validStorage := checkStorage(storage) verb := "" setBody := false var version uint64 + + printer := getPrinter() + switch method { case "get": verb = "GET" @@ -309,6 +345,14 @@ func executeAPIRequest(method string, c *client.Client) bool { if !validStorage || !hasSuffix { glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>/", method, prettyWireStorage()) } + case "print": + data := readConfig(storage, c.RESTClient.Codec) + obj, err := latest.Codec.Decode(data) + if err != nil { + glog.Fatalf("error setting resource version: %v", err) + } + printer.PrintObj(obj, os.Stdout) + return true default: return false } @@ -345,36 +389,6 @@ func executeAPIRequest(method string, c *client.Client) bool { return false } - var printer kubecfg.ResourcePrinter - switch { - case *json: - printer = &kubecfg.IdentityPrinter{} - case *yaml: - printer = &kubecfg.YAMLPrinter{} - case len(*templateFile) > 0 || len(*templateStr) > 0: - var data []byte - if len(*templateFile) > 0 { - var err error - data, err = ioutil.ReadFile(*templateFile) - if err != nil { - glog.Fatalf("Error reading template %s, %v\n", *templateFile, err) - return false - } - } else { - data = []byte(*templateStr) - } - tmpl, err := template.New("output").Parse(string(data)) - if err != nil { - glog.Fatalf("Error parsing template %s, %v\n", string(data), err) - return false - } - printer = &kubecfg.TemplatePrinter{ - Template: tmpl, - } - default: - printer = humanReadablePrinter() - } - if err = printer.PrintObj(obj, os.Stdout); err != nil { body, _ := result.Raw() glog.Fatalf("Failed to print: %v\nRaw received object:\n%#v\n\nBody received: %v", err, obj, string(body))