• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 16522004449

25 Jul 2025 12:30PM UTC coverage: 64.151%. Remained the same
16522004449

Pull #4898

github

web-flow
Merge branch 'master' into version/refactor-add-tests
Pull Request #4898: ✨ Improve version command output: add runtime fallbacks and unit tests.

2627 of 4095 relevant lines covered (64.15%)

13.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.95
/pkg/cli/init.go
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/v4/pkg/config"
28
        "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
29
)
30

31
const initErrorMsg = "failed to initialize project"
32

33
func (c CLI) newInitCmd() *cobra.Command {
10✔
34
        cmd := &cobra.Command{
10✔
35
                Use:   "init",
10✔
36
                Short: "Initialize a new project",
10✔
37
                Long: `Initialize a new project.
10✔
38

10✔
39
For further help about a specific plugin, set --plugins.
10✔
40
`,
10✔
41
                Example: c.getInitHelpExamples(),
10✔
42
                Run:     func(_ *cobra.Command, _ []string) {},
10✔
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")
10✔
48

10✔
49
        // In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
10✔
50
        // this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
10✔
51
        if len(c.resolvedPlugins) == 0 {
13✔
52
                cmdErr(cmd, noResolvedPluginError{})
3✔
53
                return cmd
3✔
54
        }
3✔
55

56
        // Obtain the plugin keys and subcommands from the plugins that implement plugin.Init.
57
        subcommands := c.filterSubcommands(
7✔
58
                func(p plugin.Plugin) bool {
14✔
59
                        _, isValid := p.(plugin.Init)
7✔
60
                        return isValid
7✔
61
                },
7✔
62
                func(p plugin.Plugin) plugin.Subcommand {
6✔
63
                        return p.(plugin.Init).GetInitSubcommand()
6✔
64
                },
6✔
65
        )
66

67
        // Verify that there is at least one remaining plugin.
68
        if len(subcommands) == 0 {
8✔
69
                cmdErr(cmd, noAvailablePluginError{"project initialization"})
1✔
70
                return cmd
1✔
71
        }
1✔
72

73
        c.applySubcommandHooks(cmd, subcommands, initErrorMsg, true)
6✔
74

6✔
75
        return cmd
6✔
76
}
77

78
func (c CLI) getInitHelpExamples() string {
10✔
79
        var sb strings.Builder
10✔
80
        for _, version := range c.getAvailableProjectVersions() {
10✔
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")
10✔
89
}
90

91
func (c CLI) getAvailableProjectVersions() (projectVersions []string) {
10✔
92
        versionSet := make(map[config.Version]struct{})
10✔
93
        for _, p := range c.plugins {
17✔
94
                // Only return versions of non-deprecated plugins.
7✔
95
                if _, isDeprecated := p.(plugin.Deprecated); !isDeprecated {
7✔
96
                        for _, version := range p.SupportedProjectVersions() {
×
97
                                versionSet[version] = struct{}{}
×
98
                        }
×
99
                }
100
        }
101
        for version := range versionSet {
10✔
102
                projectVersions = append(projectVersions, strconv.Quote(version.String()))
×
103
        }
×
104
        sort.Strings(projectVersions)
10✔
105
        return projectVersions
10✔
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc