mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Add PreserveTildeOnSave config flag (default false). When enabled, the
Save path folds $HOME prefixes back to ~ when serializing the in-memory
Config to YAML so the on-disk config stays portable across machines
(e.g. when shared via dotfiles).
The in-memory Config is left untouched; cloneForSave() produces a
shallow copy with path fields rewritten. Mirrors the existing
expandPath() calls performed in Load():
- cfg.Source, cfg.ExtrasSource
- cfg.Targets[*].Path / Skills.Path / Agents.Path
- cfg.Extras[*].Source and Extras[*].Targets[*].Path
Adds:
- utils.FoldHomePath() helper with unit tests (exact-home, child,
sibling-non-home, unrelated-abs, relative, Windows case-insensitive)
- 3 Save-path tests covering default-off, fold-on, and Load→Save
idempotency.
Default off keeps the change backward compatible: existing users see no
behavior difference. Users who share their config via dotfiles can opt
in with:
preserve_tilde_on_save: true
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// PathsEqual compares two paths for equality.
|
|
// On Windows, paths are compared case-insensitively since NTFS is case-insensitive.
|
|
// On Unix systems, paths are compared exactly.
|
|
func PathsEqual(a, b string) bool {
|
|
if runtime.GOOS == "windows" {
|
|
return strings.EqualFold(a, b)
|
|
}
|
|
return a == b
|
|
}
|
|
|
|
// PathHasPrefix checks if path starts with prefix.
|
|
// On Windows, comparison is case-insensitive.
|
|
// On Unix systems, comparison is exact.
|
|
// ResolveSymlink resolves symlinks on a path so filepath.Walk enters
|
|
// symlinked directories. Falls back to the original path on error.
|
|
func ResolveSymlink(path string) string {
|
|
if resolved, err := filepath.EvalSymlinks(path); err == nil {
|
|
return resolved
|
|
}
|
|
return path
|
|
}
|
|
|
|
func PathHasPrefix(path, prefix string) bool {
|
|
if runtime.GOOS == "windows" {
|
|
return strings.HasPrefix(strings.ToLower(path), strings.ToLower(prefix))
|
|
}
|
|
return strings.HasPrefix(path, prefix)
|
|
}
|
|
|
|
// FoldHomePath folds an absolute path under the user's home directory back to
|
|
// the ~ form for stable, machine-agnostic YAML serialization.
|
|
//
|
|
// Returns the original path unchanged when:
|
|
// - input is empty
|
|
// - user home cannot be determined
|
|
// - path is not under the home prefix (strict match on home + separator)
|
|
// - path already starts with ~
|
|
//
|
|
// On Windows ~ is increasingly accepted but not universally honored by shells;
|
|
// we still fold to keep behavior parallel across OSes (downstream consumers
|
|
// already expand via ExpandPath/normalizeTargetPath).
|
|
//
|
|
// fork patch: preserve ~ in saved config paths (iFwu/skillshare).
|
|
func FoldHomePath(path string) string {
|
|
if path == "" || HasTildePrefix(path) {
|
|
return path
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil || home == "" {
|
|
return path
|
|
}
|
|
return FoldHomePathWith(path, home)
|
|
}
|
|
|
|
// FoldHomePathWith is the home-explicit form of FoldHomePath. Callers that
|
|
// fold many paths in a tight loop should resolve the home directory once
|
|
// (it can involve a syscall on Windows) and pass it here directly.
|
|
func FoldHomePathWith(path, home string) string {
|
|
if path == "" || HasTildePrefix(path) || home == "" {
|
|
return path
|
|
}
|
|
if PathsEqual(path, home) {
|
|
return "~"
|
|
}
|
|
sep := string(filepath.Separator)
|
|
if PathHasPrefix(path, home+sep) {
|
|
return "~" + path[len(home):]
|
|
}
|
|
return path
|
|
}
|