diff --git a/src/internal/api/packages/find.go b/src/internal/api/packages/find.go index 226a0f6682..99ed7f5a8a 100644 --- a/src/internal/api/packages/find.go +++ b/src/internal/api/packages/find.go @@ -18,12 +18,6 @@ import ( "github.com/defenseunicorns/zarf/src/pkg/utils" ) -// Find zarf-packages on the local system (https://regex101.com/r/TUUftK/1) -var packagePattern = regexp.MustCompile(`zarf-package[^\s\\\/]*\.tar(\.zst)?$`) - -// Find zarf-init packages on the local system -var currentInitPattern = regexp.MustCompile(packager.GetInitPackageName("") + "$") - // FindInHomeStream returns all packages in the user's home directory. // If the init query parameter is true, only init packages will be returned. func FindInHomeStream(w http.ResponseWriter, r *http.Request) { @@ -32,9 +26,9 @@ func FindInHomeStream(w http.ResponseWriter, r *http.Request) { w.Header().Set("Connection", "keep-alive") init := r.URL.Query().Get("init") - regexp := packagePattern + regexp := packager.ZarfPackagePattern if init == "true" { - regexp = currentInitPattern + regexp = packager.ZarfInitPattern } done := make(chan bool) @@ -63,7 +57,7 @@ func FindInitStream(w http.ResponseWriter, _ *http.Request) { go func() { // stream init packages in the execution directory if execDir, err := utils.GetFinalExecutablePath(); err == nil { - streamDirPackages(execDir, currentInitPattern, w) + streamDirPackages(execDir, packager.ZarfInitPattern, w) } else { streamError(err, w) } @@ -76,11 +70,11 @@ func FindInitStream(w http.ResponseWriter, _ *http.Request) { streamError(err, w) } } - streamDirPackages(cachePath, currentInitPattern, w) + streamDirPackages(cachePath, packager.ZarfInitPattern, w) // Find init packages in the current working directory if cwd, err := os.Getwd(); err == nil { - streamDirPackages(cwd, currentInitPattern, w) + streamDirPackages(cwd, packager.ZarfInitPattern, w) } else { streamError(err, w) } @@ -98,7 +92,7 @@ func FindPackageStream(w http.ResponseWriter, _ *http.Request) { go func() { if cwd, err := os.Getwd(); err == nil { - streamDirPackages(cwd, packagePattern, w) + streamDirPackages(cwd, packager.ZarfPackagePattern, w) } else { streamError(err, w) } diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index c8573aa029..281bd6f80f 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -12,6 +12,7 @@ import ( "io" "os" "path/filepath" + "regexp" "sort" "strings" @@ -39,6 +40,15 @@ type Packager struct { warnings []string } +// Zarf Packager Variables. +var ( + // Find zarf-packages on the local system (https://regex101.com/r/TUUftK/1) + ZarfPackagePattern = regexp.MustCompile(`zarf-package[^\s\\\/]*\.tar(\.zst)?$`) + + // Find zarf-init packages on the local system + ZarfInitPattern = regexp.MustCompile(GetInitPackageName("") + "$") +) + /* New creates a new package instance with the provided config. diff --git a/src/pkg/packager/remove.go b/src/pkg/packager/remove.go index 98ba68ed6c..305f2187b2 100644 --- a/src/pkg/packager/remove.go +++ b/src/pkg/packager/remove.go @@ -7,7 +7,6 @@ package packager import ( "encoding/json" "fmt" - "regexp" "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/config/lang" @@ -26,8 +25,7 @@ func (p *Packager) Remove(packageName string) (err error) { defer spinner.Stop() // If the user input is a path to a package, extract the package - isTarball := regexp.MustCompile(`.*zarf-package-.*\.tar\.zst$`).MatchString - if isTarball(packageName) { + if ZarfPackagePattern.MatchString(packageName) { if utils.InvalidPath(packageName) { message.Fatalf(nil, lang.CmdPackageRemoveTarballErr) }