Skip to content

Commit

Permalink
Merge pull request #7 from inteon/change_temp_dir
Browse files Browse the repository at this point in the history
Also use cache dir as temporary directory, solving cross-disk copy errors
  • Loading branch information
cert-manager-prow[bot] authored May 3, 2024
2 parents 1da07ea + e2d88b8 commit 75149d8
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions pkg/cache/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func calculateCacheKey(src mod.KloneSource) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s", src.RepoURL, src.RepoHash, src.RepoPath))))[:30]
return fmt.Sprintf("cache-%x", sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s", src.RepoURL, src.RepoHash, src.RepoPath))))[:30]
}

func getCacheDir() (string, error) {
Expand All @@ -28,14 +28,6 @@ func getCacheDir() (string, error) {
return filepath.Abs(filepath.Clean(filepath.Join(home, ".cache", "klone")))
}

func getTempDir() (string, error) {
if tempDir := os.Getenv("KLONE_TEMP_DIR"); tempDir != "" {
return filepath.Abs(filepath.Clean(tempDir))
}

return os.TempDir(), nil
}

func CloneWithCache(
destPath string,
src mod.KloneSource,
Expand All @@ -46,17 +38,16 @@ func CloneWithCache(
return err
}

if err := os.MkdirAll(cacheDir, 0755); err != nil {
return err
}

cachePath := filepath.Join(cacheDir, calculateCacheKey(src))

if _, err := os.Stat(cachePath); err != nil && !os.IsNotExist(err) {
return err
} else if err != nil {
tmpParentDir, err := getTempDir()
if err != nil {
return err
}

tempDir, err := os.MkdirTemp(tmpParentDir, "klone-*")
tempDir, err := os.MkdirTemp(cacheDir, "temp-*")
if err != nil {
return err
}
Expand Down

0 comments on commit 75149d8

Please sign in to comment.