Skip to content

Commit

Permalink
capture errors on src/config/config.go. checking ci
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp committed Sep 13, 2024
1 parent 9f99493 commit 7505f18
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 24 deletions.
21 changes: 15 additions & 6 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
13 changes: 8 additions & 5 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion src/internal/packager/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion src/internal/packager/sbom/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ var componentPrefix = "zarf-component-"
func Catalog(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,
}
Expand Down
6 changes: 5 additions & 1 deletion src/pkg/packager/composer/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7505f18

Please sign in to comment.