2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"reflect"
|
2014-12-16 22:20:51 +00:00
|
|
|
"sort"
|
2014-10-06 01:24:19 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
2014-12-16 22:20:51 +00:00
|
|
|
"time"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-12-01 04:12:37 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// GetPrinter takes a format type, an optional format argument, a version and a convertor
|
|
|
|
// to be used if the underlying printer requires the object to be in a specific schema (
|
|
|
|
// any of the generic formatters), and the default printer to use for this object.
|
|
|
|
func GetPrinter(format, formatArgument, version string, convertor runtime.ObjectConvertor, defaultPrinter ResourcePrinter) (ResourcePrinter, error) {
|
2014-10-06 01:24:19 +00:00
|
|
|
var printer ResourcePrinter
|
|
|
|
switch format {
|
|
|
|
case "json":
|
2014-11-18 18:04:10 +00:00
|
|
|
printer = &JSONPrinter{version, convertor}
|
2014-10-06 01:24:19 +00:00
|
|
|
case "yaml":
|
2014-11-18 18:04:10 +00:00
|
|
|
printer = &YAMLPrinter{version, convertor}
|
2014-10-06 01:24:19 +00:00
|
|
|
case "template":
|
2014-11-18 18:04:10 +00:00
|
|
|
if len(formatArgument) == 0 {
|
2014-11-12 00:15:22 +00:00
|
|
|
return nil, fmt.Errorf("template format specified but no template given")
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2014-11-07 22:41:59 +00:00
|
|
|
var err error
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err = NewTemplatePrinter([]byte(formatArgument), version, convertor)
|
2014-10-30 02:32:25 +00:00
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
return nil, fmt.Errorf("error parsing template %s, %v\n", formatArgument, err)
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
|
|
|
case "templatefile":
|
2014-11-18 18:04:10 +00:00
|
|
|
if len(formatArgument) == 0 {
|
2014-11-12 00:15:22 +00:00
|
|
|
return nil, fmt.Errorf("templatefile format specified but no template file given")
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
data, err := ioutil.ReadFile(formatArgument)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
return nil, fmt.Errorf("error reading template %s, %v\n", formatArgument, err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err = NewTemplatePrinter(data, version, convertor)
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
2014-11-12 00:15:22 +00:00
|
|
|
return nil, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-01 22:44:03 +00:00
|
|
|
case "":
|
2014-10-27 19:56:34 +00:00
|
|
|
printer = defaultPrinter
|
2014-11-01 22:44:03 +00:00
|
|
|
default:
|
2014-11-12 00:15:22 +00:00
|
|
|
return nil, fmt.Errorf("output format %q not recognized", format)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-12 00:15:22 +00:00
|
|
|
return printer, nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// ResourcePrinter is an interface that knows how to print runtime objects.
|
2014-10-06 01:24:19 +00:00
|
|
|
type ResourcePrinter interface {
|
2014-11-01 22:44:03 +00:00
|
|
|
// Print receives an arbitrary object, formats it and prints it to a writer.
|
2014-10-06 01:24:19 +00:00
|
|
|
PrintObj(runtime.Object, io.Writer) error
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON.
|
|
|
|
// The input object is assumed to be in the internal version of an API and is converted
|
|
|
|
// to the given version first.
|
2014-11-12 00:15:22 +00:00
|
|
|
type JSONPrinter struct {
|
2014-11-18 18:04:10 +00:00
|
|
|
version string
|
|
|
|
convertor runtime.ObjectConvertor
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
// PrintObj is an implementation of ResourcePrinter.PrintObj which simply writes the object to the Writer.
|
2014-11-18 18:04:10 +00:00
|
|
|
func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|
|
|
outObj, err := p.convertor.ConvertToVersion(obj, p.version)
|
2014-11-12 00:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
data, err := json.Marshal(outObj)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-11 23:29:01 +00:00
|
|
|
dst := bytes.Buffer{}
|
|
|
|
err = json.Indent(&dst, data, "", " ")
|
|
|
|
dst.WriteByte('\n')
|
|
|
|
_, err = w.Write(dst.Bytes())
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML.
|
|
|
|
// The input object is assumed to be in the internal version of an API and is converted
|
|
|
|
// to the given version first.
|
2014-11-12 00:15:22 +00:00
|
|
|
type YAMLPrinter struct {
|
2014-11-18 18:04:10 +00:00
|
|
|
version string
|
|
|
|
convertor runtime.ObjectConvertor
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
// PrintObj prints the data as YAML.
|
2014-11-18 18:04:10 +00:00
|
|
|
func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|
|
|
outObj, err := p.convertor.ConvertToVersion(obj, p.version)
|
2014-11-11 23:29:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
output, err := yaml.Marshal(outObj)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprint(w, string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerEntry struct {
|
|
|
|
columns []string
|
|
|
|
printFunc reflect.Value
|
|
|
|
}
|
|
|
|
|
2014-11-12 01:31:13 +00:00
|
|
|
// HumanReadablePrinter is an implementation of ResourcePrinter which attempts to provide
|
|
|
|
// more elegant output. It is not threadsafe, but you may call PrintObj repeatedly; headers
|
|
|
|
// will only be printed if the object type changes. This makes it useful for printing items
|
|
|
|
// recieved from watches.
|
2014-10-06 01:24:19 +00:00
|
|
|
type HumanReadablePrinter struct {
|
|
|
|
handlerMap map[reflect.Type]*handlerEntry
|
2014-10-16 01:54:46 +00:00
|
|
|
noHeaders bool
|
2014-11-12 01:31:13 +00:00
|
|
|
lastType reflect.Type
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHumanReadablePrinter creates a HumanReadablePrinter.
|
2014-10-16 01:54:46 +00:00
|
|
|
func NewHumanReadablePrinter(noHeaders bool) *HumanReadablePrinter {
|
|
|
|
printer := &HumanReadablePrinter{
|
|
|
|
handlerMap: make(map[reflect.Type]*handlerEntry),
|
|
|
|
noHeaders: noHeaders,
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
printer.addDefaultHandlers()
|
|
|
|
return printer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler adds a print handler with a given set of columns to HumanReadablePrinter instance.
|
|
|
|
// printFunc is the function that will be called to print an object.
|
|
|
|
// It must be of the following type:
|
|
|
|
// func printFunc(object ObjectType, w io.Writer) error
|
|
|
|
// where ObjectType is the type of the object that will be printed.
|
|
|
|
func (h *HumanReadablePrinter) Handler(columns []string, printFunc interface{}) error {
|
|
|
|
printFuncValue := reflect.ValueOf(printFunc)
|
|
|
|
if err := h.validatePrintHandlerFunc(printFuncValue); err != nil {
|
|
|
|
glog.Errorf("Unable to add print handler: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objType := printFuncValue.Type().In(0)
|
|
|
|
h.handlerMap[objType] = &handlerEntry{
|
|
|
|
columns: columns,
|
|
|
|
printFunc: printFuncValue,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) error {
|
|
|
|
if printFunc.Kind() != reflect.Func {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler. %#v is not a function.", printFunc)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
funcType := printFunc.Type()
|
|
|
|
if funcType.NumIn() != 2 || funcType.NumOut() != 1 {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler." +
|
2014-10-06 01:24:19 +00:00
|
|
|
"Must accept 2 parameters and return 1 value.")
|
|
|
|
}
|
|
|
|
if funcType.In(1) != reflect.TypeOf((*io.Writer)(nil)).Elem() ||
|
|
|
|
funcType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler. The expected signature is: "+
|
2014-10-06 01:24:19 +00:00
|
|
|
"func handler(obj %v, w io.Writer) error", funcType.In(0))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-22 18:46:28 +00:00
|
|
|
var podColumns = []string{"NAME", "IMAGE(S)", "HOST", "LABELS", "STATUS"}
|
|
|
|
var replicationControllerColumns = []string{"NAME", "IMAGE(S)", "SELECTOR", "REPLICAS"}
|
|
|
|
var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT"}
|
2014-11-22 16:33:02 +00:00
|
|
|
var minionColumns = []string{"NAME", "LABELS"}
|
2014-10-06 01:24:19 +00:00
|
|
|
var statusColumns = []string{"STATUS"}
|
2014-12-16 22:20:51 +00:00
|
|
|
var eventColumns = []string{"TIME", "NAME", "KIND", "CONDITION", "REASON", "MESSAGE"}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
// addDefaultHandlers adds print handlers for default Kubernetes types.
|
|
|
|
func (h *HumanReadablePrinter) addDefaultHandlers() {
|
|
|
|
h.Handler(podColumns, printPod)
|
|
|
|
h.Handler(podColumns, printPodList)
|
|
|
|
h.Handler(replicationControllerColumns, printReplicationController)
|
|
|
|
h.Handler(replicationControllerColumns, printReplicationControllerList)
|
|
|
|
h.Handler(serviceColumns, printService)
|
|
|
|
h.Handler(serviceColumns, printServiceList)
|
|
|
|
h.Handler(minionColumns, printMinion)
|
|
|
|
h.Handler(minionColumns, printMinionList)
|
|
|
|
h.Handler(statusColumns, printStatus)
|
2014-11-04 02:02:27 +00:00
|
|
|
h.Handler(eventColumns, printEvent)
|
|
|
|
h.Handler(eventColumns, printEventList)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "Unknown object: %s", string(data))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) printHeader(columnNames []string, w io.Writer) error {
|
|
|
|
if _, err := fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func podHostString(host, ip string) string {
|
|
|
|
if host == "" && ip == "" {
|
|
|
|
return "<unassigned>"
|
|
|
|
}
|
|
|
|
return host + "/" + ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPod(pod *api.Pod, w io.Writer) error {
|
2014-11-07 02:09:46 +00:00
|
|
|
// TODO: remove me when pods are converted
|
|
|
|
spec := &api.PodSpec{}
|
2014-11-13 15:52:13 +00:00
|
|
|
if err := api.Scheme.Convert(&pod.Spec, spec); err != nil {
|
2014-11-07 02:09:46 +00:00
|
|
|
glog.Errorf("Unable to convert pod manifest: %v", err)
|
|
|
|
}
|
2014-11-15 00:20:43 +00:00
|
|
|
il := listOfImages(spec)
|
|
|
|
// Be paranoid about the case where there is no image.
|
|
|
|
var firstImage string
|
|
|
|
if len(il) > 0 {
|
|
|
|
firstImage, il = il[0], il[1:]
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
|
2014-11-15 00:20:43 +00:00
|
|
|
pod.Name, firstImage,
|
2014-11-13 15:52:13 +00:00
|
|
|
podHostString(pod.Status.Host, pod.Status.HostIP),
|
2014-11-24 23:41:11 +00:00
|
|
|
formatLabels(pod.Labels), pod.Status.Phase)
|
2014-11-15 00:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Lay out all the other images on separate lines.
|
|
|
|
for _, image := range il {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", "", image, "", "", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPodList(podList *api.PodList, w io.Writer) error {
|
|
|
|
for _, pod := range podList.Items {
|
|
|
|
if err := printPod(&pod, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
func printReplicationController(controller *api.ReplicationController, w io.Writer) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%d\n",
|
2014-11-07 02:09:46 +00:00
|
|
|
controller.Name, makeImageList(&controller.Spec.Template.Spec),
|
2014-11-24 23:41:11 +00:00
|
|
|
formatLabels(controller.Spec.Selector), controller.Spec.Replicas)
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
for _, controller := range list.Items {
|
|
|
|
if err := printReplicationController(&controller, w); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printService(svc *api.Service, w io.Writer) error {
|
2014-11-24 23:41:11 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", svc.Name, formatLabels(svc.Labels),
|
|
|
|
formatLabels(svc.Spec.Selector), svc.Spec.PortalIP, svc.Spec.Port)
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printServiceList(list *api.ServiceList, w io.Writer) error {
|
|
|
|
for _, svc := range list.Items {
|
|
|
|
if err := printService(&svc, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-08 03:44:27 +00:00
|
|
|
func printMinion(minion *api.Node, w io.Writer) error {
|
2014-11-24 23:41:11 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\n", minion.Name, formatLabels(minion.Labels))
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-08 03:44:27 +00:00
|
|
|
func printMinionList(list *api.NodeList, w io.Writer) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
for _, minion := range list.Items {
|
|
|
|
if err := printMinion(&minion, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printStatus(status *api.Status, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%v\n", status.Status)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 02:02:27 +00:00
|
|
|
func printEvent(event *api.Event, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(
|
2014-12-16 22:20:51 +00:00
|
|
|
w, "%s\t%s\t%s\t%s\t%s\t%s\n",
|
|
|
|
event.Timestamp.Time.Format(time.RFC1123Z),
|
2014-11-04 02:02:27 +00:00
|
|
|
event.InvolvedObject.Name,
|
|
|
|
event.InvolvedObject.Kind,
|
2014-12-12 21:27:25 +00:00
|
|
|
event.Condition,
|
2014-11-04 02:02:27 +00:00
|
|
|
event.Reason,
|
|
|
|
event.Message,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:20:51 +00:00
|
|
|
// Sorts and prints the EventList in a human-friendly format.
|
2014-11-04 02:02:27 +00:00
|
|
|
func printEventList(list *api.EventList, w io.Writer) error {
|
2014-12-16 22:20:51 +00:00
|
|
|
sort.Sort(SortableEvents(list.Items))
|
2014-11-04 02:02:27 +00:00
|
|
|
for i := range list.Items {
|
|
|
|
if err := printEvent(&list.Items[i], w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
// PrintObj prints the obj in a human-friendly format according to the type of the obj.
|
|
|
|
func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error {
|
|
|
|
w := tabwriter.NewWriter(output, 20, 5, 3, ' ', 0)
|
|
|
|
defer w.Flush()
|
2014-11-12 01:31:13 +00:00
|
|
|
t := reflect.TypeOf(obj)
|
|
|
|
if handler := h.handlerMap[t]; handler != nil {
|
|
|
|
if !h.noHeaders && t != h.lastType {
|
2014-10-16 01:54:46 +00:00
|
|
|
h.printHeader(handler.columns, w)
|
2014-11-12 01:31:13 +00:00
|
|
|
h.lastType = t
|
2014-10-16 01:54:46 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w)}
|
|
|
|
resultValue := handler.printFunc.Call(args)[0]
|
|
|
|
if resultValue.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
return resultValue.Interface().(error)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
return fmt.Errorf("error: unknown type %#v", obj)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
|
|
|
|
type TemplatePrinter struct {
|
2014-11-20 22:09:59 +00:00
|
|
|
rawTemplate string
|
|
|
|
template *template.Template
|
|
|
|
version string
|
|
|
|
convertor runtime.ObjectConvertor
|
2014-11-07 22:41:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
func NewTemplatePrinter(tmpl []byte, asVersion string, convertor runtime.ObjectConvertor) (*TemplatePrinter, error) {
|
2014-11-07 22:41:59 +00:00
|
|
|
t, err := template.New("output").Parse(string(tmpl))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-20 22:09:59 +00:00
|
|
|
return &TemplatePrinter{
|
|
|
|
rawTemplate: string(tmpl),
|
|
|
|
template: t,
|
|
|
|
version: asVersion,
|
|
|
|
convertor: convertor,
|
|
|
|
}, nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj formats the obj with the Go Template.
|
2014-11-18 18:04:10 +00:00
|
|
|
func (p *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|
|
|
outObj, err := p.convertor.ConvertToVersion(obj, p.version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(outObj)
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
out := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-20 22:09:59 +00:00
|
|
|
if err = p.template.Execute(w, out); err != nil {
|
|
|
|
return fmt.Errorf("error executing template '%v': '%v'\n----data----\n%#v\n", p.rawTemplate, err, out)
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:56:41 +00:00
|
|
|
func tabbedString(f func(io.Writer) error) (string, error) {
|
2014-10-06 01:24:19 +00:00
|
|
|
out := new(tabwriter.Writer)
|
2014-11-20 16:50:50 +00:00
|
|
|
buf := &bytes.Buffer{}
|
2014-10-06 01:24:19 +00:00
|
|
|
out.Init(buf, 0, 8, 1, '\t', 0)
|
|
|
|
|
|
|
|
err := f(out)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Flush()
|
|
|
|
str := string(buf.String())
|
|
|
|
return str, nil
|
|
|
|
}
|