Skip to content

Commit

Permalink
Merge pull request kubernetes#1476 from brendandburns/print
Browse files Browse the repository at this point in the history
Add the ability to print objects that are parsed in, without sending them anywhere.
  • Loading branch information
dchen1107 committed Sep 29, 2014
2 parents 09a3231 + 8f1643b commit a208fbf
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -309,6 +345,14 @@ func executeAPIRequest(method string, c *client.Client) bool {
if !validStorage || !hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>/<id>", 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
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit a208fbf

Please sign in to comment.