Skip to content

Commit 2a42ae2

Browse files
committed
feat(dotfiles): search for common dotfiles
1 parent 388fe81 commit 2a42ae2

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

system/backup/search_dotfiles.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package backup
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
var DotfilePaths = []string{
9+
"~/.bashrc",
10+
"~/.zshrc",
11+
"~/.vimrc",
12+
"~/.config",
13+
"~/.bash_history",
14+
"~/.zsh_history",
15+
"~/.gitconfig",
16+
"~/.profile",
17+
"~/.npmrc",
18+
}
19+
20+
type Dotfile struct {
21+
Path string
22+
Found bool
23+
}
24+
25+
// Expand ~ to home directory
26+
func expandHome(path string) string {
27+
if strings.HasPrefix(path, "~") {
28+
if home, err := os.UserHomeDir(); err == nil {
29+
return strings.Replace(path, "~", home, 1)
30+
}
31+
}
32+
return path
33+
}
34+
35+
// Check which dotfiles exist
36+
func CheckDotfiles() ([]Dotfile, error) {
37+
var result []Dotfile
38+
39+
for _, rawPath := range DotfilePaths {
40+
fullPath := expandHome(rawPath)
41+
42+
_, err := os.Stat(fullPath)
43+
result = append(result, Dotfile{
44+
Path: fullPath,
45+
Found: err == nil,
46+
})
47+
}
48+
return result, nil
49+
}

0 commit comments

Comments
 (0)