diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 376db85da9..9dd680c259 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -94,19 +94,27 @@ func findInitPackage(ctx context.Context, initPackageName string) (string, error } // Create the cache directory if it doesn't exist - if helpers.InvalidPath(config.GetAbsCachePath()) { - if err := helpers.CreateDirectory(config.GetAbsCachePath(), helpers.ReadExecuteAllWriteUser); err != nil { - return "", fmt.Errorf("unable to create the cache directory %s: %w", config.GetAbsCachePath(), err) + absCachePath, err := config.GetAbsCachePath() + if err != nil { + return "", err + } + // Verify that we can write to the path + // FIXME(mkcp): Decompose this into a helper function + if helpers.InvalidPath(absCachePath) { + // Create the directory if the path is invalid + if err := helpers.CreateDirectory(absCachePath, helpers.ReadExecuteAllWriteUser); err != nil { + return "", fmt.Errorf("unable to create the cache directory %s: %w", absCachePath, err) } } // Next, look in the cache directory - if !helpers.InvalidPath(filepath.Join(config.GetAbsCachePath(), initPackageName)) { - return filepath.Join(config.GetAbsCachePath(), initPackageName), nil + if !helpers.InvalidPath(filepath.Join(absCachePath, initPackageName)) { + // join and return + return filepath.Join(absCachePath, initPackageName), nil } // Finally, if the init-package doesn't exist in the cache directory, suggest downloading it - downloadCacheTarget, err := downloadInitPackage(ctx, config.GetAbsCachePath()) + downloadCacheTarget, err := downloadInitPackage(ctx, absCachePath) if err != nil { if errors.Is(err, lang.ErrInitNotFound) { return "", err @@ -130,6 +138,7 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er message.Note(lang.CmdInitPullNote) // Prompt the user if --confirm not specified + // FIXME(mkcp): This condition can never be met if !confirmDownload { prompt := &survey.Confirm{ Message: lang.CmdInitPullConfirm, diff --git a/src/cmd/tools/zarf.go b/src/cmd/tools/zarf.go index 5a9cd11718..df516f9506 100644 --- a/src/cmd/tools/zarf.go +++ b/src/cmd/tools/zarf.go @@ -205,11 +205,15 @@ var clearCacheCmd = &cobra.Command{ Aliases: []string{"c"}, Short: lang.CmdToolsClearCacheShort, RunE: func(_ *cobra.Command, _ []string) error { - message.Notef(lang.CmdToolsClearCacheDir, config.GetAbsCachePath()) - if err := os.RemoveAll(config.GetAbsCachePath()); err != nil { - return fmt.Errorf("unable to clear the cache directory %s: %w", config.GetAbsCachePath(), err) + cachePath, err := config.GetAbsCachePath() + if err != nil { + return err + } + message.Notef(lang.CmdToolsClearCacheDir, cachePath) + if err := os.RemoveAll(cachePath); err != nil { + return fmt.Errorf("unable to clear the cache directory %s: %w", cachePath, err) } - message.Successf(lang.CmdToolsClearCacheSuccess, config.GetAbsCachePath()) + message.Successf(lang.CmdToolsClearCacheSuccess, cachePath) return nil }, } diff --git a/src/config/config.go b/src/config/config.go index 4a4001dce6..ca80f619b6 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -97,16 +97,19 @@ func GetDataInjectionMarker() string { } // GetAbsCachePath gets the absolute cache path for images and git repos. -func GetAbsCachePath() string { +func GetAbsCachePath() (string, error) { return GetAbsHomePath(CommonOptions.CachePath) } // GetAbsHomePath replaces ~ with the absolute path to a user's home dir -func GetAbsHomePath(path string) string { - homePath, _ := os.UserHomeDir() +func GetAbsHomePath(path string) (string, error) { + homePath, err := os.UserHomeDir() + if err != nil { + return "", err + } if strings.HasPrefix(path, "~") { - return strings.Replace(path, "~", homePath, 1) + return strings.Replace(path, "~", homePath, 1), nil } - return path + return path, nil } diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 2e206742c6..a11b6097d7 100644 --- a/src/internal/packager/images/pull.go +++ b/src/internal/packager/images/pull.go @@ -300,7 +300,11 @@ func CleanupInProgressLayers(ctx context.Context, img v1.Image) error { if err != nil { return err } - cacheDir := filepath.Join(config.GetAbsCachePath(), layout.ImagesDir) + absPath, err := config.GetAbsCachePath() + if err != nil { + return err + } + cacheDir := filepath.Join(absPath, layout.ImagesDir) location := filepath.Join(cacheDir, digest.String()) info, err := os.Stat(location) if errors.Is(err, fs.ErrNotExist) { diff --git a/src/internal/packager/sbom/catalog.go b/src/internal/packager/sbom/catalog.go index 58aa23870a..65c32c039b 100755 --- a/src/internal/packager/sbom/catalog.go +++ b/src/internal/packager/sbom/catalog.go @@ -54,9 +54,13 @@ var componentPrefix = "zarf-component-" func Catalog(ctx context.Context, componentSBOMs map[string]*layout.ComponentSBOM, imageList []transform.Image, paths *layout.PackagePaths) error { imageCount := len(imageList) componentCount := len(componentSBOMs) + cachePath, err := config.GetAbsCachePath() + if err != nil { + return err + } builder := Builder{ spinner: message.NewProgressSpinner("Creating SBOMs for %d images and %d components with files.", imageCount, componentCount), - cachePath: config.GetAbsCachePath(), + cachePath: cachePath, imagesPath: paths.Images.Base, outputDir: paths.SBOMs.Path, } diff --git a/src/pkg/packager/composer/oci.go b/src/pkg/packager/composer/oci.go index 2541e45f32..4d2589f717 100644 --- a/src/pkg/packager/composer/oci.go +++ b/src/pkg/packager/composer/oci.go @@ -64,7 +64,11 @@ func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error { componentDesc := manifest.Locate(filepath.Join(layout.ComponentsDir, fmt.Sprintf("%s.tar", name))) - cache := filepath.Join(config.GetAbsCachePath(), "oci") + absCachePath, err := config.GetAbsCachePath() + if err != nil { + return err + } + cache := filepath.Join(absCachePath, "oci") if err := helpers.CreateDirectory(cache, helpers.ReadWriteExecuteUser); err != nil { return err } diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index 4c71ec9ac7..87ad5ee0b8 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -184,12 +184,16 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths dst.AddImages() + cachePath, err := config.GetAbsCachePath() + if err != nil { + return err + } pullCfg := images.PullConfig{ DestinationDirectory: dst.Images.Base, ImageList: imageList, Arch: arch, RegistryOverrides: pc.createOpts.RegistryOverrides, - CacheDirectory: filepath.Join(config.GetAbsCachePath(), layout.ImagesDir), + CacheDirectory: filepath.Join(cachePath, layout.ImagesDir), } pulled, err := images.Pull(ctx, pullCfg) diff --git a/src/pkg/packager/deploy.go b/src/pkg/packager/deploy.go index ee3796ac6c..5aba0ffc1e 100644 --- a/src/pkg/packager/deploy.go +++ b/src/pkg/packager/deploy.go @@ -438,8 +438,11 @@ func (p *Packager) processComponentFiles(component v1alpha1.ZarfComponent, pkgLo } // Replace temp target directory and home directory - file.Target = strings.Replace(file.Target, "###ZARF_TEMP###", p.layout.Base, 1) - file.Target = config.GetAbsHomePath(file.Target) + target, err := config.GetAbsHomePath(strings.Replace(file.Target, "###ZARF_TEMP###", p.layout.Base, 1)) + if err != nil { + return err + } + file.Target = target fileList := []string{} if helpers.IsDir(fileLocation) { @@ -467,9 +470,9 @@ func (p *Packager) processComponentFiles(component v1alpha1.ZarfComponent, pkgLo // Copy the file to the destination spinner.Updatef("Saving %s", file.Target) - err := helpers.CreatePathAndCopy(fileLocation, file.Target) - if err != nil { - return fmt.Errorf("unable to copy file %s to %s: %w", fileLocation, file.Target, err) + err2 := helpers.CreatePathAndCopy(fileLocation, file.Target) + if err2 != nil { + return fmt.Errorf("unable to copy file %s to %s: %w", fileLocation, file.Target, err2) } // Loop over all symlinks and create them