Skip to content

Commit

Permalink
add validation for the name of the project in the init
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Nov 11, 2019
1 parent ae06fc8 commit 563c037
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cmd/init_project.go
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -137,6 +138,20 @@ func (o *projectOptions) validate() error {
return err
}
}

// use directory name as prefix
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error to get the current path: %v", err)
}

// check if the name of th project pass is a valid name for k8s objects
// it will be used to create the namespace
projectName := filepath.Base(dir)
if err := util.IsValidName(strings.ToLower(projectName)); err != nil {
return fmt.Errorf("project name (%v) is invalid: (%v)", projectName, err)
}

if o.project.Repo == "" {
repoPath, err := findCurrentRepo()
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions cmd/util/validations.go
@@ -0,0 +1,51 @@
package util

import (
"fmt"
"regexp"
)

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

const (
qnameCharFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
// The value is 56 because it will be contact with "-system" = 63
qualifiedNameMaxLength int = 56
)
var qualifiedNameRegexp = regexp.MustCompile("^" + qnameCharFmt + "$")

//IsValidName used to check the name of the project
func IsValidName(value string) []string {
var errs []string
if len(value) > qualifiedNameMaxLength {
errs = append(errs, MaxLenError(qualifiedNameMaxLength))
}
if !qualifiedNameRegexp.MatchString(value) {
errs = append(errs, RegexError("invalid value for project name", qnameCharFmt))
}
return errs
}

// RegexError returns a string explanation of a regex validation failure.
func RegexError(msg string, fmt string, examples ...string) string {
if len(examples) == 0 {
return msg + " (regex used for validation is '" + fmt + "')"
}
msg += " (e.g. "
for i := range examples {
if i > 0 {
msg += " or "
}
msg += "'" + examples[i] + "', "
}
msg += "regex used for validation is '" + fmt + "')"
return msg
}

// MaxLenError returns a string explanation of a "string too long" validation
// failure.
func MaxLenError(length int) string {
return fmt.Sprintf("must be no more than %d characters", length)
}
3 changes: 2 additions & 1 deletion pkg/scaffold/project/kustomize.go
Expand Up @@ -19,6 +19,7 @@ package project
import (
"os"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)
Expand All @@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) {
if err != nil {
return input.Input{}, err
}
c.Prefix = filepath.Base(dir)
c.Prefix = strings.ToLower(filepath.Base(dir))
}
c.TemplateBody = kustomizeTemplate
c.Input.IfExistsAction = input.Error
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v2/kustomize.go
Expand Up @@ -19,6 +19,7 @@ package v2
import (
"os"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)
Expand All @@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) {
if err != nil {
return input.Input{}, err
}
c.Prefix = filepath.Base(dir)
c.Prefix = strings.ToLower(filepath.Base(dir))
}
c.TemplateBody = kustomizeTemplate
c.Input.IfExistsAction = input.Error
Expand Down

0 comments on commit 563c037

Please sign in to comment.