Use containers/storage/pkg/regexp in place of regexp

This is a cleaner solution and guarantees the variables
will be used before they are initialized.

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2023-01-12 18:31:19 -05:00
parent c433982d18
commit c2b36beb40
No known key found for this signature in database
GPG key ID: A2DF901DABE2C028
4 changed files with 10 additions and 35 deletions

View file

@ -8,9 +8,9 @@ import (
"net"
"net/url"
"os"
"regexp"
"strings"
"sync"
"github.com/containers/storage/pkg/regexp"
)
const (
@ -23,9 +23,8 @@ const (
var (
whiteSpaces = " \t"
alphaRegexp *regexp.Regexp
domainRegexp *regexp.Regexp
onceRegex sync.Once
alphaRegexp = regexp.Delayed(`[a-zA-Z]`)
domainRegexp = regexp.Delayed(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
)
// validateExtraHost validates that the specified string is a valid extrahost and returns it.
@ -54,10 +53,6 @@ func validateIPAddress(val string) (string, error) {
}
func ValidateDomain(val string) (string, error) {
onceRegex.Do(func() {
alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
})
if alphaRegexp.FindString(val) == "" {
return "", fmt.Errorf("%s is not a valid domain", val)
}

View file

@ -6,9 +6,7 @@ import (
"net"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"github.com/containers/buildah/pkg/parse"
@ -25,15 +23,13 @@ import (
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/regexp"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sirupsen/logrus"
)
var (
umaskRegex *regexp.Regexp
onceRegex sync.Once
)
var umaskRegex = regexp.Delayed(`^[0-7]{1,4}$`)
// WithStorageConfig uses the given configuration to set up container storage.
// If this is not specified, the system default configuration will be used
@ -1797,9 +1793,6 @@ func WithTimezone(path string) CtrCreateOption {
// WithUmask sets the umask in the container
func WithUmask(umask string) CtrCreateOption {
onceRegex.Do(func() {
umaskRegex = regexp.MustCompile(`^[0-7]{1,4}$`)
})
return func(ctr *Container) error {
if ctr.valid {
return define.ErrCtrFinalized

View file

@ -13,11 +13,9 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"github.com/containers/buildah/define"
"github.com/containers/image/v5/types"
@ -26,6 +24,7 @@ import (
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/regexp"
"github.com/docker/go-units"
"github.com/hashicorp/go-multierror"
jsoniter "github.com/json-iterator/go"
@ -37,17 +36,10 @@ type devino struct {
Ino uint64
}
var (
iidRegex *regexp.Regexp
onceRegex sync.Once
)
var iidRegex = regexp.Delayed(`^[0-9a-f]{12}`)
// Build creates an image using a containerfile reference
func Build(ctx context.Context, containerFiles []string, options entities.BuildOptions) (*entities.BuildReport, error) {
onceRegex.Do(func() {
iidRegex = regexp.MustCompile(`^[0-9a-f]{12}`)
})
if options.CommonBuildOpts == nil {
options.CommonBuildOpts = new(define.CommonBuildOptions)
}

View file

@ -3,11 +3,10 @@ package quadlet
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/containers/podman/v4/pkg/systemd/parser"
"github.com/containers/storage/pkg/regexp"
)
const (
@ -75,8 +74,7 @@ const (
)
var (
onceRegex sync.Once
validPortRange *regexp.Regexp
validPortRange = regexp.Delayed(`\d+(-\d+)?(/udp|/tcp)?$`)
// Supported keys in "Container" group
supportedContainerKeys = map[string]bool{
@ -157,9 +155,6 @@ func replaceExtension(name string, extension string, extraPrefix string, extraSu
}
func isPortRange(port string) bool {
onceRegex.Do(func() {
validPortRange = regexp.MustCompile(`\d+(-\d+)?(/udp|/tcp)?$`)
})
return validPortRange.MatchString(port)
}