Skip to content

Commit 5f247a5

Browse files
committed
improve CLI with default values
improve doc generator for CLI
1 parent c479c74 commit 5f247a5

29 files changed

Lines changed: 1008 additions & 414 deletions

cmd/internal/gen-cli-docs/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func main() {
99
cmd := mokapi.NewCmdMokapi()
10-
err := cmd.GenMarkdown("./docs/cli")
10+
err := cmd.GenMarkdown("./docs/configuration/static")
1111
if err != nil {
1212
fmt.Println(err.Error())
1313
}

config/static/static_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
type Config struct {
1616
Log MokApiLog `json:"log" yaml:"log"`
17-
ConfigFile string `json:"-" yaml:"-" flag:"config-file"`
17+
ConfigFile string `json:"-" yaml:"-" flag:"cli-input"`
1818
Providers Providers `json:"providers" yaml:"providers"`
1919
Api Api `json:"api" yaml:"api"`
2020
RootCaCert tls.FileOrContent `json:"rootCaCert" yaml:"rootCaCert" name:"root-ca-cert"`

pkg/cli/clitest/reader.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package clitest
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
type TestFileReader struct {
9+
Files map[string][]byte
10+
}
11+
12+
func (t *TestFileReader) ReadFile(name string) ([]byte, error) {
13+
if b, ok := t.Files[name]; ok {
14+
return b, nil
15+
}
16+
// if test is executed on windows we get second path
17+
name = strings.ReplaceAll(name, "\\", "/")
18+
if b, ok := t.Files[name]; ok {
19+
return b, nil
20+
}
21+
return nil, os.ErrNotExist
22+
}
23+
24+
func (t *TestFileReader) FileExists(name string) bool {
25+
if _, ok := t.Files[name]; ok {
26+
return true
27+
}
28+
name = strings.ReplaceAll(name, "\\", "/")
29+
_, ok := t.Files[name]
30+
return ok
31+
}

pkg/cli/command.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ func (c *Command) Execute() error {
5050
}
5151
}
5252

53-
if !cmd.Flags().IsValidFlag("help") {
54-
cmd.Flags().Bool("help", false, "Show help information and exit")
55-
}
56-
57-
if cmd.Version != "" {
58-
if !cmd.Flags().IsValidFlag("version") {
59-
cmd.Flags().Bool("version", false, "Show version information and exit")
60-
}
61-
}
62-
6353
positional, err := parseFlags(args, envPrefix, cmd.Flags())
6454
if err != nil {
6555
return err
@@ -116,6 +106,12 @@ func (c *Command) Flags() *FlagSet {
116106
c.configFile = s
117107
},
118108
}
109+
110+
c.Flags().Bool("help", false, FlagDoc{Short: "Show help information and exit"})
111+
112+
if c.Version != "" {
113+
c.Flags().Bool("version", false, FlagDoc{Short: "Show version information and exit"})
114+
}
119115
}
120116
return c.flags
121117
}

pkg/cli/command_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestCommand(t *testing.T) {
2323
name: "--foo",
2424
cmd: func() *Command {
2525
c := &Command{Name: "foo"}
26-
c.Flags().Bool("foo", false, "")
26+
c.Flags().Bool("foo", false, FlagDoc{})
2727
return c
2828
},
2929
args: []string{"--foo"},
@@ -37,7 +37,7 @@ func TestCommand(t *testing.T) {
3737
name: "-f",
3838
cmd: func() *Command {
3939
c := &Command{Name: "foo"}
40-
c.Flags().BoolShort("foo", "f", false, "")
40+
c.Flags().BoolShort("foo", "f", false, FlagDoc{})
4141
return c
4242
},
4343
args: []string{"-f"},
@@ -51,7 +51,7 @@ func TestCommand(t *testing.T) {
5151
name: "bind to config",
5252
cmd: func() *Command {
5353
c := &Command{Config: &config{}}
54-
c.Flags().Bool("flag", false, "")
54+
c.Flags().Bool("flag", false, FlagDoc{})
5555
return c
5656
},
5757
args: []string{"--flag"},
@@ -65,7 +65,7 @@ func TestCommand(t *testing.T) {
6565
name: "--count",
6666
cmd: func() *Command {
6767
c := &Command{Config: &config{}}
68-
c.Flags().Int("count", 12, "")
68+
c.Flags().Int("count", 12, FlagDoc{})
6969
return c
7070
},
7171
args: []string{"--count", "10"},
@@ -78,7 +78,7 @@ func TestCommand(t *testing.T) {
7878
name: "--count default",
7979
cmd: func() *Command {
8080
c := &Command{Config: &config{}}
81-
c.Flags().Int("count", 12, "")
81+
c.Flags().Int("count", 12, FlagDoc{})
8282
return c
8383
},
8484
args: []string{},
@@ -91,7 +91,7 @@ func TestCommand(t *testing.T) {
9191
name: "--skip-prefix",
9292
cmd: func() *Command {
9393
c := &Command{Config: &config{}}
94-
c.Flags().StringSlice("skip-prefix", []string{"_"}, "", false)
94+
c.Flags().StringSlice("skip-prefix", []string{"_"}, false, FlagDoc{})
9595
return c
9696
},
9797
args: []string{"--skip-prefix", "_", "foo_"},
@@ -105,7 +105,7 @@ func TestCommand(t *testing.T) {
105105
name: "--skip-prefix default",
106106
cmd: func() *Command {
107107
c := &Command{Config: &config{}}
108-
c.Flags().StringSlice("skip-prefix", []string{"_"}, "", false)
108+
c.Flags().StringSlice("skip-prefix", []string{"_"}, false, FlagDoc{})
109109
return c
110110
},
111111
args: []string{},

0 commit comments

Comments
 (0)