@@ -25,8 +25,8 @@ import (
25
25
"github.com/spf13/pflag"
26
26
27
27
internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
28
- "sigs.k8s.io/kubebuilder/v3/pkg/internal/validation "
29
- "sigs.k8s.io/kubebuilder/v3/pkg/model/ config"
28
+ "sigs.k8s.io/kubebuilder/v3/pkg/config "
29
+ cfgv3alpha "sigs.k8s.io/kubebuilder/v3/pkg/config/v3alpha "
30
30
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
31
31
)
32
32
@@ -74,9 +74,9 @@ type cli struct { //nolint:maligned
74
74
// CLI version string.
75
75
version string
76
76
// Default project version in case none is provided and a config file can't be found.
77
- defaultProjectVersion string
77
+ defaultProjectVersion config. Version
78
78
// Default plugins in case none is provided and a config file can't be found.
79
- defaultPlugins map [string ][]string
79
+ defaultPlugins map [config. Version ][]string
80
80
// Plugins registered in the cli.
81
81
plugins map [string ]plugin.Plugin
82
82
// Commands injected by options.
@@ -87,7 +87,7 @@ type cli struct { //nolint:maligned
87
87
/* Internal fields */
88
88
89
89
// Project version to scaffold.
90
- projectVersion string
90
+ projectVersion config. Version
91
91
// Plugin keys to scaffold with.
92
92
pluginKeys []string
93
93
@@ -131,8 +131,8 @@ func newCLI(opts ...Option) (*cli, error) {
131
131
// Default cli options.
132
132
c := & cli {
133
133
commandName : "kubebuilder" ,
134
- defaultProjectVersion : internalconfig . DefaultVersion ,
135
- defaultPlugins : make (map [string ][]string ),
134
+ defaultProjectVersion : cfgv3alpha . Version ,
135
+ defaultPlugins : make (map [config. Version ][]string ),
136
136
plugins : make (map [string ]plugin.Plugin ),
137
137
}
138
138
@@ -191,90 +191,98 @@ func (c *cli) getInfoFromFlags() (string, []string, error) {
191
191
}
192
192
193
193
// getInfoFromConfigFile obtains the project version and plugin keys from the project config file.
194
- func getInfoFromConfigFile () (string , []string , error ) {
194
+ func getInfoFromConfigFile () (config. Version , []string , error ) {
195
195
// Read the project configuration file
196
196
projectConfig , err := internalconfig .Read ()
197
197
switch {
198
198
case err == nil :
199
199
case os .IsNotExist (err ):
200
- return "" , nil , nil
200
+ return config. Version {} , nil , nil
201
201
default :
202
- return "" , nil , err
202
+ return config. Version {} , nil , err
203
203
}
204
204
205
205
return getInfoFromConfig (projectConfig )
206
206
}
207
207
208
208
// getInfoFromConfig obtains the project version and plugin keys from the project config.
209
209
// It is extracted from getInfoFromConfigFile for testing purposes.
210
- func getInfoFromConfig (projectConfig * config.Config ) (string , []string , error ) {
210
+ func getInfoFromConfig (projectConfig config.Config ) (config. Version , []string , error ) {
211
211
// Split the comma-separated plugins
212
212
var pluginSet []string
213
- if projectConfig .Layout != "" {
214
- for _ , p := range strings .Split (projectConfig .Layout , "," ) {
213
+ if projectConfig .GetLayout () != "" {
214
+ for _ , p := range strings .Split (projectConfig .GetLayout () , "," ) {
215
215
pluginSet = append (pluginSet , strings .TrimSpace (p ))
216
216
}
217
217
}
218
218
219
- return projectConfig .Version , pluginSet , nil
219
+ return projectConfig .GetVersion () , pluginSet , nil
220
220
}
221
221
222
222
// resolveFlagsAndConfigFileConflicts checks if the provided combined input from flags and
223
223
// the config file is valid and uses default values in case some info was not provided.
224
224
func (c cli ) resolveFlagsAndConfigFileConflicts (
225
- flagProjectVersion , cfgProjectVersion string ,
225
+ flagProjectVersionString string ,
226
+ cfgProjectVersion config.Version ,
226
227
flagPlugins , cfgPlugins []string ,
227
- ) (string , []string , error ) {
228
+ ) (config.Version , []string , error ) {
229
+ // Parse project configuration version from flags
230
+ var flagProjectVersion config.Version
231
+ if flagProjectVersionString != "" {
232
+ if err := flagProjectVersion .Parse (flagProjectVersionString ); err != nil {
233
+ return config.Version {}, nil , fmt .Errorf ("unable to parse project version flag: %w" , err )
234
+ }
235
+ }
236
+
228
237
// Resolve project version
229
- var projectVersion string
238
+ var projectVersion config.Version
239
+ isFlagProjectVersionInvalid := flagProjectVersion .Validate () != nil
240
+ isCfgProjectVersionInvalid := cfgProjectVersion .Validate () != nil
230
241
switch {
231
- // If they are both blank , use the default
232
- case flagProjectVersion == "" && cfgProjectVersion == "" :
242
+ // If they are both invalid (empty is invalid) , use the default
243
+ case isFlagProjectVersionInvalid && isCfgProjectVersionInvalid :
233
244
projectVersion = c .defaultProjectVersion
234
- // If they are equal doesn't matter which we choose
235
- case flagProjectVersion == cfgProjectVersion :
245
+ // If any is invalid (empty is invalid), choose the other
246
+ case isCfgProjectVersionInvalid :
236
247
projectVersion = flagProjectVersion
237
- // If any is blank, choose the other
238
- case cfgProjectVersion == "" :
239
- projectVersion = flagProjectVersion
240
- case flagProjectVersion == "" :
248
+ case isFlagProjectVersionInvalid :
241
249
projectVersion = cfgProjectVersion
242
- // If none is blank and they are different error out
250
+ // If they are equal doesn't matter which we choose
251
+ case flagProjectVersion .Compare (cfgProjectVersion ) == 0 :
252
+ projectVersion = flagProjectVersion
253
+ // If both are valid (empty is invalid) and they are different error out
243
254
default :
244
- return "" , nil , fmt .Errorf ("project version conflict between command line args (%s) " +
245
- "and project configuration file (%s)" , flagProjectVersion , cfgProjectVersion )
246
- }
247
- // It still may be empty if default, flag and config project versions are empty
248
- if projectVersion != "" {
249
- // Validate the project version
250
- if err := validation .ValidateProjectVersion (projectVersion ); err != nil {
251
- return "" , nil , err
252
- }
255
+ return config.Version {}, nil , fmt .Errorf ("project version conflict between command line args (%s) " +
256
+ "and project configuration file (%s)" , flagProjectVersionString , cfgProjectVersion )
253
257
}
254
258
255
259
// Resolve plugins
256
260
var plugins []string
261
+ isFlagPluginsEmpty := len (flagPlugins ) == 0
262
+ isCfgPluginsEmpty := len (cfgPlugins ) == 0
257
263
switch {
258
- // If they are both blank, use the default
259
- case len (flagPlugins ) == 0 && len (cfgPlugins ) == 0 :
260
- plugins = c .defaultPlugins [projectVersion ]
264
+ // If they are both empty, use the default
265
+ case isFlagPluginsEmpty && isCfgPluginsEmpty :
266
+ if defaults , hasDefaults := c .defaultPlugins [projectVersion ]; hasDefaults {
267
+ plugins = defaults
268
+ }
269
+ // If any is empty, choose the other
270
+ case isCfgPluginsEmpty :
271
+ plugins = flagPlugins
272
+ case isFlagPluginsEmpty :
273
+ plugins = cfgPlugins
261
274
// If they are equal doesn't matter which we choose
262
275
case equalStringSlice (flagPlugins , cfgPlugins ):
263
276
plugins = flagPlugins
264
- // If any is blank, choose the other
265
- case len (cfgPlugins ) == 0 :
266
- plugins = flagPlugins
267
- case len (flagPlugins ) == 0 :
268
- plugins = cfgPlugins
269
- // If none is blank and they are different error out
277
+ // If none is empty and they are different error out
270
278
default :
271
- return "" , nil , fmt .Errorf ("plugins conflict between command line args (%v) " +
279
+ return config. Version {} , nil , fmt .Errorf ("plugins conflict between command line args (%v) " +
272
280
"and project configuration file (%v)" , flagPlugins , cfgPlugins )
273
281
}
274
282
// Validate the plugins
275
283
for _ , p := range plugins {
276
284
if err := plugin .ValidateKey (p ); err != nil {
277
- return "" , nil , err
285
+ return config. Version {} , nil , err
278
286
}
279
287
}
280
288
@@ -315,8 +323,8 @@ func (c *cli) resolve() error {
315
323
// under no support contract. However users should be notified _why_ their plugin cannot be found.
316
324
var extraErrMsg string
317
325
if version != "" {
318
- ver , err := plugin .ParseVersion ( version )
319
- if err != nil {
326
+ var ver plugin.Version
327
+ if err := ver . Parse ( version ); err != nil {
320
328
return fmt .Errorf ("error parsing input plugin version from key %q: %v" , pluginKey , err )
321
329
}
322
330
if ! ver .IsStable () {
0 commit comments