add per-OS file variants via files.<os>/ directories
This commit is contained in:
+13
-4
@@ -89,10 +89,7 @@ func LoadConfig(path string) (*packageConfig, error) {
|
||||
}
|
||||
|
||||
func SelectTarget(cfg *packageConfig) (string, error) {
|
||||
osKey := runtime.GOOS
|
||||
if osKey == "darwin" {
|
||||
osKey = "macos"
|
||||
}
|
||||
osKey := OSKey()
|
||||
if cfg.disabled[osKey] {
|
||||
return "", ErrTargetDisabled
|
||||
}
|
||||
@@ -105,6 +102,18 @@ func SelectTarget(cfg *packageConfig) (string, error) {
|
||||
return "", fmt.Errorf("missing target for %s and default", osKey)
|
||||
}
|
||||
|
||||
func OSKey() string {
|
||||
osKey := runtime.GOOS
|
||||
if osKey == "darwin" {
|
||||
osKey = "macos"
|
||||
}
|
||||
return osKey
|
||||
}
|
||||
|
||||
func VariantDirName() string {
|
||||
return FilesDirName + "." + OSKey()
|
||||
}
|
||||
|
||||
func ParseIgnore(cfgTbl *lua.LTable) ([]string, error) {
|
||||
ignoreVal := cfgTbl.RawGetString("ignore")
|
||||
if ignoreVal == lua.LNil {
|
||||
|
||||
+179
-57
@@ -11,42 +11,74 @@ import (
|
||||
)
|
||||
|
||||
func ApplyPackage(filesDir, targetRoot string, cfg *packageConfig) error {
|
||||
return ApplyPackages([]string{filesDir}, targetRoot, cfg)
|
||||
}
|
||||
|
||||
func ApplyPackages(filesDirs []string, targetRoot string, cfg *packageConfig) error {
|
||||
if err := EnsureDir(targetRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return filepath.WalkDir(filesDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if path == filesDir {
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(filesDir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ShouldIgnorePath(rel, cfg) {
|
||||
if entry.IsDir() {
|
||||
return filepath.SkipDir
|
||||
for _, filesDir := range filesDirs {
|
||||
if _, err := os.Stat(filesDir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(targetRoot, rel)
|
||||
err := filepath.WalkDir(filesDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if path == filesDir {
|
||||
return nil
|
||||
}
|
||||
|
||||
if entry.IsDir() {
|
||||
return EnsureDir(targetPath)
|
||||
}
|
||||
rel, err := filepath.Rel(filesDir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcAbs, err := filepath.Abs(path)
|
||||
if ShouldIgnorePath(rel, cfg) {
|
||||
if entry.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(targetRoot, rel)
|
||||
|
||||
if entry.IsDir() {
|
||||
return EnsureDir(targetPath)
|
||||
}
|
||||
|
||||
srcAbs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// For overlay mode: remove existing symlink if it points to a different source
|
||||
if info, err := os.Lstat(targetPath); err == nil && info.Mode()&os.ModeSymlink != 0 {
|
||||
current, err := os.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current != srcAbs {
|
||||
// Remove the old symlink to allow overlay
|
||||
if err := os.Remove(targetPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return LinkFile(srcAbs, targetPath)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return LinkFile(srcAbs, targetPath)
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LinkFile(src, dst string) error {
|
||||
@@ -88,68 +120,158 @@ func EnsureDir(path string) error {
|
||||
}
|
||||
|
||||
func FindStaleLinks(filesDir, targetRoot string) ([]string, error) {
|
||||
filesAbs, err := filepath.Abs(filesDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FindStaleLinksMulti([]string{filesDir}, targetRoot)
|
||||
}
|
||||
|
||||
func FindStaleLinksMulti(filesDirs []string, targetRoot string) ([]string, error) {
|
||||
targetAbs, err := filepath.Abs(targetRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var stale []string
|
||||
walkErr := filepath.WalkDir(filesDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
// Collect all valid source directories (prefixes) for checking
|
||||
var sourcePrefixes []string
|
||||
for _, filesDir := range filesDirs {
|
||||
filesAbs, err := filepath.Abs(filesDir)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(filesAbs, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(targetAbs, rel)
|
||||
info, err := os.Lstat(targetPath)
|
||||
if err != nil {
|
||||
if _, err := os.Stat(filesDir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
// Symlink doesn't exist - not stale, just not applied
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
sourcePrefixes = append(sourcePrefixes, filesAbs)
|
||||
}
|
||||
|
||||
if len(sourcePrefixes) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var stale []string
|
||||
seen := make(map[string]bool)
|
||||
|
||||
// First pass: walk source directories to find files that exist
|
||||
for _, filesDir := range filesDirs {
|
||||
filesAbs, err := filepath.Abs(filesDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = filepath.WalkDir(filesDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
||||
rel, err := filepath.Rel(filesAbs, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(targetAbs, rel)
|
||||
seen[targetPath] = true
|
||||
|
||||
info, err := os.Lstat(targetPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if info.Mode()&os.ModeSymlink == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
src, err := os.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !filepath.IsAbs(src) {
|
||||
src = filepath.Join(filepath.Dir(targetPath), src)
|
||||
}
|
||||
src = filepath.Clean(src)
|
||||
|
||||
// Check if symlink points to any of our source directories
|
||||
pointsToRepo := false
|
||||
for _, prefix := range sourcePrefixes {
|
||||
if strings.HasPrefix(src, prefix+string(os.PathSeparator)) || src == prefix {
|
||||
pointsToRepo = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !pointsToRepo {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if the source file in repo still exists
|
||||
if _, err := os.Stat(src); errors.Is(err, os.ErrNotExist) {
|
||||
stale = append(stale, targetPath)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: walk target directory to find orphaned symlinks
|
||||
// (symlinks pointing to our repo but source file no longer exists)
|
||||
targetEntries, err := os.ReadDir(targetAbs)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return stale, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, entry := range targetEntries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
targetPath := filepath.Join(targetAbs, entry.Name())
|
||||
if seen[targetPath] {
|
||||
continue
|
||||
}
|
||||
|
||||
info, err := os.Lstat(targetPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink == 0 {
|
||||
// Not a symlink, skip
|
||||
return nil
|
||||
continue
|
||||
}
|
||||
|
||||
src, err := os.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
continue
|
||||
}
|
||||
if !filepath.IsAbs(src) {
|
||||
src = filepath.Join(filepath.Dir(targetPath), src)
|
||||
}
|
||||
src = filepath.Clean(src)
|
||||
|
||||
// Only consider symlinks pointing to our repo
|
||||
if !strings.HasPrefix(src, filesAbs+string(os.PathSeparator)) && src != filesAbs {
|
||||
return nil
|
||||
// Check if symlink points to any of our source directories
|
||||
pointsToRepo := false
|
||||
for _, prefix := range sourcePrefixes {
|
||||
if strings.HasPrefix(src, prefix+string(os.PathSeparator)) || src == prefix {
|
||||
pointsToRepo = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !pointsToRepo {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if the source file in repo still exists
|
||||
// Check if source still exists
|
||||
if _, err := os.Stat(src); errors.Is(err, os.ErrNotExist) {
|
||||
stale = append(stale, targetPath)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if walkErr != nil {
|
||||
return nil, walkErr
|
||||
}
|
||||
|
||||
return stale, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -304,3 +305,121 @@ func TestRestoreOne(t *testing.T) {
|
||||
t.Errorf("wrong content after restore: %s", string(content))
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyPackages_Overlay(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
baseDir := filepath.Join(tmp, "files")
|
||||
linuxDir := filepath.Join(tmp, "files.linux")
|
||||
targetDir := filepath.Join(tmp, "target")
|
||||
|
||||
// Create base files
|
||||
if err := os.MkdirAll(baseDir, 0o755); err != nil {
|
||||
t.Fatalf("failed to create base dir: %v", err)
|
||||
}
|
||||
baseFile := filepath.Join(baseDir, "common.txt")
|
||||
if err := os.WriteFile(baseFile, []byte("common"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create base file: %v", err)
|
||||
}
|
||||
sharedFile := filepath.Join(baseDir, "shared.txt")
|
||||
if err := os.WriteFile(sharedFile, []byte("shared"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create shared file: %v", err)
|
||||
}
|
||||
|
||||
// Create Linux-specific override
|
||||
if err := os.MkdirAll(linuxDir, 0o755); err != nil {
|
||||
t.Fatalf("failed to create linux dir: %v", err)
|
||||
}
|
||||
linuxFile := filepath.Join(linuxDir, "common.txt") // overrides base
|
||||
if err := os.WriteFile(linuxFile, []byte("linux"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create linux file: %v", err)
|
||||
}
|
||||
|
||||
cfg := &packageConfig{}
|
||||
filesDirs := []string{baseDir, linuxDir}
|
||||
|
||||
// Apply both directories
|
||||
if err := ApplyPackages(filesDirs, targetDir, cfg); err != nil {
|
||||
t.Errorf("ApplyPackages failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify shared file from base is linked
|
||||
targetShared := filepath.Join(targetDir, "shared.txt")
|
||||
content, err := os.ReadFile(targetShared)
|
||||
if err != nil {
|
||||
t.Errorf("shared file not found: %v", err)
|
||||
} else if string(content) != "shared" {
|
||||
t.Errorf("shared file wrong content: %s", string(content))
|
||||
}
|
||||
|
||||
// Verify common.txt is overridden by Linux variant
|
||||
targetCommon := filepath.Join(targetDir, "common.txt")
|
||||
content, err = os.ReadFile(targetCommon)
|
||||
if err != nil {
|
||||
t.Errorf("common file not found: %v", err)
|
||||
} else if string(content) != "linux" {
|
||||
t.Errorf("common file should be overridden by linux variant, got: %s", string(content))
|
||||
}
|
||||
|
||||
// Verify symlink points to linux variant
|
||||
src, err := os.Readlink(targetCommon)
|
||||
if err != nil {
|
||||
t.Errorf("failed to read symlink: %v", err)
|
||||
} else if !strings.HasSuffix(src, "files.linux/common.txt") {
|
||||
t.Errorf("symlink should point to linux variant, got: %s", src)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindStaleLinksMulti_WithVariants(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
baseDir := filepath.Join(tmp, "files")
|
||||
linuxDir := filepath.Join(tmp, "files.linux")
|
||||
targetDir := filepath.Join(tmp, "target")
|
||||
|
||||
if err := os.MkdirAll(baseDir, 0o755); err != nil {
|
||||
t.Fatalf("failed to create base dir: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(linuxDir, 0o755); err != nil {
|
||||
t.Fatalf("failed to create linux dir: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(targetDir, 0o755); err != nil {
|
||||
t.Fatalf("failed to create target dir: %v", err)
|
||||
}
|
||||
|
||||
// Create files
|
||||
baseFile := filepath.Join(baseDir, "base.txt")
|
||||
if err := os.WriteFile(baseFile, []byte("base"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create base file: %v", err)
|
||||
}
|
||||
linuxFile := filepath.Join(linuxDir, "linux.txt")
|
||||
if err := os.WriteFile(linuxFile, []byte("linux"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create linux file: %v", err)
|
||||
}
|
||||
|
||||
// Create symlinks
|
||||
targetBase := filepath.Join(targetDir, "base.txt")
|
||||
if err := os.Symlink(baseFile, targetBase); err != nil {
|
||||
t.Fatalf("failed to create base symlink: %v", err)
|
||||
}
|
||||
targetLinux := filepath.Join(targetDir, "linux.txt")
|
||||
if err := os.Symlink(linuxFile, targetLinux); err != nil {
|
||||
t.Fatalf("failed to create linux symlink: %v", err)
|
||||
}
|
||||
|
||||
// Delete the linux source file to create a stale link
|
||||
if err := os.Remove(linuxFile); err != nil {
|
||||
t.Fatalf("failed to remove linux file: %v", err)
|
||||
}
|
||||
|
||||
filesDirs := []string{baseDir, linuxDir}
|
||||
stale, err := FindStaleLinksMulti(filesDirs, targetDir)
|
||||
if err != nil {
|
||||
t.Errorf("FindStaleLinksMulti failed: %v", err)
|
||||
}
|
||||
|
||||
if len(stale) != 1 {
|
||||
t.Errorf("expected 1 stale link, got %d", len(stale))
|
||||
}
|
||||
if len(stale) > 0 && stale[0] != targetLinux {
|
||||
t.Errorf("expected stale link to be %s, got %s", targetLinux, stale[0])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user