sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/cli/init.go (about) 1 /* 2 Copyright 2020 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 cli 18 19 import ( 20 "fmt" 21 "sort" 22 "strconv" 23 "strings" 24 25 "github.com/spf13/cobra" 26 27 "sigs.k8s.io/kubebuilder/v3/pkg/config" 28 "sigs.k8s.io/kubebuilder/v3/pkg/plugin" 29 ) 30 31 const initErrorMsg = "failed to initialize project" 32 33 func (c CLI) newInitCmd() *cobra.Command { 34 cmd := &cobra.Command{ 35 Use: "init", 36 Short: "Initialize a new project", 37 Long: `Initialize a new project. 38 39 For further help about a specific plugin, set --plugins. 40 `, 41 Example: c.getInitHelpExamples(), 42 Run: func(cmd *cobra.Command, args []string) {}, 43 } 44 45 // Register --project-version on the dynamically created command 46 // so that it shows up in help and does not cause a parse error. 47 cmd.Flags().String(projectVersionFlag, c.defaultProjectVersion.String(), "project version") 48 49 // In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of 50 // this subcommand. This allows the use of subcommands that do not require resolved plugins like help. 51 if len(c.resolvedPlugins) == 0 { 52 cmdErr(cmd, noResolvedPluginError{}) 53 return cmd 54 } 55 56 // Obtain the plugin keys and subcommands from the plugins that implement plugin.Init. 57 subcommands := c.filterSubcommands( 58 func(p plugin.Plugin) bool { 59 _, isValid := p.(plugin.Init) 60 return isValid 61 }, 62 func(p plugin.Plugin) plugin.Subcommand { 63 return p.(plugin.Init).GetInitSubcommand() 64 }, 65 ) 66 67 // Verify that there is at least one remaining plugin. 68 if len(subcommands) == 0 { 69 cmdErr(cmd, noAvailablePluginError{"project initialization"}) 70 return cmd 71 } 72 73 c.applySubcommandHooks(cmd, subcommands, initErrorMsg, true) 74 75 return cmd 76 } 77 78 func (c CLI) getInitHelpExamples() string { 79 var sb strings.Builder 80 for _, version := range c.getAvailableProjectVersions() { 81 rendered := fmt.Sprintf(` # Help for initializing a project with version %[2]s 82 %[1]s init --project-version=%[2]s -h 83 84 `, 85 c.commandName, version) 86 sb.WriteString(rendered) 87 } 88 return strings.TrimSuffix(sb.String(), "\n\n") 89 } 90 91 func (c CLI) getAvailableProjectVersions() (projectVersions []string) { 92 versionSet := make(map[config.Version]struct{}) 93 for _, p := range c.plugins { 94 // Only return versions of non-deprecated plugins. 95 if _, isDeprecated := p.(plugin.Deprecated); !isDeprecated { 96 for _, version := range p.SupportedProjectVersions() { 97 versionSet[version] = struct{}{} 98 } 99 } 100 } 101 for version := range versionSet { 102 projectVersions = append(projectVersions, strconv.Quote(version.String())) 103 } 104 sort.Strings(projectVersions) 105 return projectVersions 106 }