Files
ddc/internal/config/libraries.go
T
2026-10-25 20:24:50 -04:00

57 lines
1.1 KiB
Go
Executable File

package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
func parseLibraryRoots(raw string) ([]string, error) {
var roots []string
seenRoots := make(map[string]bool)
for _, entry := range strings.Split(raw, ";") {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
root, err := resolveRoot(entry)
if err != nil {
return nil, err
}
if seenRoots[root] {
continue
}
seenRoots[root] = true
roots = append(roots, root)
}
if len(roots) == 0 {
return nil, errors.New("LIBRARY_ROOTS must list at least one folder")
}
return roots, nil
}
func resolveRoot(entry string) (string, error) {
cleaned := filepath.Clean(entry)
if !filepath.IsAbs(cleaned) || cleaned == "/" {
return "", fmt.Errorf("LIBRARY_ROOTS: invalid path %q", entry)
}
resolved, err := filepath.EvalSymlinks(cleaned)
if err != nil {
return "", fmt.Errorf("LIBRARY_ROOTS: %w", err)
}
info, err := os.Stat(resolved)
if err != nil {
return "", fmt.Errorf("LIBRARY_ROOTS: %w", err)
}
if !info.IsDir() {
return "", fmt.Errorf("LIBRARY_ROOTS: %q is not a directory", entry)
}
return resolved, nil
}