From 175952919092cd7304e5e8aeddb9cce37b6a614b Mon Sep 17 00:00:00 2001 From: Adrian Orive Date: Tue, 27 Oct 2020 13:06:40 +0100 Subject: [PATCH] Enhance regex patterns - Used non-capturing groups when the info is not relevant - Simplified some expressions Signed-off-by: Adrian Orive --- pkg/internal/validation/dns.go | 4 ++-- pkg/internal/validation/project.go | 2 +- pkg/model/resource/options.go | 2 +- pkg/plugin/internal/util/go_version.go | 2 +- pkg/plugin/plugin.go | 12 ++++++------ 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/internal/validation/dns.go b/pkg/internal/validation/dns.go index 8168eac7fbe..33a150f9e70 100644 --- a/pkg/internal/validation/dns.go +++ b/pkg/internal/validation/dns.go @@ -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 { diff --git a/pkg/internal/validation/project.go b/pkg/internal/validation/project.go index 3afe3e98f21..7aa7eee8325 100644 --- a/pkg/internal/validation/project.go +++ b/pkg/internal/validation/project.go @@ -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 + "$") diff --git a/pkg/model/resource/options.go b/pkg/model/resource/options.go index 30216c963c3..eb434fe9f0d 100644 --- a/pkg/model/resource/options.go +++ b/pkg/model/resource/options.go @@ -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" diff --git a/pkg/plugin/internal/util/go_version.go b/pkg/plugin/internal/util/go_version.go index b4ccda7f966..fb71f7589cb 100644 --- a/pkg/plugin/internal/util/go_version.go +++ b/pkg/plugin/internal/util/go_version.go @@ -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") diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index a1295f11234..045fc8cc32a 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -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. @@ -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") } @@ -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() }