This commit is contained in:
2025-12-27 02:57:25 -06:00
parent 63a6f8f7dc
commit 1d5d6aacb4
68 changed files with 6884 additions and 0 deletions

44
internal/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,44 @@
package cache
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"software-station/internal/models"
)
const cacheDir = ".cache"
func init() {
if err := os.MkdirAll(cacheDir, 0750); err != nil {
log.Printf("Warning: failed to create cache directory: %v", err)
}
}
func GetCachePath(owner, repo string) string {
return filepath.Join(cacheDir, filepath.Clean(fmt.Sprintf("%s_%s.json", owner, repo)))
}
func SaveToCache(owner, repo string, software models.Software) error {
path := GetCachePath(owner, repo)
data, err := json.MarshalIndent(software, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0600)
}
func GetFromCache(owner, repo string) (*models.Software, error) {
path := GetCachePath(owner, repo)
data, err := os.ReadFile(path) // #nosec G304
if err != nil {
return nil, err
}
var software models.Software
if err := json.Unmarshal(data, &software); err != nil {
return nil, err
}
return &software, nil
}

43
internal/cache/cache_test.go vendored Normal file
View File

@@ -0,0 +1,43 @@
package cache
import (
"os"
"software-station/internal/models"
"testing"
)
func TestCache(t *testing.T) {
owner := "test-owner"
repo := "test-repo"
software := models.Software{
Name: "test-repo",
Owner: "test-owner",
Description: "test desc",
}
// Clean up before and after
path := GetCachePath(owner, repo)
os.Remove(path)
defer os.Remove(path)
// Test GetFromCache missing
_, err := GetFromCache(owner, repo)
if err == nil {
t.Error("expected error for missing cache")
}
// Test SaveToCache
err = SaveToCache(owner, repo, software)
if err != nil {
t.Fatalf("failed to save to cache: %v", err)
}
// Test GetFromCache success
cached, err := GetFromCache(owner, repo)
if err != nil {
t.Fatalf("failed to get from cache: %v", err)
}
if cached.Name != software.Name || cached.Owner != software.Owner {
t.Errorf("cached data mismatch: %+v", cached)
}
}