-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.go
106 lines (83 loc) · 2.29 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package dojoBuilder
import (
"os"
"path/filepath"
"syscall"
)
var (
installExcludeFunc ExcludeFunc = func(path string, f os.FileInfo) (bool, error) {
return false, nil
}
// DefaultInstallExcludeFunc skips .git folder, .gitignore and .gitattributes files
DefaultInstallExcludeFunc = func(path string, f os.FileInfo) (bool, error) {
var skippedFilesPatterns []string = []string{`\.gitignore`, `\.gitattributes`}
var skippedDirsPatterns []string = []string{`\.git`}
if f.IsDir() {
return IsMatchSliceMember(skippedDirsPatterns, path)
}
return IsMatchSliceMember(skippedFilesPatterns, path)
}
)
func SetInstallExcludeFunc(exFunc ExcludeFunc) { installExcludeFunc = exFunc }
func (c *Config) installFiles() (err error) {
// Delete obsolete symlink and folders
err = filepath.Walk(c.DestDir, func(path string, f os.FileInfo, err error) (_err error) {
var forceRemove bool
if path == c.DestDir {
return nil
}
srcPath := c.SrcDir + path[len(c.DestDir):]
if forceRemove, _err = installExcludeFunc(srcPath, f); _err != nil {
return _err
}
if _, err = os.Stat(srcPath); os.IsNotExist(err) || forceRemove {
if err = os.RemoveAll(path); err != nil {
return err
}
}
return
})
if err != nil {
return
}
// Create new symlinks and folders
err = filepath.Walk(c.SrcDir, func(path string, f os.FileInfo, err error) (_err error) {
if path == c.SrcDir {
return nil
}
newPath := c.DestDir + path[len(c.SrcDir):]
if _, err = os.Stat(newPath); err == nil {
return
}
isDir := f.IsDir()
if skip, _err := installExcludeFunc(path, f); _err != nil {
return err
} else if skip {
if isDir {
return filepath.SkipDir
}
return nil
} else if isDir {
if _err = os.Mkdir(newPath, 0754); _err != nil {
return _err
}
} else if f.Mode()&os.ModeSymlink == os.ModeSymlink {
origPath, _err := filepath.EvalSymlinks(path)
if _err != nil {
return _err
}
// fmt.Printf("Path : %s\nPoints to : %s\n\n", path, origPath)
if _err = os.Symlink(origPath, c.DestDir+path[len(c.SrcDir):]); _err != nil {
return _err
}
} else {
if _err = os.Link(path, c.DestDir+path[len(c.SrcDir):]); _err != nil {
return _err
}
}
st := f.Sys().(*syscall.Stat_t)
os.Chown(newPath, int(st.Uid), int(st.Gid))
return
})
return
}