Skip to content

Commit

Permalink
add required flags validation for create api command
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Nov 16, 2019
1 parent dc32e46 commit 2a98119
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (o *apiOptions) runAddAPI() {
log.Fatalf("unknown pattern %q", o.pattern)
}

if err := o.apiScaffolder.Validate(); err != nil {
log.Fatalln(err)
}

reader := bufio.NewReader(os.Stdin)
if !o.resourceFlag.Changed {
fmt.Println("Create Resource [y/n]")
Expand All @@ -100,10 +104,6 @@ func (o *apiOptions) runAddAPI() {
o.apiScaffolder.DoController = util.Yesno(reader)
}

if err := o.apiScaffolder.Validate(); err != nil {
log.Fatalln(err)
}

fmt.Println("Writing scaffold for you to edit...")

if err := o.apiScaffolder.Scaffold(); err != nil {
Expand Down
44 changes: 32 additions & 12 deletions pkg/scaffold/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,59 @@ type Resource struct {

// Validate checks the Resource values to make sure they are valid.
func (r *Resource) Validate() error {
if len(r.Group) == 0 {
if r.isGroupEmpty() {
return fmt.Errorf("group cannot be empty")
}
if len(r.Version) == 0 {
if r.isVersionEmpty() {
return fmt.Errorf("version cannot be empty")
}
if len(r.Kind) == 0 {
if r.isKindEmpty() {
return fmt.Errorf("kind cannot be empty")
}

if len(r.Resource) == 0 {
r.Resource = flect.Pluralize(strings.ToLower(r.Kind))
}

// Check if the Group has a valid value for for it
if err := IsDNS1123Subdomain(r.Group); err != nil {
return fmt.Errorf("group name is invalid: (%v)", err)
}

r.GroupImportSafe = strings.Replace(r.Group, "-", "", -1)
r.GroupImportSafe = strings.Replace(r.GroupImportSafe, ".", "", -1)

// Check if the version is a valid value
versionMatch := regexp.MustCompile("^v\\d+(alpha\\d+|beta\\d+)?$")
if !versionMatch.MatchString(r.Version) {
return fmt.Errorf(
"version must match ^v\\d+(alpha\\d+|beta\\d+)?$ (was %s)", r.Version)
}
// Check if the Kind is a valid value
if r.Kind != flect.Pascalize(r.Kind) {
return fmt.Errorf("kind must be PascalCase (expected %s was %s)", flect.Pascalize(r.Kind), r.Kind)
}

// todo: move it for the proper place since they are not validations and then, should not be here
// Add in r.Resource the Kind plural
if len(r.Resource) == 0 {
r.Resource = flect.Pluralize(strings.ToLower(r.Kind))
}
// Replace the caracter "-" for "" to allow scaffold the go imports
r.GroupImportSafe = strings.Replace(r.Group, "-", "", -1)
r.GroupImportSafe = strings.Replace(r.GroupImportSafe, ".", "", -1)
return nil
}

// isKindEmpty will return true if the --kind flag do not be informed
// NOTE: required check if the flags are assuming the other flags as value
func (r *Resource) isKindEmpty() bool {
return len(r.Kind) == 0 || r.Kind == "--group" || r.Kind == "--version"
}

// isVersionEmpty will return true if the --version flag do not be informed
// NOTE: required check if the flags are assuming the other flags as value
func (r *Resource) isVersionEmpty() bool {
return len(r.Version) == 0 || r.Version == "--group" || r.Version == "--kind"
}

// isVersionEmpty will return true if the --group flag do not be informed
// NOTE: required check if the flags are assuming the other flags as value
func (r *Resource) isGroupEmpty() bool {
return len(r.Group) == 0 || r.Group == "--version" || r.Group == "--kind"
}

// The following code came from "k8s.io/apimachinery/pkg/util/validation"
// If be required the usage of more funcs from this then please replace it for the import
// ---------------------------------------
Expand Down

0 comments on commit 2a98119

Please sign in to comment.