Skip to content

Commit

Permalink
pkg/plugin: inject command name from Context so help messages contain
Browse files Browse the repository at this point in the history
the correct command name

pkg/cli: use layout or cliPluginKeys in error messages if no plugin
for a command is found
  • Loading branch information
estroz committed Apr 9, 2020
1 parent 12baa7c commit a067400
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
10 changes: 5 additions & 5 deletions pkg/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (c cli) bindCreateAPI(ctx plugin.Context, cmd *cobra.Command) {
getter = tmpGetter
}
}
if !hasGetter {
err := fmt.Errorf("project version %q does not support an API creation plugin",
c.projectVersion)

cfg, err := config.LoadInitialized()
if err != nil {
cmdErr(cmd, err)
return
}

cfg, err := config.LoadInitialized()
if err != nil {
if !hasGetter {
err := fmt.Errorf("layout plugin %q does not support an API creation plugin", cfg.Layout)
cmdErr(cmd, err)
return
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ func (c cli) bindInit(ctx plugin.Context, cmd *cobra.Command) {
}
}
if !hasGetter {
log.Fatalf("project version %q does not support an initialization plugin",
c.projectVersion)
if len(c.cliPluginKeys) == 0 {
log.Fatalf("project version %q does not support an initialization plugin", c.projectVersion)
} else {
log.Fatalf("plugin %q does not support an initialization plugin", c.cliPluginKeys)
}
}

cfg := config.New(config.DefaultPath)
Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (c cli) bindCreateWebhook(ctx plugin.Context, cmd *cobra.Command) {
getter = tmpGetter
}
}
if !hasGetter {
err := fmt.Errorf("project version %q does not support a webhook creation plugin",
c.projectVersion)

cfg, err := config.LoadInitialized()
if err != nil {
cmdErr(cmd, err)
return
}

cfg, err := config.LoadInitialized()
if err != nil {
if !hasGetter {
err := fmt.Errorf("layout plugin %q does not support a webhook creation plugin", cfg.Layout)
cmdErr(cmd, err)
return
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/plugin/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (

type initPlugin struct {
config *config.Config
// For help text.
commandName string

// boilerplate options
license string
Expand All @@ -57,7 +59,7 @@ var (
_ cmdutil.RunOptions = &initPlugin{}
)

func (p initPlugin) UpdateContext(ctx *plugin.Context) {
func (p *initPlugin) UpdateContext(ctx *plugin.Context) {
ctx.Description = `Initialize a new project including vendor/ directory and Go package directories.
Writes the following files:
Expand All @@ -76,6 +78,8 @@ project will prompt the user to run 'dep ensure' after writing the project files
%s init --project-version=1 --domain example.org --license apache2 --owner "The Kubernetes authors"
`,
ctx.CommandName)

p.commandName = ctx.CommandName
}

func (p *initPlugin) BindFlags(fs *pflag.FlagSet) {
Expand Down Expand Up @@ -177,6 +181,6 @@ func (p *initPlugin) PostScaffold() error {
return err
}

fmt.Println("Next: define a resource with:\n$ kubebuilder create api")
fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName)
return nil
}
8 changes: 6 additions & 2 deletions pkg/plugin/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (

type initPlugin struct {
config *config.Config
// For help text.
commandName string

// boilerplate options
license string
Expand All @@ -55,7 +57,7 @@ var (
_ cmdutil.RunOptions = &initPlugin{}
)

func (p initPlugin) UpdateContext(ctx *plugin.Context) {
func (p *initPlugin) UpdateContext(ctx *plugin.Context) {
ctx.Description = `Initialize a new project including vendor/ directory and Go package directories.
Writes the following files:
Expand All @@ -74,6 +76,8 @@ project will prompt the user to run 'dep ensure' after writing the project files
%s init --project-version=2 --domain example.org --license apache2 --owner "The Kubernetes authors"
`,
ctx.CommandName)

p.commandName = ctx.CommandName
}

func (p *initPlugin) BindFlags(fs *pflag.FlagSet) {
Expand Down Expand Up @@ -176,6 +180,6 @@ func (p *initPlugin) PostScaffold() error {
return err
}

fmt.Println("Next: define a resource with:\n$ kubebuilder create api")
fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName)
return nil
}
11 changes: 7 additions & 4 deletions pkg/plugin/v2/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v2

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
Expand All @@ -33,6 +32,8 @@ import (

type createWebhookPlugin struct {
config *config.Config
// For help text.
commandName string

resource *resource.Options
defaulting bool
Expand All @@ -45,7 +46,7 @@ var (
_ cmdutil.RunOptions = &createAPIPlugin{}
)

func (p createWebhookPlugin) UpdateContext(ctx *plugin.Context) {
func (p *createWebhookPlugin) UpdateContext(ctx *plugin.Context) {
ctx.Description = `Scaffold a webhook for an API resource. You can choose to scaffold defaulting,
validating and (or) conversion webhooks.
`
Expand All @@ -57,6 +58,8 @@ validating and (or) conversion webhooks.
%s create webhook --group crew --version v1 --kind FirstMate --conversion
`,
ctx.CommandName, ctx.CommandName)

p.commandName = ctx.CommandName
}

func (p *createWebhookPlugin) BindFlags(fs *pflag.FlagSet) {
Expand Down Expand Up @@ -88,8 +91,8 @@ func (p *createWebhookPlugin) Validate() error {
}

if !p.defaulting && !p.validation && !p.conversion {
return errors.New("kubebuilder webhook requires at least one of" +
" --defaulting, --programmatic-validation and --conversion to be true")
return fmt.Errorf("%s create webhook requires at least one of --defaulting,"+
" --programmatic-validation and --conversion to be true", p.commandName)
}

return nil
Expand Down

0 comments on commit a067400

Please sign in to comment.