Skip to content

Commit

Permalink
fix: generated cli examples
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Oct 6, 2023
1 parent fc713b1 commit 0dce31a
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 144 deletions.
2 changes: 1 addition & 1 deletion docs/user/commands/kyverno-json_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ kyverno-json jp [flags]

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json jp function](kyverno-json_jp_function.md) - Provides function informations.
* [kyverno-json jp parse](kyverno-json_jp_parse.md) - Parses jmespath expression and shows corresponding AST.
* [kyverno-json jp parse](kyverno-json_jp_parse.md) - Parses jmespath expression and prints corresponding AST.
* [kyverno-json jp query](kyverno-json_jp_query.md) - Provides a command-line interface to JMESPath, enhanced with Kyverno specific custom functions.

4 changes: 2 additions & 2 deletions docs/user/commands/kyverno-json_jp_function.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ kyverno-json jp function [function_name]... [flags]

```
# List functions
jp function
kyverno-json jp function
# Get function infos
jp function truncate
kyverno-json jp function truncate
```

Expand Down
22 changes: 20 additions & 2 deletions docs/user/commands/kyverno-json_jp_parse.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
## kyverno-json jp parse

Parses jmespath expression and shows corresponding AST.
Parses jmespath expression and prints corresponding AST.

### Synopsis

Parses jmespath expression and shows corresponding AST.
Parses jmespath expression and prints corresponding AST.


```
kyverno-json jp parse [-f file|expression]... [flags]
```

### Examples

```
# Parse expression
kyverno-json jp parse 'request.object.metadata.name | truncate(@, `9`)'
# Parse expression from a file
kyverno-json jp parse -f my-file
# Parse expression from stdin
kyverno-json jp parse
# Parse multiple expressionxs
kyverno-json jp parse -f my-file1 -f my-file-2 'request.object.metadata.name | truncate(@, `9`)'
```

### Options

```
Expand Down
15 changes: 15 additions & 0 deletions docs/user/commands/kyverno-json_jp_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ Provides a command-line interface to JMESPath, enhanced with Kyverno specific cu

Provides a command-line interface to JMESPath, enhanced with Kyverno specific custom functions.


```
kyverno-json jp query [-i input] [-q query|query]... [flags]
```

### Examples

```
# Evaluate query
kyverno-json jp query -i object.yaml 'request.object.metadata.name | truncate(@, `9`)'
# Evaluate query
kyverno-json jp query -i object.yaml -q query-file
# Evaluate multiple queries
kyverno-json jp query -i object.yaml -q query-file-1 -q query-file-2 'request.object.metadata.name | truncate(@, `9`)'
```

### Options

```
Expand Down
23 changes: 5 additions & 18 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package command

import (
"strings"

"github.com/spf13/cobra"
)

type Command struct {
parent *cobra.Command
parents []string
experimental bool
description []string
websiteUrl string
examples []Example
}

func new(parent *cobra.Command, experimental bool, options ...option) Command {
cmd := Command{
parent: parent,
experimental: experimental,
}
func New(options ...option) Command {
var cmd Command
for _, opt := range options {
if opt != nil {
opt(&cmd)
Expand All @@ -27,14 +22,6 @@ func new(parent *cobra.Command, experimental bool, options ...option) Command {
return cmd
}

func New(parent *cobra.Command, options ...option) Command {
return new(parent, false, options...)
}

func NewExperimental(parent *cobra.Command, options ...option) Command {
return new(parent, true, options...)
}

func Description(c Command, short bool) string {
if len(c.description) == 0 {
return ""
Expand All @@ -59,8 +46,8 @@ func Examples(c Command) string {
return ""
}
var useLine string
if c.parent != nil {
useLine = c.parent.UseLine() + " "
if len(c.parents) != 0 {
useLine = strings.Join(c.parents, " ") + " "
}
var lines []string
for _, example := range c.examples {
Expand Down
12 changes: 12 additions & 0 deletions pkg/command/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ func WithExample(title, command string) option {
})
}
}

func WithExperimental(experimental bool) option {
return func(d *Command) {
d.experimental = experimental
}
}

func WithParents(parents ...string) option {
return func(d *Command) {
d.parents = parents
}
}
14 changes: 3 additions & 11 deletions pkg/commands/docs/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/spf13/cobra"
)

func Command(parent *cobra.Command) *cobra.Command {
func Command(parents ...string) *cobra.Command {
var options options
doc := command.New(
parent,
command.WithParents(parents...),
command.WithDescription(
"Generates reference documentation.",
"The docs command generates CLI reference documentation.",
Expand All @@ -33,15 +33,7 @@ func Command(parent *cobra.Command) *cobra.Command {
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, _ []string) error {
root := parent
if root != nil {
for {
if !root.HasParent() {
break
}
root = root.Parent()
}
}
root := cmd.Root()
if err := options.validate(root); err != nil {
return err
}
Expand Down
17 changes: 4 additions & 13 deletions pkg/commands/docs/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,18 @@ import (
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestCommandWithNilRoot(t *testing.T) {
cmd := Command(nil)
assert.NotNil(t, cmd)
cmd.SetArgs([]string{"-o", "foo"})
err := cmd.Execute()
assert.Error(t, err)
}

func TestCommandWithoutArgs(t *testing.T) {
cmd := Command(&cobra.Command{})
cmd := Command()
assert.NotNil(t, cmd)
err := cmd.Execute()
assert.Error(t, err)
}

func TestCommandWithInvalidArg(t *testing.T) {
cmd := Command(&cobra.Command{})
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
Expand All @@ -40,7 +31,7 @@ func TestCommandWithInvalidArg(t *testing.T) {
}

func TestCommandWithInvalidFlag(t *testing.T) {
cmd := Command(&cobra.Command{})
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
Expand All @@ -54,7 +45,7 @@ func TestCommandWithInvalidFlag(t *testing.T) {
}

func TestCommandHelp(t *testing.T) {
cmd := Command(&cobra.Command{})
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetOut(b)
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/jp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/spf13/cobra"
)

func Command(parent *cobra.Command) *cobra.Command {
func Command(parents ...string) *cobra.Command {
doc := command.New(
parent,
command.WithParents(parents...),
command.WithDescription("Provides a command-line interface to JMESPath, enhanced with custom functions."),
command.WithExample("List functions", "jp function"),
command.WithExample("Evaluate query", "jp query -i object.yaml 'request.object.metadata.name | truncate(@, `9`)'"),
Expand All @@ -28,9 +28,9 @@ func Command(parent *cobra.Command) *cobra.Command {
},
}
cmd.AddCommand(
function.Command(cmd),
parse.Command(),
query.Command(),
function.Command(append(parents, "jp")...),
parse.Command(append(parents, "jp")...),
query.Command(append(parents, "jp")...),
)
return cmd
}
10 changes: 5 additions & 5 deletions pkg/commands/jp/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
)

func TestCommand(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
err := cmd.Execute()
assert.NoError(t, err)
}

func TestCommandWithArgs(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{"foo"})
err := cmd.Execute()
assert.Error(t, err)
}

func TestCommandWithInvalidArg(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
Expand All @@ -39,7 +39,7 @@ func TestCommandWithInvalidArg(t *testing.T) {
}

func TestCommandWithInvalidFlag(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
Expand All @@ -53,7 +53,7 @@ func TestCommandWithInvalidFlag(t *testing.T) {
}

func TestCommandHelp(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetOut(b)
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/jp/function/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
)

func Command(parent *cobra.Command) *cobra.Command {
func Command(parents ...string) *cobra.Command {
doc := command.New(
parent,
command.WithParents(parents...),
command.WithDescription("Provides function informations."),
command.WithExample("List functions", "function"),
command.WithExample("Get function infos", "function truncate"),
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/jp/function/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import (
)

func TestCommand(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
err := cmd.Execute()
assert.NoError(t, err)
}

func TestCommandWithOneArg(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{"truncate"})
err := cmd.Execute()
assert.NoError(t, err)
}

func TestCommandWithArgs(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
cmd.SetArgs([]string{"truncate", "to_upper"})
err := cmd.Execute()
assert.NoError(t, err)
}

func TestCommandWithInvalidFlag(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetErr(b)
Expand All @@ -47,7 +47,7 @@ func TestCommandWithInvalidFlag(t *testing.T) {
}

func TestCommandHelp(t *testing.T) {
cmd := Command(nil)
cmd := Command()
assert.NotNil(t, cmd)
b := bytes.NewBufferString("")
cmd.SetOut(b)
Expand Down
16 changes: 13 additions & 3 deletions pkg/commands/jp/parse/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import (
"path/filepath"

"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/kyverno-json/pkg/command"
"github.com/spf13/cobra"
)

func Command() *cobra.Command {
func Command(parents ...string) *cobra.Command {
doc := command.New(
command.WithParents(parents...),
command.WithDescription("Parses jmespath expression and prints corresponding AST."),
command.WithExample("Parse expression", "parse 'request.object.metadata.name | truncate(@, `9`)'"),
command.WithExample("Parse expression from a file", "parse -f my-file"),
command.WithExample("Parse expression from stdin", "parse"),
command.WithExample("Parse multiple expressionxs", "parse -f my-file1 -f my-file-2 'request.object.metadata.name | truncate(@, `9`)'"),
)
var files []string
cmd := &cobra.Command{
Use: "parse [-f file|expression]...",
Short: "Parses jmespath expression and shows corresponding AST.",
Long: "Parses jmespath expression and shows corresponding AST.",
Short: command.Description(doc, true),
Long: command.Description(doc, false),
Example: command.Examples(doc),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
expressions, err := loadExpressions(cmd, args, files)
Expand Down
Loading

0 comments on commit 0dce31a

Please sign in to comment.