Build Path Cleanup
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package topdata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -21,6 +22,7 @@ const (
|
||||
partsManifestReleaseTag = "parts-manifest-current"
|
||||
partsManifestAssetName = "sow-parts-manifest.json"
|
||||
partsManifestCacheFileName = "sow-parts-manifest.json"
|
||||
partsManifestCacheMaxAge = time.Hour
|
||||
partsManifestRequestTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
@@ -51,12 +53,21 @@ func resolveReleasedPartsManifest(p *project.Project, progress func(string)) (ma
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cachePath := filepath.Join(p.Root, ".cache", "topdata", partsManifestCacheFileName)
|
||||
if strings.TrimSpace(os.Getenv("SOW_PARTS_MANIFEST_REFRESH")) == "" {
|
||||
manifest, fresh, err := readFreshPartsManifestCache(cachePath, partsManifestCacheMaxAge)
|
||||
if err != nil {
|
||||
progress(fmt.Sprintf("Ignoring cached parts manifest: %v", err))
|
||||
} else if fresh {
|
||||
progress(fmt.Sprintf("Using cached released parts manifest from %s...", cachePath))
|
||||
return inventoryFromPartsManifest(manifest), nil
|
||||
}
|
||||
}
|
||||
progress(fmt.Sprintf("Fetching released parts manifest from %s...", manifestURL))
|
||||
manifest, err := fetchPartsManifest(manifestURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cachePath := filepath.Join(p.Root, ".cache", "topdata", partsManifestCacheFileName)
|
||||
if err := writePartsManifestCache(cachePath, manifest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -144,6 +155,31 @@ func fetchPartsManifest(manifestURL string) (*partsManifest, error) {
|
||||
return &manifest, nil
|
||||
}
|
||||
|
||||
func readFreshPartsManifestCache(path string, maxAge time.Duration) (*partsManifest, bool, error) {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil, false, err
|
||||
}
|
||||
if maxAge > 0 && time.Since(info.ModTime()) > maxAge {
|
||||
return nil, false, nil
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var manifest partsManifest
|
||||
if err := json.Unmarshal(raw, &manifest); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if len(manifest.Categories) == 0 {
|
||||
return nil, false, fmt.Errorf("categories is empty")
|
||||
}
|
||||
return &manifest, true, nil
|
||||
}
|
||||
|
||||
func fetchJSON(target string, out any) error {
|
||||
req, err := http.NewRequest(http.MethodGet, target, nil)
|
||||
if err != nil {
|
||||
@@ -181,8 +217,16 @@ func writePartsManifestCache(path string, manifest *partsManifest) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal parts manifest cache: %w", err)
|
||||
}
|
||||
payload = append(payload, '\n')
|
||||
current, err := os.ReadFile(path)
|
||||
if err == nil && bytes.Equal(current, payload) {
|
||||
return nil
|
||||
}
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("read parts manifest cache: %w", err)
|
||||
}
|
||||
tmpPath := path + ".tmp"
|
||||
if err := os.WriteFile(tmpPath, append(payload, '\n'), 0o644); err != nil {
|
||||
if err := os.WriteFile(tmpPath, payload, 0o644); err != nil {
|
||||
return fmt.Errorf("write parts manifest cache: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmpPath, path); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user