diff --git a/pkg/kubectl/cmd/config/view.go b/pkg/kubectl/cmd/config/view.go index 8220645eabd..205795ea2ab 100644 --- a/pkg/kubectl/cmd/config/view.go +++ b/pkg/kubectl/cmd/config/view.go @@ -18,6 +18,7 @@ package config import ( "errors" + "fmt" "io" "github.com/golang/glog" @@ -52,6 +53,8 @@ $ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2 func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command { options := &ViewOptions{ConfigAccess: ConfigAccess} + // Default to yaml + defaultOutputFormat := "yaml" cmd := &cobra.Command{ Use: "view", @@ -60,6 +63,11 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command { Example: view_example, Run: func(cmd *cobra.Command, args []string) { options.Complete() + outputFormat := cmdutil.GetFlagString(cmd, "output") + if outputFormat == "wide" { + fmt.Printf("--output wide is not available in kubectl config view; reset to default output format (%s)\n\n", defaultOutputFormat) + cmd.Flags().Set("output", defaultOutputFormat) + } printer, _, err := cmdutil.PrinterForCommand(cmd) if err != nil { @@ -76,8 +84,7 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command { } cmdutil.AddPrinterFlags(cmd) - // Default to yaml - cmd.Flags().Set("output", "yaml") + cmd.Flags().Set("output", defaultOutputFormat) options.Merge.Default(true) cmd.Flags().Var(&options.Merge, "merge", "merge together the full hierarchy of kubeconfig files") diff --git a/pkg/util/jsonpath/parser.go b/pkg/util/jsonpath/parser.go index 85aa2c1316e..facc4834d60 100644 --- a/pkg/util/jsonpath/parser.go +++ b/pkg/util/jsonpath/parser.go @@ -63,8 +63,12 @@ func NewParser(name string) *Parser { // parseAction parsed the expression inside delimiter func parseAction(name, text string) (*Parser, error) { p, err := Parse(name, fmt.Sprintf("%s%s%s", leftDelim, text, rightDelim)) + // when error happens, p will be nil, so we need to return here + if err != nil { + return p, err + } p.Root = p.Root.Nodes[0].(*ListNode) - return p, err + return p, nil } func (p *Parser) Parse(text string) error { diff --git a/pkg/util/jsonpath/parser_test.go b/pkg/util/jsonpath/parser_test.go index e0102062e8b..a061043b836 100644 --- a/pkg/util/jsonpath/parser_test.go +++ b/pkg/util/jsonpath/parser_test.go @@ -21,44 +21,46 @@ import ( ) type parserTest struct { - name string - text string - nodes []Node + name string + text string + nodes []Node + shouldError bool } var parserTests = []parserTest{ - {"plain", `hello jsonpath`, []Node{newText("hello jsonpath")}}, + {"plain", `hello jsonpath`, []Node{newText("hello jsonpath")}, false}, {"variable", `hello {.jsonpath}`, - []Node{newText("hello "), newList(), newField("jsonpath")}}, + []Node{newText("hello "), newList(), newField("jsonpath")}, false}, {"arrayfiled", `hello {['jsonpath']}`, - []Node{newText("hello "), newList(), newField("jsonpath")}}, - {"quote", `{"{"}`, []Node{newList(), newText("{")}}, + []Node{newText("hello "), newList(), newField("jsonpath")}, false}, + {"quote", `{"{"}`, []Node{newList(), newText("{")}, false}, {"array", `{[1:3]}`, []Node{newList(), - newArray([3]ParamsEntry{{1, true}, {3, true}, {0, false}})}}, + newArray([3]ParamsEntry{{1, true}, {3, true}, {0, false}})}, false}, {"allarray", `{.book[*].author}`, []Node{newList(), newField("book"), - newArray([3]ParamsEntry{{0, false}, {0, false}, {0, false}}), newField("author")}}, + newArray([3]ParamsEntry{{0, false}, {0, false}, {0, false}}), newField("author")}, false}, {"wildcard", `{.bicycle.*}`, - []Node{newList(), newField("bicycle"), newWildcard()}}, + []Node{newList(), newField("bicycle"), newWildcard()}, false}, {"filter", `{[?(@.price<3)]}`, []Node{newList(), newFilter(newList(), newList(), "<"), - newList(), newField("price"), newList(), newInt(3)}}, - {"recursive", `{..}`, []Node{newList(), newRecursive()}}, + newList(), newField("price"), newList(), newInt(3)}, false}, + {"recursive", `{..}`, []Node{newList(), newRecursive()}, false}, {"recurField", `{..price}`, - []Node{newList(), newRecursive(), newField("price")}}, + []Node{newList(), newRecursive(), newField("price")}, false}, {"arraydict", `{['book.price']}`, []Node{newList(), newField("book"), newField("price"), - }}, + }, false}, {"union", `{['bicycle.price', 3, 'book.price']}`, []Node{newList(), newUnion([]*ListNode{}), newList(), newField("bicycle"), newField("price"), newList(), newArray([3]ParamsEntry{{3, true}, {4, true}, {0, false}}), newList(), newField("book"), newField("price"), - }}, + }, false}, {"range", `{range .items}{.name},{end}`, []Node{ newList(), newIdentifier("range"), newField("items"), newList(), newField("name"), newText(","), newList(), newIdentifier("end"), - }}, + }, false}, + {"malformat input", `{\\\}`, []Node{}, true}, } func collectNode(nodes []Node, cur Node) []Node { @@ -82,6 +84,12 @@ func collectNode(nodes []Node, cur Node) []Node { func TestParser(t *testing.T) { for _, test := range parserTests { parser, err := Parse(test.name, test.text) + if test.shouldError { + if err == nil { + t.Errorf("unexpected non-error when parsing %s", test.name) + } + continue + } if err != nil { t.Errorf("parse %s error %v", test.name, err) }