@@ -54,39 +54,59 @@ type Resource struct {
54
54
55
55
// Validate checks the Resource values to make sure they are valid.
56
56
func (r * Resource ) Validate () error {
57
- if len ( r . Group ) == 0 {
57
+ if r . isGroupEmpty () {
58
58
return fmt .Errorf ("group cannot be empty" )
59
59
}
60
- if len ( r . Version ) == 0 {
60
+ if r . isVersionEmpty () {
61
61
return fmt .Errorf ("version cannot be empty" )
62
62
}
63
- if len ( r . Kind ) == 0 {
63
+ if r . isKindEmpty () {
64
64
return fmt .Errorf ("kind cannot be empty" )
65
65
}
66
-
67
- if len (r .Resource ) == 0 {
68
- r .Resource = flect .Pluralize (strings .ToLower (r .Kind ))
69
- }
70
-
66
+ // Check if the Group has a valid value for for it
71
67
if err := IsDNS1123Subdomain (r .Group ); err != nil {
72
68
return fmt .Errorf ("group name is invalid: (%v)" , err )
73
69
}
74
-
75
- r .GroupImportSafe = strings .Replace (r .Group , "-" , "" , - 1 )
76
- r .GroupImportSafe = strings .Replace (r .GroupImportSafe , "." , "" , - 1 )
77
-
70
+ // Check if the version is a valid value
78
71
versionMatch := regexp .MustCompile ("^v\\ d+(alpha\\ d+|beta\\ d+)?$" )
79
72
if ! versionMatch .MatchString (r .Version ) {
80
73
return fmt .Errorf (
81
74
"version must match ^v\\ d+(alpha\\ d+|beta\\ d+)?$ (was %s)" , r .Version )
82
75
}
76
+ // Check if the Kind is a valid value
83
77
if r .Kind != flect .Pascalize (r .Kind ) {
84
78
return fmt .Errorf ("kind must be PascalCase (expected %s was %s)" , flect .Pascalize (r .Kind ), r .Kind )
85
79
}
86
80
81
+ // todo: move it for the proper place since they are not validations and then, should not be here
82
+ // Add in r.Resource the Kind plural
83
+ if len (r .Resource ) == 0 {
84
+ r .Resource = flect .Pluralize (strings .ToLower (r .Kind ))
85
+ }
86
+ // Replace the caracter "-" for "" to allow scaffold the go imports
87
+ r .GroupImportSafe = strings .Replace (r .Group , "-" , "" , - 1 )
88
+ r .GroupImportSafe = strings .Replace (r .GroupImportSafe , "." , "" , - 1 )
87
89
return nil
88
90
}
89
91
92
+ // isKindEmpty will return true if the --kind flag do not be informed
93
+ // NOTE: required check if the flags are assuming the other flags as value
94
+ func (r * Resource ) isKindEmpty () bool {
95
+ return len (r .Kind ) == 0 || r .Kind == "--group" || r .Kind == "--version"
96
+ }
97
+
98
+ // isVersionEmpty will return true if the --version flag do not be informed
99
+ // NOTE: required check if the flags are assuming the other flags as value
100
+ func (r * Resource ) isVersionEmpty () bool {
101
+ return len (r .Version ) == 0 || r .Version == "--group" || r .Version == "--kind"
102
+ }
103
+
104
+ // isVersionEmpty will return true if the --group flag do not be informed
105
+ // NOTE: required check if the flags are assuming the other flags as value
106
+ func (r * Resource ) isGroupEmpty () bool {
107
+ return len (r .Group ) == 0 || r .Group == "--version" || r .Group == "--kind"
108
+ }
109
+
90
110
// The following code came from "k8s.io/apimachinery/pkg/util/validation"
91
111
// If be required the usage of more funcs from this then please replace it for the import
92
112
// ---------------------------------------
0 commit comments