Store compresses the package dir and store in cache, the meta is expected to be a string and would be used to calculate the hash key for cache.
(packageDir, meta string)
| 76 | // Store compresses the package dir and store in cache, |
| 77 | // the meta is expected to be a string and would be used to calculate the hash key for cache. |
| 78 | func (d DevArtifactCache) Store(packageDir, meta string) error { |
| 79 | if !fileio.PathExists(packageDir) { |
| 80 | return fmt.Errorf("package dir does not exist: %s", packageDir) |
| 81 | } |
| 82 | |
| 83 | // Validate packageDir format and extract metadata. |
| 84 | // Path format: ~/celer/x86_64-linux-dev/nameVersion |
| 85 | parts := strings.Split(filepath.ToSlash(packageDir), "/") |
| 86 | if len(parts) < 1 { |
| 87 | return fmt.Errorf("invalid package dir: %s", packageDir) |
| 88 | } |
| 89 | |
| 90 | // Extract from path components. |
| 91 | nameVersion := parts[len(parts)-1] |
| 92 | |
| 93 | // Validate nameVersion format (should be name@version) |
| 94 | versionParts := strings.Split(nameVersion, "@") |
| 95 | if len(versionParts) != 2 { |
| 96 | return fmt.Errorf("invalid package dir: %s", packageDir) |
| 97 | } |
| 98 | |
| 99 | // Extract tar.gz to a tmp dir. |
| 100 | archiveName := fmt.Sprintf("%s.tar.gz", nameVersion) |
| 101 | if err := dirs.CleanTmpFilesDir(); err != nil { |
| 102 | return fmt.Errorf("failed to clean tmp files dir -> %w", err) |
| 103 | } |
| 104 | tempArchive, err := os.CreateTemp(dirs.TmpFilesDir, archiveName+".*") |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | tempArchivePath := tempArchive.Name() |
| 109 | tempArchive.Close() |
| 110 | defer os.Remove(tempArchivePath) |
| 111 | |
| 112 | if err := fileio.Targz(tempArchivePath, packageDir, false); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | |
| 116 | destDir := filepath.Join(d.cacheDir, nameVersion) |
| 117 | metaDir := filepath.Join(destDir, "metas") |
| 118 | |
| 119 | // Calculate checksum of metadata (this would be the cache key). |
| 120 | data := sha256.Sum256([]byte(meta)) |
| 121 | hash := fmt.Sprintf("%x", data) |
| 122 | |
| 123 | // Create dirs. |
| 124 | if err := os.MkdirAll(destDir, os.ModePerm); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | if err := os.MkdirAll(metaDir, os.ModePerm); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | // Copy archive directly to final path. |
| 132 | archivePath := filepath.Join(destDir, hash+".tar.gz") |
| 133 | if err := fileio.CopyFile(tempArchivePath, archivePath); err != nil { |
| 134 | return err |
| 135 | } |
nothing calls this directly
no test coverage detected