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

✨ Make the edit command to be a plugin. #1691

Merged
merged 1 commit into from
Oct 27, 2020
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
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
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
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())
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
// 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"
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
"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
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
func (c cli) bindCreateWebhook(ctx plugin.Context, cmd *cobra.Command) {
var getter plugin.CreateWebhookPluginGetter
for _, p := range c.resolvedPlugins {
Expand Down
12 changes: 12 additions & 0 deletions pkg/plugin/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ type CreateWebhookPluginGetter interface {
type CreateWebhook interface {
GenericSubcommand
}

prafull01 marked this conversation as resolved.
Show resolved Hide resolved
// EditPluginGetter is an interface that defines gets an Edit plugin
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
type EditPluginGetter interface {
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
Base
// GetEditPlugin returns the underlying Edit interface.
GetEditPlugin() Edit
}

prafull01 marked this conversation as resolved.
Show resolved Hide resolved
// Edit is an interface that represents an `edit` command
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
type Edit interface {
prafull01 marked this conversation as resolved.
Show resolved Hide resolved
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
}
5 changes: 5 additions & 0 deletions pkg/plugin/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ var (
_ plugin.InitPluginGetter = Plugin{}
_ plugin.CreateAPIPluginGetter = Plugin{}
_ plugin.CreateWebhookPluginGetter = Plugin{}
_ plugin.EditPluginGetter = Plugin{}
)

// Plugin defines the plugins operations for the v2 plugin version.
type Plugin struct {
initPlugin
createAPIPlugin
createWebhookPlugin
editPlugin
}

// Name returns the name of the plugin for the v2 which is in this case `go.kubebuilder.io`
Expand All @@ -61,3 +63,6 @@ func (p Plugin) GetCreateAPIPlugin() plugin.CreateAPI { return &p.createAPIPlugi

// GetCreateWebhookPlugin will return the plugin for v2 which is responsible for scaffold webhooks for the project
func (p Plugin) GetCreateWebhookPlugin() plugin.CreateWebhook { return &p.createWebhookPlugin }

// GetEditPlugin will return the plugin for v2 which is responsible for editing the scaffold of the project
func (p Plugin) GetEditPlugin() plugin.Edit { return &p.editPlugin }
Loading