Skip to content

Commit

Permalink
Merge pull request #1745 from Adirio/regex
Browse files Browse the repository at this point in the history
👻 Enhance regex patterns
  • Loading branch information
k8s-ci-robot committed Oct 29, 2020
2 parents 67f91f7 + 8f88e45 commit 6a639d5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/internal/validation/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
// "k8s.io/apimachinery" is needed, re-consider whether to add the dependency.

const (
dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
dns1123LabelFmt string = "[a-z0-9](?:[-a-z0-9]*[a-z0-9])?"
dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
dns1035LabelFmt string = "[a-z](?:[-a-z0-9]*[a-z0-9])?"
)

type dnsValidationConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/validation/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// projectVersionFmt defines the project version format from a project config.
const projectVersionFmt string = "[1-9][0-9]*(-(alpha|beta))?"
const projectVersionFmt string = "[1-9][0-9]*(?:-(?:alpha|beta))?"

var projectVersionRe = regexp.MustCompile("^" + projectVersionFmt + "$")

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/resource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const (
versionPattern = "^v\\d+(alpha\\d+|beta\\d+)?$"
versionPattern = "^v\\d+(?:alpha\\d+|beta\\d+)?$"
groupRequired = "group cannot be empty"
versionRequired = "version cannot be empty"
kindRequired = "kind cannot be empty"
Expand Down
20 changes: 10 additions & 10 deletions pkg/model/resource/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ var _ = Describe("Resource Options", func() {
options := &Options{Group: "crew", Version: "1", Kind: "FirstMate"}
Expect(options.Validate()).NotTo(Succeed())
Expect(options.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was 1)`))

options = &Options{Group: "crew", Version: "1beta1", Kind: "FirstMate"}
Expect(options.Validate()).NotTo(Succeed())
Expect(options.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1beta1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was 1beta1)`))

options = &Options{Group: "crew", Version: "a1beta1", Kind: "FirstMate"}
Expect(options.Validate()).NotTo(Succeed())
Expect(options.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was a1beta1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was a1beta1)`))

options = &Options{Group: "crew", Version: "v1beta", Kind: "FirstMate"}
Expect(options.Validate()).NotTo(Succeed())
Expect(options.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was v1beta)`))

options = &Options{Group: "crew", Version: "v1beta1alpha1", Kind: "FirstMate"}
Expect(options.Validate()).NotTo(Succeed())
Expect(options.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta1alpha1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was v1beta1alpha1)`))
})

It("should fail if the Kind is not specified", func() {
Expand Down Expand Up @@ -149,27 +149,27 @@ var _ = Describe("Resource Options", func() {
options := &Options{Group: "crew", Version: "1", Kind: "FirstMate"}
Expect(options.ValidateV2()).NotTo(Succeed())
Expect(options.ValidateV2().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was 1)`))

options = &Options{Group: "crew", Version: "1beta1", Kind: "FirstMate"}
Expect(options.ValidateV2()).NotTo(Succeed())
Expect(options.ValidateV2().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1beta1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was 1beta1)`))

options = &Options{Group: "crew", Version: "a1beta1", Kind: "FirstMate"}
Expect(options.ValidateV2()).NotTo(Succeed())
Expect(options.ValidateV2().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was a1beta1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was a1beta1)`))

options = &Options{Group: "crew", Version: "v1beta", Kind: "FirstMate"}
Expect(options.ValidateV2()).NotTo(Succeed())
Expect(options.ValidateV2().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was v1beta)`))

options = &Options{Group: "crew", Version: "v1beta1alpha1", Kind: "FirstMate"}
Expect(options.ValidateV2()).NotTo(Succeed())
Expect(options.ValidateV2().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta1alpha1)`))
`version must match ^v\d+(?:alpha\d+|beta\d+)?$ (was v1beta1alpha1)`))
})

It("should fail if the Kind is not specified for V2", func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/internal/util/go_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func fetchAndCheckGoVersion() error {
// checkGoVersion should only ever check if the Go version >= 1.13, since the kubebuilder binary only cares
// that the go binary supports go modules which were stabilized in that version (i.e. in go 1.13) by default
func checkGoVersion(verStr string) error {
goVerRegex := `^go?([0-9]+)\.([0-9]+)([\.0-9A-Za-z\-]+)?$`
goVerRegex := `^go?([0-9]+)\.([0-9]+)[\.0-9A-Za-z\-]*$`
m := regexp.MustCompile(goVerRegex).FindStringSubmatch(verStr)
if m == nil {
return fmt.Errorf("invalid version string")
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
)

// verRe defines the string format of a version.
var verRe = regexp.MustCompile("^(v)?([1-9][0-9]*)(-alpha|-beta)?$")
var verRe = regexp.MustCompile("^v?([1-9][0-9]*)(?:-(alpha|beta))?$")

// Version is a plugin version containing a non-zero integer and an optional stage value
// that if present identifies a version as not stable to some degree.
Expand Down Expand Up @@ -75,15 +75,15 @@ func ParseVersion(version string) (v Version, err error) {
return v, errors.New("plugin version is empty")
}

// A valid version string will have 4 submatches, each of which may be empty: the full string, "v",
// the integer, and the stage suffix. Invalid version strings do not have 4 submatches.
// A valid version string will have 3 submatches, each of which may be empty: the full string,
// the integer, and the stage suffix. Invalid version strings do not have 3 submatches.
submatches := verRe.FindStringSubmatch(version)
if len(submatches) != 4 {
if len(submatches) != 3 {
return v, fmt.Errorf("version format must match %s", verRe.String())
}

// Parse version number.
versionNumStr := submatches[2]
versionNumStr := submatches[1]
if versionNumStr == "" {
return v, errors.New("version must contain an integer")
}
Expand All @@ -92,7 +92,7 @@ func ParseVersion(version string) (v Version, err error) {
}

// Parse stage suffix, if any.
v.Stage = strings.TrimPrefix(submatches[3], "-")
v.Stage = submatches[2]

return v, v.Validate()
}
Expand Down

0 comments on commit 6a639d5

Please sign in to comment.