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
4 changed files with 10 additions and 35 deletions

View File

@ -8,9 +8,9 @@ import (
"net" "net"
"net/url" "net/url"
"os" "os"
"regexp"
"strings" "strings"
"sync"
"github.com/containers/storage/pkg/regexp"
) )
const ( const (
@ -23,9 +23,8 @@ const (
var ( var (
whiteSpaces = " \t" whiteSpaces = " \t"
alphaRegexp *regexp.Regexp alphaRegexp = regexp.Delayed(`[a-zA-Z]`)
domainRegexp *regexp.Regexp 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*$`)
onceRegex sync.Once
) )
// validateExtraHost validates that the specified string is a valid extrahost and returns it. // 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) { 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) == "" { if alphaRegexp.FindString(val) == "" {
return "", fmt.Errorf("%s is not a valid domain", val) return "", fmt.Errorf("%s is not a valid domain", val)
} }

View File

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

View File

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

View File

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