@@ -57,16 +57,8 @@ func equalStringSlice(a, b []string) bool {
57
57
return true
58
58
}
59
59
60
- // CLI interacts with a command line interface.
61
- type CLI interface {
62
- // Run runs the CLI, usually returning an error if command line configuration
63
- // is incorrect.
64
- Run () error
65
- }
66
-
67
- // cli defines the command line structure and interfaces that are used to
68
- // scaffold kubebuilder project files.
69
- type cli struct { //nolint:maligned
60
+ // CLI is the command line utility that is used to scaffold kubebuilder project files.
61
+ type CLI struct { //nolint:maligned
70
62
/* Fields set by Option */
71
63
72
64
// Root command name. It is injected downstream to provide correct help, usage, examples and errors.
@@ -77,11 +69,11 @@ type cli struct { //nolint:maligned
77
69
defaultProjectVersion config.Version
78
70
// Default plugins in case none is provided and a config file can't be found.
79
71
defaultPlugins map [config.Version ][]string
80
- // Plugins registered in the cli .
72
+ // Plugins registered in the CLI .
81
73
plugins map [string ]plugin.Plugin
82
74
// Commands injected by options.
83
75
extraCommands []* cobra.Command
84
- // Whether to add a completion command to the cli .
76
+ // Whether to add a completion command to the CLI .
85
77
completionCommand bool
86
78
87
79
/* Internal fields */
@@ -98,12 +90,17 @@ type cli struct { //nolint:maligned
98
90
cmd * cobra.Command
99
91
}
100
92
101
- // New creates a new cli instance.
102
- // Developer errors (e.g. not registering any plugins, extra commands with conflicting names) return an error
103
- // while user errors (e.g. errors while parsing flags, unresolvable plugins) create a command which return the error.
104
- func New (opts ... Option ) (CLI , error ) {
93
+ // New creates a new CLI instance.
94
+ //
95
+ // It follows the functional options pattern in order to customize the resulting CLI.
96
+ //
97
+ // It returns an error if any of the provided options fails. As some processing needs
98
+ // to be done, execution errors may be found here. Instead of returning an error, this
99
+ // function will return a valid CLI that errors in Run so that help is provided to the
100
+ // user.
101
+ func New (options ... Option ) (* CLI , error ) {
105
102
// Create the CLI.
106
- c , err := newCLI (opts ... )
103
+ c , err := newCLI (options ... )
107
104
if err != nil {
108
105
return nil , err
109
106
}
@@ -125,20 +122,20 @@ func New(opts ...Option) (CLI, error) {
125
122
return c , nil
126
123
}
127
124
128
- // newCLI creates a default cli instance and applies the provided options.
125
+ // newCLI creates a default CLI instance and applies the provided options.
129
126
// It is as a separate function for test purposes.
130
- func newCLI (opts ... Option ) (* cli , error ) {
131
- // Default cli options.
132
- c := & cli {
127
+ func newCLI (options ... Option ) (* CLI , error ) {
128
+ // Default CLI options.
129
+ c := & CLI {
133
130
commandName : "kubebuilder" ,
134
131
defaultProjectVersion : cfgv3 .Version ,
135
132
defaultPlugins : make (map [config.Version ][]string ),
136
133
plugins : make (map [string ]plugin.Plugin ),
137
134
}
138
135
139
136
// Apply provided options.
140
- for _ , opt := range opts {
141
- if err := opt (c ); err != nil {
137
+ for _ , option := range options {
138
+ if err := option (c ); err != nil {
142
139
return nil , err
143
140
}
144
141
}
@@ -147,7 +144,7 @@ func newCLI(opts ...Option) (*cli, error) {
147
144
}
148
145
149
146
// getInfoFromFlags obtains the project version and plugin keys from flags.
150
- func (c * cli ) getInfoFromFlags () (string , []string , error ) {
147
+ func (c * CLI ) getInfoFromFlags () (string , []string , error ) {
151
148
// Partially parse the command line arguments
152
149
fs := pflag .NewFlagSet ("base" , pflag .ContinueOnError )
153
150
@@ -221,7 +218,7 @@ func getInfoFromConfig(projectConfig config.Config) (config.Version, []string, e
221
218
222
219
// resolveFlagsAndConfigFileConflicts checks if the provided combined input from flags and
223
220
// the config file is valid and uses default values in case some info was not provided.
224
- func (c cli ) resolveFlagsAndConfigFileConflicts (
221
+ func (c CLI ) resolveFlagsAndConfigFileConflicts (
225
222
flagProjectVersionString string ,
226
223
cfgProjectVersion config.Version ,
227
224
flagPlugins , cfgPlugins []string ,
@@ -290,7 +287,7 @@ func (c cli) resolveFlagsAndConfigFileConflicts(
290
287
}
291
288
292
289
// getInfo obtains the project version and plugin keys resolving conflicts among flags and the project config file.
293
- func (c * cli ) getInfo () error {
290
+ func (c * CLI ) getInfo () error {
294
291
// Get project version and plugin info from flags
295
292
flagProjectVersion , flagPlugins , err := c .getInfoFromFlags ()
296
293
if err != nil {
@@ -312,7 +309,7 @@ const unstablePluginMsg = " (plugin version is unstable, there may be an upgrade
312
309
"https://kubebuilder.io/migration/plugin/plugins.html)"
313
310
314
311
// resolve selects from the available plugins those that match the project version and plugin keys provided.
315
- func (c * cli ) resolve () error {
312
+ func (c * CLI ) resolve () error {
316
313
var plugins []plugin.Plugin
317
314
for _ , pluginKey := range c .pluginKeys {
318
315
name , version := plugin .SplitKey (pluginKey )
@@ -402,7 +399,7 @@ func (c *cli) resolve() error {
402
399
403
400
// addSubcommands returns a root command with a subcommand tree reflecting the
404
401
// current project's state.
405
- func (c * cli ) addSubcommands () {
402
+ func (c * CLI ) addSubcommands () {
406
403
// kubebuilder completion
407
404
// Only add completion if requested
408
405
if c .completionCommand {
@@ -432,7 +429,7 @@ func (c *cli) addSubcommands() {
432
429
}
433
430
434
431
// buildCmd creates the underlying cobra command and stores it internally.
435
- func (c * cli ) buildCmd () error {
432
+ func (c * CLI ) buildCmd () error {
436
433
c .cmd = c .newRootCmd ()
437
434
438
435
// Get project version and plugin keys.
@@ -452,7 +449,7 @@ func (c *cli) buildCmd() error {
452
449
}
453
450
454
451
// addExtraCommands adds the additional commands.
455
- func (c * cli ) addExtraCommands () error {
452
+ func (c * CLI ) addExtraCommands () error {
456
453
for _ , cmd := range c .extraCommands {
457
454
for _ , subCmd := range c .cmd .Commands () {
458
455
if cmd .Name () == subCmd .Name () {
@@ -465,15 +462,17 @@ func (c *cli) addExtraCommands() error {
465
462
}
466
463
467
464
// printDeprecationWarnings prints the deprecation warnings of the resolved plugins.
468
- func (c cli ) printDeprecationWarnings () {
465
+ func (c CLI ) printDeprecationWarnings () {
469
466
for _ , p := range c .resolvedPlugins {
470
467
if d , isDeprecated := p .(plugin.Deprecated ); isDeprecated {
471
468
fmt .Printf (noticeColor , fmt .Sprintf (deprecationFmt , d .DeprecationWarning ()))
472
469
}
473
470
}
474
471
}
475
472
476
- // Run implements CLI.Run.
477
- func (c cli ) Run () error {
473
+ // Run executes the CLI utility.
474
+ //
475
+ // If an error is found, command help and examples will be printed.
476
+ func (c CLI ) Run () error {
478
477
return c .cmd .Execute ()
479
478
}
0 commit comments