Skip to content

Commit 8ea5dba

Browse files
authored
Merge pull request #1163 from camilamacedo86/fix-issue-1153
add required flags validation for create api command
2 parents 9ca3349 + 2a98119 commit 8ea5dba

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

cmd/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func (o *apiOptions) runAddAPI() {
8989
log.Fatalf("unknown pattern %q", o.pattern)
9090
}
9191

92+
if err := o.apiScaffolder.Validate(); err != nil {
93+
log.Fatalln(err)
94+
}
95+
9296
reader := bufio.NewReader(os.Stdin)
9397
if !o.resourceFlag.Changed {
9498
fmt.Println("Create Resource [y/n]")
@@ -100,10 +104,6 @@ func (o *apiOptions) runAddAPI() {
100104
o.apiScaffolder.DoController = util.Yesno(reader)
101105
}
102106

103-
if err := o.apiScaffolder.Validate(); err != nil {
104-
log.Fatalln(err)
105-
}
106-
107107
fmt.Println("Writing scaffold for you to edit...")
108108

109109
if err := o.apiScaffolder.Scaffold(); err != nil {

pkg/scaffold/resource/resource.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,59 @@ type Resource struct {
5454

5555
// Validate checks the Resource values to make sure they are valid.
5656
func (r *Resource) Validate() error {
57-
if len(r.Group) == 0 {
57+
if r.isGroupEmpty() {
5858
return fmt.Errorf("group cannot be empty")
5959
}
60-
if len(r.Version) == 0 {
60+
if r.isVersionEmpty() {
6161
return fmt.Errorf("version cannot be empty")
6262
}
63-
if len(r.Kind) == 0 {
63+
if r.isKindEmpty() {
6464
return fmt.Errorf("kind cannot be empty")
6565
}
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
7167
if err := IsDNS1123Subdomain(r.Group); err != nil {
7268
return fmt.Errorf("group name is invalid: (%v)", err)
7369
}
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
7871
versionMatch := regexp.MustCompile("^v\\d+(alpha\\d+|beta\\d+)?$")
7972
if !versionMatch.MatchString(r.Version) {
8073
return fmt.Errorf(
8174
"version must match ^v\\d+(alpha\\d+|beta\\d+)?$ (was %s)", r.Version)
8275
}
76+
// Check if the Kind is a valid value
8377
if r.Kind != flect.Pascalize(r.Kind) {
8478
return fmt.Errorf("kind must be PascalCase (expected %s was %s)", flect.Pascalize(r.Kind), r.Kind)
8579
}
8680

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)
8789
return nil
8890
}
8991

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+
90110
// The following code came from "k8s.io/apimachinery/pkg/util/validation"
91111
// If be required the usage of more funcs from this then please replace it for the import
92112
// ---------------------------------------

0 commit comments

Comments
 (0)