|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "sigs.k8s.io/kubebuilder/v4/pkg/config" |
| 23 | + "sigs.k8s.io/kubebuilder/v4/pkg/machinery" |
| 24 | + "sigs.k8s.io/kubebuilder/v4/pkg/plugin" |
| 25 | + "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha/scaffolds" |
| 26 | +) |
| 27 | + |
| 28 | +var _ plugin.EditSubcommand = &editSubcommand{} |
| 29 | + |
| 30 | +type editSubcommand struct { |
| 31 | + config config.Config |
| 32 | +} |
| 33 | + |
| 34 | +func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) { |
| 35 | + subcmdMeta.Description = metaDataDescription |
| 36 | + |
| 37 | + subcmdMeta.Examples = fmt.Sprintf(` # Edit a common project with this plugin |
| 38 | + %[1]s edit --plugins=%[2]s |
| 39 | +`, cliMeta.CommandName, pluginKey) |
| 40 | +} |
| 41 | + |
| 42 | +func (p *editSubcommand) InjectConfig(c config.Config) error { |
| 43 | + p.config = c |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (p *editSubcommand) PreScaffold(machinery.Filesystem) error { |
| 48 | + if len(p.config.GetCliVersion()) == 0 { |
| 49 | + return fmt.Errorf( |
| 50 | + "you must manually upgrade your project to a version that records the CLI version in PROJECT (`cliVersion`) " + |
| 51 | + "to allow the `alpha update` command to work properly before using this plugin.\n" + |
| 52 | + "More info: https://book.kubebuilder.io/migrations", |
| 53 | + ) |
| 54 | + } |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error { |
| 59 | + if err := insertPluginMetaToConfig(p.config, pluginConfig{}); err != nil { |
| 60 | + return fmt.Errorf("error inserting project plugin meta to configuration: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + scaffolder := scaffolds.NewInitScaffolder() |
| 64 | + scaffolder.InjectFS(fs) |
| 65 | + if err := scaffolder.Scaffold(); err != nil { |
| 66 | + return fmt.Errorf("error scaffolding edit subcommand: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
0 commit comments