Skip to content

Commit

Permalink
adds zarf cache location flag
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd committed Apr 15, 2022
1 parent 4183795 commit 6783714
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
16 changes: 15 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"regexp"
"strings"

"github.com/defenseunicorns/zarf/src/config"
Expand All @@ -14,13 +15,17 @@ import (

var zarfLogLevel = ""
var arch string
var zarfImageCache string

var rootCmd = &cobra.Command{
Use: "zarf [COMMAND]|[ZARF-PACKAGE]|[ZARF-YAML]",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
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",
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
19 changes: 16 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -46,7 +47,8 @@ var (

CliArch string

ZarfSeedPort string
ZarfSeedPort string
ZarfImageCachePath = ZarfDefaultImageCachePath

// Private vars
active types.ZarfPackage
Expand Down Expand Up @@ -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
}
13 changes: 0 additions & 13 deletions src/internal/images/common.go

This file was deleted.

3 changes: 2 additions & 1 deletion src/internal/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 6783714

Please sign in to comment.