Skip to content

Commit

Permalink
Globalize zarf package patterns in packager
Browse files Browse the repository at this point in the history
  • Loading branch information
Racer159 committed Jun 22, 2023
1 parent 4ef2645 commit 6a0af44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
18 changes: 6 additions & 12 deletions src/internal/api/packages/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions src/pkg/packager/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package packager
import (
"encoding/json"
"fmt"
"regexp"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
Expand All @@ -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)
}
Expand Down

0 comments on commit 6a0af44

Please sign in to comment.