-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Moved the edit command to the plugin for v3+
- Loading branch information
Showing
11 changed files
with
275 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.