mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 21:12:07 +00:00
Add --template and --templatefile options for more flexibility
Allow directly entered templates for scripting flexibility. Changes --output=template to mean "string" and --output=templatefile to mean "from file"
This commit is contained in:
parent
e46adc4cd0
commit
70aa9cc62c
@ -26,7 +26,7 @@ import (
|
|||||||
|
|
||||||
func (f *Factory) NewCmdGet(out io.Writer) *cobra.Command {
|
func (f *Factory) NewCmdGet(out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "get [(-o|--output=)console|json|yaml|...] <resource> [<id>]",
|
Use: "get [(-o|--output=)json|yaml|...] <resource> [<id>]",
|
||||||
Short: "Display one or many resources",
|
Short: "Display one or many resources",
|
||||||
Long: `Display one or many resources.
|
Long: `Display one or many resources.
|
||||||
|
|
||||||
@ -66,11 +66,9 @@ Examples:
|
|||||||
checkErr(err)
|
checkErr(err)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// TODO Add an --output-version lock which can ensure that regardless of the
|
cmd.Flags().StringP("output", "o", "", "Output format: json|yaml|template|templatefile")
|
||||||
// server version, the client output stays the same.
|
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers")
|
||||||
cmd.Flags().StringP("output", "o", "console", "Output format: console|json|yaml|template")
|
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when --output=template or --output=templatefile")
|
||||||
cmd.Flags().Bool("no-headers", false, "When output format is console, don't print headers")
|
|
||||||
cmd.Flags().StringP("template", "t", "", "Path to template file to use when --output=template")
|
|
||||||
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,18 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
|
|||||||
case "yaml":
|
case "yaml":
|
||||||
printer = &YAMLPrinter{}
|
printer = &YAMLPrinter{}
|
||||||
case "template":
|
case "template":
|
||||||
|
var data []byte
|
||||||
|
if len(templateFile) == 0 {
|
||||||
|
return printer, fmt.Errorf("template format specified but no template given")
|
||||||
|
}
|
||||||
|
tmpl, err := template.New("output").Parse(templateFile)
|
||||||
|
if err != nil {
|
||||||
|
return printer, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
|
||||||
|
}
|
||||||
|
printer = &TemplatePrinter{
|
||||||
|
Template: tmpl,
|
||||||
|
}
|
||||||
|
case "templatefile":
|
||||||
var data []byte
|
var data []byte
|
||||||
if len(templateFile) > 0 {
|
if len(templateFile) > 0 {
|
||||||
var err error
|
var err error
|
||||||
@ -63,7 +75,7 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
|
|||||||
return printer, fmt.Errorf("Error reading template %s, %v\n", templateFile, err)
|
return printer, fmt.Errorf("Error reading template %s, %v\n", templateFile, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return printer, fmt.Errorf("template format specified but no template file given")
|
return printer, fmt.Errorf("templatefile format specified but no template file given")
|
||||||
}
|
}
|
||||||
tmpl, err := template.New("output").Parse(string(data))
|
tmpl, err := template.New("output").Parse(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -52,6 +52,59 @@ func TestJSONPrinter(t *testing.T) {
|
|||||||
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
|
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrintJSON(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "json", "", nil); err != nil {
|
||||||
|
t.Errorf("unexpected error: %#v", err)
|
||||||
|
}
|
||||||
|
obj := map[string]interface{}{}
|
||||||
|
if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
|
||||||
|
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintYAML(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "yaml", "", nil); err != nil {
|
||||||
|
t.Errorf("unexpected error: %#v", err)
|
||||||
|
}
|
||||||
|
obj := map[string]interface{}{}
|
||||||
|
if err := yaml.Unmarshal(buf.Bytes(), &obj); err != nil {
|
||||||
|
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintTemplate(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name }}", nil); err != nil {
|
||||||
|
t.Errorf("unexpected error: %#v", err)
|
||||||
|
}
|
||||||
|
if buf.String() != "foo" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintEmptyTemplate(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "", nil); err == nil {
|
||||||
|
t.Errorf("unexpected non-error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintBadTemplate(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name", nil); err == nil {
|
||||||
|
t.Errorf("unexpected non-error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintBadTemplateFile(t *testing.T) {
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "templatefile", "", nil); err == nil {
|
||||||
|
t.Errorf("unexpected non-error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
|
func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user