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 a28d93e
Show file tree
Hide file tree
Showing 34 changed files with 176 additions and 160 deletions.
5 changes: 3 additions & 2 deletions docs/user/commands/kyverno-json.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## kyverno-json

kyverno-json
kyverno-json is a CLI tool to apply policies to json resources.

### Synopsis

kyverno-json is a CLI tool to apply policies to json resources
kyverno-json is a CLI tool to apply policies to json resources.


```
kyverno-json [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/user/commands/kyverno-json_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ See each sub-command's help for details on how to use the generated script.

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.
* [kyverno-json completion bash](kyverno-json_completion_bash.md) - Generate the autocompletion script for bash
* [kyverno-json completion fish](kyverno-json_completion_fish.md) - Generate the autocompletion script for fish
* [kyverno-json completion powershell](kyverno-json_completion_powershell.md) - Generate the autocompletion script for powershell
Expand Down
2 changes: 1 addition & 1 deletion docs/user/commands/kyverno-json_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ kyverno-json docs [flags]

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.

4 changes: 2 additions & 2 deletions docs/user/commands/kyverno-json_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ kyverno-json jp [flags]

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.
* [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
2 changes: 1 addition & 1 deletion docs/user/commands/kyverno-json_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ kyverno-json scan [flags]

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.

2 changes: 1 addition & 1 deletion docs/user/commands/kyverno-json_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ kyverno-json version [flags]

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json
* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.

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
Loading

0 comments on commit a28d93e

Please sign in to comment.