jsonpath user guide docs

This commit is contained in:
Dai Zuozhuo
2015-08-13 16:31:38 +08:00
parent b61a905b19
commit 85972c44a4
20 changed files with 198 additions and 207 deletions

View File

@@ -69,7 +69,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
validArgs := p.HandledResources()
cmd := &cobra.Command{
Use: "get [(-o|--output=)json|yaml|template|templatefile|wide|jsonpath|...] (TYPE [NAME | -l label] | TYPE/NAME ...)",
Use: "get [(-o|--output=)json|yaml|template|templatefile|wide|jsonpath|...] (TYPE [NAME | -l label] | TYPE/NAME ...) [flags]",
Short: "Display one or many resources",
Long: get_long,
Example: get_example,

View File

@@ -31,7 +31,7 @@ func AddPrinterFlags(cmd *cobra.Command) {
cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile|wide|jsonpath.")
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers.")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {}")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]")
cmd.Flags().String("sort-by", "", "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.")
cmd.Flags().BoolP("show-all", "a", false, "When printing, show all resources (default hide terminated pods.)")
}

View File

@@ -77,14 +77,6 @@ func TestVersionedPrinter(t *testing.T) {
}
}
func TestYAMLPrinter(t *testing.T) {
testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
}
func TestJSONPrinter(t *testing.T) {
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
}
func TestPrintDefault(t *testing.T) {
printer, found, err := GetPrinter("", "")
if err != nil {
@@ -95,107 +87,72 @@ func TestPrintDefault(t *testing.T) {
}
}
type internalType struct {
Name string
type TestPrintType struct {
Data string
}
func (*internalType) IsAnAPIObject() {
func (*TestPrintType) IsAnAPIObject() {}
}
type TestUnknownType struct{}
func TestPrintJSONForObject(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
printer, found, err := GetPrinter("json", "")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
}
if err := printer.PrintObj(&internalType{Name: "foo"}, buf); err != nil {
t.Fatalf("unexpected error: %#v", err)
}
obj := map[string]interface{}{}
if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
t.Fatalf("unexpected error: %#v\n%s", err, buf.String())
}
if obj["Name"] != "foo" {
t.Errorf("unexpected field: %#v", obj)
}
}
func (*TestUnknownType) IsAnAPIObject() {}
func TestPrintJSON(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
printer, found, err := GetPrinter("json", "")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
}
printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
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{})
printer, found, err := GetPrinter("yaml", "")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
}
printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
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{})
printer, found, err := GetPrinter("template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
}
unversionedPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
obj, err := api.Scheme.ConvertToVersion(unversionedPod, testapi.Version())
err = printer.PrintObj(obj, buf)
func TestPrinter(t *testing.T) {
//test inputs
simpleTest := &TestPrintType{"foo"}
podTest := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
testapi, err := api.Scheme.ConvertToVersion(podTest, testapi.Version())
if err != nil {
t.Fatalf("unexpected error: %#v", err)
t.Fatalf("unexpected error: %v", err)
}
if buf.String() != "foo" {
t.Errorf("unexpected output: %s", buf.String())
printerTests := []struct {
Name string
Format string
FormatArgument string
Input runtime.Object
Expect string
}{
{"test json", "json", "", simpleTest, "{\n \"Data\": \"foo\"\n}\n"},
{"test yaml", "yaml", "", simpleTest, "Data: foo\n"},
{"test template", "template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}",
podTest, "foo"},
{"test jsonpath", "jsonpath", "{.metadata.name}", podTest, "foo"},
{"emits versioned objects", "template", "{{.kind}}", testapi, "Pod"},
}
for _, test := range printerTests {
buf := bytes.NewBuffer([]byte{})
printer, found, err := GetPrinter(test.Format, test.FormatArgument)
if err != nil || !found {
t.Errorf("unexpected error: %#v", err)
}
if err := printer.PrintObj(test.Input, buf); err != nil {
t.Errorf("unexpected error: %#v", err)
}
if buf.String() != test.Expect {
t.Errorf("in %s, expect %q, got %q", test.Name, test.Expect, buf.String(), buf.String())
}
}
}
func TestPrintJSONPath(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
printer, found, err := GetPrinter("jsonpath", "{.metadata.name}")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
func TestBadPrinter(t *testing.T) {
badPrinterTests := []struct {
Name string
Format string
FormatArgument string
Error error
}{
{"empty template", "template", "", fmt.Errorf("template format specified but no template given")},
{"bad template", "template", "{{ .Name", fmt.Errorf("error parsing template {{ .Name, template: output:1: unclosed action\n")},
{"bad templatefile", "templatefile", "", fmt.Errorf("templatefile format specified but no template file given")},
{"bad jsonpath", "jsonpath", "{.Name", fmt.Errorf("error parsing jsonpath {.Name, unclosed action\n")},
}
unversionedPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
obj, err := api.Scheme.ConvertToVersion(unversionedPod, testapi.Version())
err = printer.PrintObj(obj, buf)
if err != nil {
t.Fatalf("unexpected error: %s %#v", buf, err)
}
if buf.String() != "foo" {
t.Errorf("unexpected output: %s", buf.String())
}
}
func TestPrintEmptyTemplate(t *testing.T) {
if _, _, err := GetPrinter("template", ""); err == nil {
t.Errorf("unexpected non-error")
}
}
func TestPrintBadTemplate(t *testing.T) {
if _, _, err := GetPrinter("template", "{{ .Name"); err == nil {
t.Errorf("unexpected non-error")
}
}
func TestPrintBadTemplateFile(t *testing.T) {
if _, _, err := GetPrinter("templatefile", ""); err == nil {
t.Errorf("unexpected non-error")
for _, test := range badPrinterTests {
_, _, err := GetPrinter(test.Format, test.FormatArgument)
if err == nil || err.Error() != test.Error.Error() {
t.Errorf("in %s, expect %s, got %s", test.Name, test.Error, err)
}
}
}
@@ -244,15 +201,13 @@ func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data
}
}
type TestPrintType struct {
Data string
func TestYAMLPrinter(t *testing.T) {
testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
}
func (*TestPrintType) IsAnAPIObject() {}
type TestUnknownType struct{}
func (*TestUnknownType) IsAnAPIObject() {}
func TestJSONPrinter(t *testing.T) {
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
}
func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
_, err := fmt.Fprintf(w, "%s", obj.Data)
@@ -301,27 +256,6 @@ func TestUnknownTypePrinting(t *testing.T) {
}
}
func TestTemplateEmitsVersionedObjects(t *testing.T) {
// kind is always blank in memory and set on the wire
printer, err := NewTemplatePrinter([]byte(`{{.kind}}`))
if err != nil {
t.Fatalf("tmpl fail: %v", err)
}
obj, err := api.Scheme.ConvertToVersion(&api.Pod{}, testapi.Version())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
buffer := &bytes.Buffer{}
err = printer.PrintObj(obj, buffer)
if err != nil {
t.Fatalf("print fail: %v", err)
}
if e, a := "Pod", string(buffer.Bytes()); e != a {
t.Errorf("Expected %v, got %v", e, a)
}
}
func TestTemplatePanic(t *testing.T) {
tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
printer, err := NewTemplatePrinter([]byte(tmpl))

View File

@@ -118,7 +118,7 @@ func TestStructInput(t *testing.T) {
{"union", "{[1,3,4]}", []int{0, 1, 2, 3, 4}, "1 3 4"},
{"array", "{[0:2]}", []string{"Monday", "Tudesday"}, "Monday Tudesday"},
{"variable", "hello {.Name}", storeData, "hello jsonpath"},
{"dict/", "{.Labels.web/html}", storeData, "15"},
{"dict/", "{$.Labels.web/html}", storeData, "15"},
{"dict-", "{.Labels.k8s-app}", storeData, "20"},
{"nest", "{.Bicycle.Color}", storeData, "red"},
{"allarray", "{.Book[*].Author}", storeData, "Nigel Rees Evelyn Waugh Herman Melville"},
@@ -210,6 +210,7 @@ func TestKubenates(t *testing.T) {
`127.0.0.1 127.0.0.2 127.0.0.3`},
{"double range", "{range .items[*]}{range .status.addresses[*]}{.address}, {end}{end}", nodesData,
`127.0.0.1, 127.0.0.2, 127.0.0.3, `},
{"recursive name", "{..name}", nodesData, `127.0.0.1 127.0.0.2 myself e2e`},
{"item name", "{.items[*].metadata.name}", nodesData, `127.0.0.1 127.0.0.2`},
{"union nodes capacity", "{.items[*]['metadata.name', 'status.capacity']}", nodesData,
`127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`},

View File

@@ -151,7 +151,7 @@ func (p *Parser) parseInsideAction(cur *ListNode) error {
return fmt.Errorf("unclosed action")
case r == ' ':
p.consumeText()
case r == '@': //the current object, just pass it
case r == '@' || r == '$': //the current object, just pass it
p.consumeText()
case r == '[':
return p.parseArray(cur)