diff --git a/src/cmd/root.go b/src/cmd/root.go index 002f73c1f7..44d668e228 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "regexp" "strings" "github.com/defenseunicorns/zarf/src/config" @@ -14,6 +15,7 @@ import ( var zarfLogLevel = "" var arch string +var zarfImageCache string var rootCmd = &cobra.Command{ Use: "zarf [COMMAND]|[ZARF-PACKAGE]|[ZARF-YAML]", @@ -21,6 +23,9 @@ var rootCmd = &cobra.Command{ if zarfLogLevel != "" { setLogLevel(zarfLogLevel) } + if cachePathClean(zarfImageCache) { + config.ZarfImageCachePath = zarfImageCache + } config.CliArch = arch }, Short: "Small tool to bundle dependencies with K3s for air-gapped deployments", @@ -55,10 +60,10 @@ func init() { // Re-add the original help function originalHelp(c, s) }) - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "", "Log level when running Zarf. Valid options are: warn, info, debug, trace") rootCmd.PersistentFlags().StringVarP(&arch, "architecture", "a", "", "Architecture for OCI images") + rootCmd.PersistentFlags().StringVar(&zarfImageCache, "zarf-cache", config.ZarfDefaultImageCachePath, "Specify the location of the Zarf image cache") } func setLogLevel(logLevel string) { @@ -76,3 +81,12 @@ func setLogLevel(logLevel string) { message.Warn("invalid log level setting") } } + +func cachePathClean(cachePath string) bool { + var isCleanPath = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~]+$`).MatchString + if !isCleanPath(cachePath) { + message.Warn(fmt.Sprintf("Invalid characters in Zarf cache path, defaulting to ~/%s", config.ZarfDefaultImageCachePath)) + return false + } + return true +} diff --git a/src/config/config.go b/src/config/config.go index 5de3f89d7a..66dcca89d9 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -33,8 +33,9 @@ const ( ZarfConnectAnnotationDescription = "zarf.dev/connect-description" ZarfConnectAnnotationUrl = "zarf.dev/connect-url" - ZarfManagedByLabel = "app.kubernetes.io/managed-by" - ZarfCleanupScriptsPath = "/opt/zarf" + ZarfManagedByLabel = "app.kubernetes.io/managed-by" + ZarfCleanupScriptsPath = "/opt/zarf" + ZarfDefaultImageCachePath = ".zarf-image-cache" ) var ( @@ -46,7 +47,8 @@ var ( CliArch string - ZarfSeedPort string + ZarfSeedPort string + ZarfImageCachePath = ZarfDefaultImageCachePath // Private vars active types.ZarfPackage @@ -181,3 +183,14 @@ func BuildConfig(path string) error { return utils.WriteYaml(path, active, 0400) } + +func GetImageCachePath() string { + homePath, _ := os.UserHomeDir() + if ZarfImageCachePath == ZarfDefaultImageCachePath { + return fmt.Sprintf("%s/%s", homePath, ZarfImageCachePath) + } + if string(ZarfImageCachePath[0]) == "~" { + return fmt.Sprintf("%s/%s", homePath, ZarfImageCachePath[len("~/"):]) + } + return ZarfImageCachePath +} diff --git a/src/internal/images/common.go b/src/internal/images/common.go deleted file mode 100644 index 9a9e665c6b..0000000000 --- a/src/internal/images/common.go +++ /dev/null @@ -1,13 +0,0 @@ -package images - -import ( - "fmt" - "os" -) - -var cachePath = ".zarf-image-cache" - -func init() { - homePath, _ := os.UserHomeDir() - cachePath = fmt.Sprintf("%s/%s", homePath, cachePath) -} diff --git a/src/internal/images/pull.go b/src/internal/images/pull.go index d81f24ab05..9f3c783866 100644 --- a/src/internal/images/pull.go +++ b/src/internal/images/pull.go @@ -46,7 +46,8 @@ func PullAll(buildImageList []string, imageTarballPath string) { if err != nil { spinner.Fatalf(err, "Unable to pull the image %s", src) } - img = cache.Image(img, cache.NewFilesystemCache(cachePath)) + imageCachePath := config.GetImageCachePath() + img = cache.Image(img, cache.NewFilesystemCache(imageCachePath)) imageMap[src] = img }