mirror of https://github.com/k3s-io/k3s
commit
1b437272ba
|
@ -18,6 +18,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"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 {
|
func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command {
|
||||||
options := &ViewOptions{ConfigAccess: ConfigAccess}
|
options := &ViewOptions{ConfigAccess: ConfigAccess}
|
||||||
|
// Default to yaml
|
||||||
|
defaultOutputFormat := "yaml"
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "view",
|
Use: "view",
|
||||||
|
@ -60,6 +63,11 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command {
|
||||||
Example: view_example,
|
Example: view_example,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
options.Complete()
|
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)
|
printer, _, err := cmdutil.PrinterForCommand(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -76,8 +84,7 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdutil.AddPrinterFlags(cmd)
|
cmdutil.AddPrinterFlags(cmd)
|
||||||
// Default to yaml
|
cmd.Flags().Set("output", defaultOutputFormat)
|
||||||
cmd.Flags().Set("output", "yaml")
|
|
||||||
|
|
||||||
options.Merge.Default(true)
|
options.Merge.Default(true)
|
||||||
cmd.Flags().Var(&options.Merge, "merge", "merge together the full hierarchy of kubeconfig files")
|
cmd.Flags().Var(&options.Merge, "merge", "merge together the full hierarchy of kubeconfig files")
|
||||||
|
|
|
@ -63,8 +63,12 @@ func NewParser(name string) *Parser {
|
||||||
// parseAction parsed the expression inside delimiter
|
// parseAction parsed the expression inside delimiter
|
||||||
func parseAction(name, text string) (*Parser, error) {
|
func parseAction(name, text string) (*Parser, error) {
|
||||||
p, err := Parse(name, fmt.Sprintf("%s%s%s", leftDelim, text, rightDelim))
|
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)
|
p.Root = p.Root.Nodes[0].(*ListNode)
|
||||||
return p, err
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) Parse(text string) error {
|
func (p *Parser) Parse(text string) error {
|
||||||
|
|
|
@ -21,44 +21,46 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type parserTest struct {
|
type parserTest struct {
|
||||||
name string
|
name string
|
||||||
text string
|
text string
|
||||||
nodes []Node
|
nodes []Node
|
||||||
|
shouldError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var parserTests = []parserTest{
|
var parserTests = []parserTest{
|
||||||
{"plain", `hello jsonpath`, []Node{newText("hello jsonpath")}},
|
{"plain", `hello jsonpath`, []Node{newText("hello jsonpath")}, false},
|
||||||
{"variable", `hello {.jsonpath}`,
|
{"variable", `hello {.jsonpath}`,
|
||||||
[]Node{newText("hello "), newList(), newField("jsonpath")}},
|
[]Node{newText("hello "), newList(), newField("jsonpath")}, false},
|
||||||
{"arrayfiled", `hello {['jsonpath']}`,
|
{"arrayfiled", `hello {['jsonpath']}`,
|
||||||
[]Node{newText("hello "), newList(), newField("jsonpath")}},
|
[]Node{newText("hello "), newList(), newField("jsonpath")}, false},
|
||||||
{"quote", `{"{"}`, []Node{newList(), newText("{")}},
|
{"quote", `{"{"}`, []Node{newList(), newText("{")}, false},
|
||||||
{"array", `{[1:3]}`, []Node{newList(),
|
{"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}`,
|
{"allarray", `{.book[*].author}`,
|
||||||
[]Node{newList(), newField("book"),
|
[]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.*}`,
|
{"wildcard", `{.bicycle.*}`,
|
||||||
[]Node{newList(), newField("bicycle"), newWildcard()}},
|
[]Node{newList(), newField("bicycle"), newWildcard()}, false},
|
||||||
{"filter", `{[?(@.price<3)]}`,
|
{"filter", `{[?(@.price<3)]}`,
|
||||||
[]Node{newList(), newFilter(newList(), newList(), "<"),
|
[]Node{newList(), newFilter(newList(), newList(), "<"),
|
||||||
newList(), newField("price"), newList(), newInt(3)}},
|
newList(), newField("price"), newList(), newInt(3)}, false},
|
||||||
{"recursive", `{..}`, []Node{newList(), newRecursive()}},
|
{"recursive", `{..}`, []Node{newList(), newRecursive()}, false},
|
||||||
{"recurField", `{..price}`,
|
{"recurField", `{..price}`,
|
||||||
[]Node{newList(), newRecursive(), newField("price")}},
|
[]Node{newList(), newRecursive(), newField("price")}, false},
|
||||||
{"arraydict", `{['book.price']}`, []Node{newList(),
|
{"arraydict", `{['book.price']}`, []Node{newList(),
|
||||||
newField("book"), newField("price"),
|
newField("book"), newField("price"),
|
||||||
}},
|
}, false},
|
||||||
{"union", `{['bicycle.price', 3, 'book.price']}`, []Node{newList(), newUnion([]*ListNode{}),
|
{"union", `{['bicycle.price', 3, 'book.price']}`, []Node{newList(), newUnion([]*ListNode{}),
|
||||||
newList(), newField("bicycle"), newField("price"),
|
newList(), newField("bicycle"), newField("price"),
|
||||||
newList(), newArray([3]ParamsEntry{{3, true}, {4, true}, {0, false}}),
|
newList(), newArray([3]ParamsEntry{{3, true}, {4, true}, {0, false}}),
|
||||||
newList(), newField("book"), newField("price"),
|
newList(), newField("book"), newField("price"),
|
||||||
}},
|
}, false},
|
||||||
{"range", `{range .items}{.name},{end}`, []Node{
|
{"range", `{range .items}{.name},{end}`, []Node{
|
||||||
newList(), newIdentifier("range"), newField("items"),
|
newList(), newIdentifier("range"), newField("items"),
|
||||||
newList(), newField("name"), newText(","),
|
newList(), newField("name"), newText(","),
|
||||||
newList(), newIdentifier("end"),
|
newList(), newIdentifier("end"),
|
||||||
}},
|
}, false},
|
||||||
|
{"malformat input", `{\\\}`, []Node{}, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectNode(nodes []Node, cur Node) []Node {
|
func collectNode(nodes []Node, cur Node) []Node {
|
||||||
|
@ -82,6 +84,12 @@ func collectNode(nodes []Node, cur Node) []Node {
|
||||||
func TestParser(t *testing.T) {
|
func TestParser(t *testing.T) {
|
||||||
for _, test := range parserTests {
|
for _, test := range parserTests {
|
||||||
parser, err := Parse(test.name, test.text)
|
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 {
|
if err != nil {
|
||||||
t.Errorf("parse %s error %v", test.name, err)
|
t.Errorf("parse %s error %v", test.name, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue