Skip to content

Commit

Permalink
Merge pull request #2105 from Adirio/cli-description
Browse files Browse the repository at this point in the history
✨ Allow to modify the root's command description
  • Loading branch information
k8s-ci-robot committed Mar 24, 2021
2 parents e3437d6 + d7bd9c7 commit 2d0bae4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type CLI struct { //nolint:maligned
commandName string
// CLI version string.
version string
// CLI root's command description.
description string
// Plugins registered in the CLI.
plugins map[string]plugin.Plugin
// Default plugins in case none is provided and a config file can't be found.
Expand Down Expand Up @@ -120,7 +122,9 @@ func New(options ...Option) (*CLI, error) {
func newCLI(options ...Option) (*CLI, error) {
// Default CLI options.
c := &CLI{
commandName: "kubebuilder",
commandName: "kubebuilder",
description: `CLI tool for building Kubernetes extensions and tools.
`,
plugins: make(map[string]plugin.Plugin),
defaultPlugins: make(map[config.Version][]string),
fs: machinery.Filesystem{FS: afero.NewOsFs()},
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func WithVersion(version string) Option {
}
}

// WithDescription is an Option that sets the CLI's root description.
func WithDescription(description string) Option {
return func(c *CLI) error {
c.description = description
return nil
}
}

// WithPlugins is an Option that sets the CLI's plugins.
//
// Specifying any invalid plugin results in an error.
Expand Down
10 changes: 10 additions & 0 deletions pkg/cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ var _ = Describe("CLI options", func() {
})
})

Context("WithDescription", func() {
It("should use the provided description string", func() {
description := "alternative description"
c, err = newCLI(WithDescription(description))
Expect(err).NotTo(HaveOccurred())
Expect(c).NotTo(BeNil())
Expect(c.description).To(Equal(description))
})
})

Context("WithPlugins", func() {
It("should return a valid CLI", func() {
c, err = newCLI(WithPlugins(p))
Expand Down
5 changes: 2 additions & 3 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const (

func (c CLI) newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: c.commandName,
Long: `CLI tool for building Kubernetes extensions and tools.
`,
Use: c.commandName,
Long: c.description,
Example: c.rootExamples(),
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
Expand Down

0 comments on commit 2d0bae4

Please sign in to comment.