Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow to modify the root's command description #2105

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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