Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds zarf cache flag #446

Merged
merged 6 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
jeff-mccoy marked this conversation as resolved.
Show resolved Hide resolved
}

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