Skip to content

Commit

Permalink
✨ Make the edit command to be a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
prafull01 committed Oct 27, 2020
1 parent a2f2398 commit f6a53ea
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 90 deletions.
89 changes: 0 additions & 89 deletions cmd/edit.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func main() {
&pluginv2.Plugin{},
),
cli.WithExtraCommands(
newEditCmd(),
newCompletionCmd(),
version.NewCmd(),
),
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (c cli) newAPIContext() plugin.Context {
return ctx
}

// nolint:dupl
func (c cli) bindCreateAPI(ctx plugin.Context, cmd *cobra.Command) {
var getter plugin.CreateAPIPluginGetter
for _, p := range c.resolvedPlugins {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ func (c cli) buildRootCmd() *cobra.Command {
rootCmd.AddCommand(createCmd)
}

// kubebuilder edit
rootCmd.AddCommand(c.newEditCmd())
// kubebuilder init
rootCmd.AddCommand(c.newInitCmd())

Expand Down
92 changes: 92 additions & 0 deletions pkg/cli/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"fmt"

"github.com/spf13/cobra"

"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/plugin"
)

func (c *cli) newEditCmd() *cobra.Command {
ctx := c.newEditContext()
cmd := &cobra.Command{
Use: "edit",
Short: "This command will edit the project configuration",
Long: ctx.Description,
Example: ctx.Examples,
RunE: errCmdFunc(
fmt.Errorf("project must be initialized"),
),
}

// Lookup the plugin for projectVersion and bind it to the command.
c.bindEdit(ctx, cmd)
return cmd

}

func (c *cli) newEditContext() plugin.Context {
ctx := plugin.Context{
CommandName: c.commandName,
Description: `This command will edit the project configuration. You can have single or multi group project.`,
}

return ctx
}

// nolint:dupl
func (c *cli) bindEdit(ctx plugin.Context, cmd *cobra.Command) {
var getter plugin.EditPluginGetter
for _, p := range c.resolvedPlugins {
tmpGetter, isGetter := p.(plugin.EditPluginGetter)
if isGetter {
if getter != nil {
err := fmt.Errorf("duplicate edit project plugins for project version %q (%s, %s), "+
"use a more specific plugin key", c.projectVersion, plugin.KeyFor(getter), plugin.KeyFor(p))
cmdErr(cmd, err)
return
}
getter = tmpGetter
}
}

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

if getter == nil {
err := fmt.Errorf("layout plugin %q does not support a edit project plugin", cfg.Layout)
cmdErr(cmd, err)
return
}

editProject := getter.GetEditPlugin()
editProject.InjectConfig(&cfg.Config)
editProject.BindFlags(cmd.Flags())
editProject.UpdateContext(&ctx)
cmd.Long = ctx.Description
cmd.Example = ctx.Examples
cmd.RunE = runECmdFunc(cfg, editProject,
fmt.Sprintf("failed to edit project with version %q", c.projectVersion))

}
1 change: 1 addition & 0 deletions pkg/cli/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (c cli) newWebhookContext() plugin.Context {
return ctx
}

// nolint:dupl
func (c cli) bindCreateWebhook(ctx plugin.Context, cmd *cobra.Command) {
var getter plugin.CreateWebhookPluginGetter
for _, p := range c.resolvedPlugins {
Expand Down
10 changes: 10 additions & 0 deletions pkg/plugin/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,13 @@ type CreateWebhookPluginGetter interface {
type CreateWebhook interface {
GenericSubcommand
}

type EditPluginGetter interface {
Base
//GetEditPlugin returns the underlying Edit interface.
GetEditPlugin() Edit
}

type Edit interface {
GenericSubcommand
}
82 changes: 82 additions & 0 deletions pkg/plugin/v2/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import (
"fmt"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/internal/cmdutil"
"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/plugin"
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds"
)

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

multigroup bool
}

var (
_ plugin.Edit = &editPlugin{}
_ cmdutil.RunOptions = &editPlugin{}
)

func (p *editPlugin) UpdateContext(ctx *plugin.Context) {
ctx.Description = `This command will edit the project configuration. You can have single or multi group project.`

ctx.Examples = fmt.Sprintf(`# Enable the multigroup layout
%s edit --multigroup
# Disable the multigroup layout
%s edit --multigroup=false
`, ctx.CommandName, ctx.CommandName)

p.commandName = ctx.CommandName
}

func (p *editPlugin) BindFlags(fs *pflag.FlagSet) {
fs.BoolVar(&p.multigroup, "multigroup", false, "enable or disable multigroup layout")
}

func (p *editPlugin) InjectConfig(c *config.Config) {
// v3 project configs get a 'layout' value.
if c.IsV3() {
c.Layout = plugin.KeyFor(Plugin{})
}
p.config = c
}

func (p *editPlugin) Run() error {
return cmdutil.Run(p)
}

func (p *editPlugin) Validate() error {
return nil
}

func (p *editPlugin) GetScaffolder() (scaffold.Scaffolder, error) {
return scaffolds.NewEditScaffolder(p.config, p.multigroup), nil
}

func (p *editPlugin) PostScaffold() error {
return nil
}
3 changes: 3 additions & 0 deletions pkg/plugin/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ var (
_ plugin.InitPluginGetter = Plugin{}
_ plugin.CreateAPIPluginGetter = Plugin{}
_ plugin.CreateWebhookPluginGetter = Plugin{}
_ plugin.EditPluginGetter = Plugin{}
)

type Plugin struct {
initPlugin
createAPIPlugin
createWebhookPlugin
editPlugin
}

func (Plugin) Name() string { return pluginName }
Expand All @@ -47,3 +49,4 @@ func (Plugin) SupportedProjectVersions() []string { return supported
func (p Plugin) GetInitPlugin() plugin.Init { return &p.initPlugin }
func (p Plugin) GetCreateAPIPlugin() plugin.CreateAPI { return &p.createAPIPlugin }
func (p Plugin) GetCreateWebhookPlugin() plugin.CreateWebhook { return &p.createWebhookPlugin }
func (p Plugin) GetEditPlugin() plugin.Edit { return &p.editPlugin }
Loading

0 comments on commit f6a53ea

Please sign in to comment.