Skip to content

Commit

Permalink
Add a template printer to kubecfg.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Jul 15, 2014
1 parent dcbb309 commit f7bd5a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"strconv"
"strings"
"text/template"
"time"

kube_client "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
Expand All @@ -50,6 +51,7 @@ var (
verbose = flag.Bool("verbose", false, "If true, print extra information")
proxy = flag.Bool("proxy", false, "If true, run a proxy to the api server")
www = flag.String("www", "", "If -proxy is true, use this directory to serve static files")
templateFile = flag.String("template", "", "If present load this file as a golang template and us it for output printing")
)

func usage() {
Expand Down Expand Up @@ -188,6 +190,20 @@ func executeAPIRequest(method string, s *kube_client.Client) bool {
printer = &kubecfg.IdentityPrinter{}
} else if *yaml {
printer = &kubecfg.YAMLPrinter{}
} else if len(*templateFile) > 0 {
data, err := ioutil.ReadFile(*templateFile)
if err != nil {
glog.Fatalf("Error reading template %s, %v\n", *templateFile, err)
return false
}
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,
}
} else {
printer = &kubecfg.HumanReadablePrinter{}
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/kubecfg/resource_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"strings"
"text/tabwriter"
"text/template"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
Expand Down Expand Up @@ -235,3 +236,19 @@ func (h *HumanReadablePrinter) PrintObj(obj interface{}, output io.Writer) error
return err
}
}

type TemplatePrinter struct {
Template *template.Template
}

func (t *TemplatePrinter) Print(data []byte, w io.Writer) error {
obj, err := api.Decode(data)
if err != nil {
return err
}
return t.PrintObj(obj, w)
}

func (t *TemplatePrinter) PrintObj(obj interface{}, w io.Writer) error {
return t.Template.Execute(w, obj)
}

0 comments on commit f7bd5a6

Please sign in to comment.