@@ -14,77 +14,58 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package cli // nolint:dupl
17
+ package cli //nolint:dupl
18
18
19
19
import (
20
20
"fmt"
21
21
22
22
"github.com/spf13/cobra"
23
23
24
- yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
25
24
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
26
25
)
27
26
27
+ const apiErrorMsg = "failed to create API"
28
+
28
29
func (c CLI ) newCreateAPICmd () * cobra.Command {
29
- ctx := c .newAPIContext ()
30
30
cmd := & cobra.Command {
31
- Use : "api" ,
32
- Short : "Scaffold a Kubernetes API" ,
33
- Long : ctx . Description ,
34
- Example : ctx . Examples ,
31
+ Use : "api" ,
32
+ Short : "Scaffold a Kubernetes API" ,
33
+ Long : `Scaffold a Kubernetes API.
34
+ ` ,
35
35
RunE : errCmdFunc (
36
36
fmt .Errorf ("api subcommand requires an existing project" ),
37
37
),
38
38
}
39
39
40
- // Lookup the plugin for projectVersion and bind it to the command.
41
- c .bindCreateAPI (ctx , cmd )
42
- return cmd
43
- }
44
-
45
- func (c CLI ) newAPIContext () plugin.Context {
46
- return plugin.Context {
47
- CommandName : c .commandName ,
48
- Description : `Scaffold a Kubernetes API.
49
- ` ,
50
- }
51
- }
52
-
53
- // nolint:dupl
54
- func (c CLI ) bindCreateAPI (ctx plugin.Context , cmd * cobra.Command ) {
40
+ // In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
41
+ // this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
55
42
if len (c .resolvedPlugins ) == 0 {
56
- cmdErr (cmd , fmt . Errorf ( noPluginError ) )
57
- return
43
+ cmdErr (cmd , noResolvedPluginError {} )
44
+ return cmd
58
45
}
59
46
60
- var createAPIPlugin plugin.CreateAPI
61
- for _ , p := range c .resolvedPlugins {
62
- tmpPlugin , isValid := p .(plugin.CreateAPI )
63
- if isValid {
64
- if createAPIPlugin != nil {
65
- err := fmt .Errorf ("duplicate API creation plugins (%s, %s), use a more specific plugin key" ,
66
- plugin .KeyFor (createAPIPlugin ), plugin .KeyFor (p ))
67
- cmdErr (cmd , err )
68
- return
69
- }
70
- createAPIPlugin = tmpPlugin
71
- }
72
- }
47
+ // Obtain the plugin keys and subcommands from the plugins that implement plugin.CreateAPI.
48
+ pluginKeys , subcommands := c .filterSubcommands (
49
+ func (p plugin.Plugin ) bool {
50
+ _ , isValid := p .(plugin.CreateAPI )
51
+ return isValid
52
+ },
53
+ func (p plugin.Plugin ) plugin.Subcommand {
54
+ return p .(plugin.CreateAPI ).GetCreateAPISubcommand ()
55
+ },
56
+ )
73
57
74
- if createAPIPlugin == nil {
75
- cmdErr (cmd , fmt .Errorf ("resolved plugins do not provide an API creation plugin: %v" , c .pluginKeys ))
76
- return
58
+ // Verify that there is at least one remaining plugin.
59
+ if len (* subcommands ) == 0 {
60
+ cmdErr (cmd , noAvailablePluginError {"API creation" })
61
+ return cmd
77
62
}
78
63
79
- subcommand := createAPIPlugin . GetCreateAPISubcommand ()
80
- subcommand . BindFlags (cmd . Flags () )
81
- subcommand . UpdateContext ( & ctx )
82
- cmd . Long = ctx . Description
83
- cmd .Example = ctx . Examples
64
+ // Initialization methods.
65
+ options := c . initializationMethods (cmd , subcommands )
66
+
67
+ // Execution methods.
68
+ cmd .PreRunE , cmd . RunE , cmd . PostRunE = c . executionMethodsFuncs ( pluginKeys , subcommands , options , apiErrorMsg )
84
69
85
- cfg := yamlstore .New (c .fs )
86
- msg := fmt .Sprintf ("failed to create API with %q" , plugin .KeyFor (createAPIPlugin ))
87
- cmd .PreRunE = preRunECmdFunc (subcommand , cfg , msg )
88
- cmd .RunE = runECmdFunc (c .fs , subcommand , msg )
89
- cmd .PostRunE = postRunECmdFunc (cfg , msg )
70
+ return cmd
90
71
}
0 commit comments