-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_install.go
258 lines (211 loc) · 5.5 KB
/
cmd_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
)
type InstallOptions struct {
global GlobalOptions
DisableCheck bool
}
var installOpts InstallOptions
func init() {
cmdRoot.AddCommand(cmdInstall)
cmdInstall.Flags().BoolVar(&installOpts.DisableCheck, "disable-check", false, "don't check for broken symlinks on install")
}
var cmdInstall = &cobra.Command{
Use: "install",
Short: "install modules",
RunE: func(cmd *cobra.Command, args []string) error {
opts := installOpts
opts.global = globalOpts
return runInstall(cmd, opts, args)
},
}
func readdir(dir string) []os.FileInfo {
f, err := os.Open(dir)
ok(err)
fis, err := f.Readdir(-1)
ok(err)
ok(f.Close())
return fis
}
func subdirs(dir string) (dirs []string) {
for _, fi := range readdir(dir) {
if !fi.IsDir() {
continue
}
name := fi.Name()
if name == ".git" || name == "manage" || name == "old" {
continue
}
dirs = append(dirs, name)
}
return dirs
}
func isSymlink(fi os.FileInfo) bool {
return (fi.Mode() & os.ModeSymlink) != 0
}
func isDir(name string) bool {
fi, err := os.Stat(name)
ok(err)
return fi.Mode().IsDir()
}
// exists checks if the target name points to exists.
func exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
// linkDirContents links all entries in srcdir to dstdir.
func linkDirContents(opts InstallOptions, srcdir, dstdir string) {
for _, entry := range readdir(srcdir) {
link(opts, filepath.Join(srcdir, entry.Name()), dstdir)
}
}
// resolveConflict resolves a conflict where two sources (src1 exists, src2 not
// yet) are to be linked to the name dst.
func resolveConflict(opts InstallOptions, src1, src2, dst string) {
v("resolve conflict for %v:\n %v\n %v\n", dst, src1, src2)
if !isDir(src1) {
warn("unable to resolve conflict for %v: source %v is not a directory\n", dst, src1)
return
}
if !isDir(src2) {
warn("unable to resolve conflict for %v: source %v is not a directory\n", dst, src2)
}
// first, remove the symlink
ok(os.Remove(dst))
// create a new directory
ok(os.Mkdir(dst, 0755))
// then link the file separately
linkDirContents(opts, src1, dst)
linkDirContents(opts, src2, dst)
v("conflict for %v resolved\n", dst)
}
// link creates a symlink to the file/dir src in targetdir.
func link(opts InstallOptions, src, targetdir string) {
base := filepath.Base(src)
if len(base) == 0 {
warn("invalid item name %v, skipping\n", src)
return
}
dst := filepath.Join(targetdir, base)
fi, err := os.Lstat(dst)
if err == nil {
if !isSymlink(fi) && !fi.IsDir() {
warn("skipping already existing item %v (neither symlink nor directory)\n", dst)
return
}
if fi.IsDir() {
v("%v exists, descending into %v\n", dst, src)
linkDirContents(opts, src, dst)
return
}
linkTarget := readlink(dst)
switch {
case linkTarget == src:
// nothing to do, dst already points to src
return
case isSubdir(opts.global.Base, readlink(dst)):
resolveConflict(opts, linkTarget, src, dst)
return
case exists(dst) && isDir(dst):
v("descending into %v\n", linkTarget)
linkDirContents(opts, src, dst)
return
case exists(dst):
v("symlink %v already exists, skipping\n", dst)
return
default:
v("removing dangling symlink\n")
if !opts.global.DryRun {
ok(os.Remove(dst))
}
}
}
v("link %v -> %v\n", src, dst)
if !opts.global.DryRun {
ok(os.Symlink(src, dst))
}
}
func install(opts InstallOptions, moduleName, targetdir string) {
if moduleName == "" {
return
}
modulePath := filepath.Join(opts.global.Base, moduleName)
v("install %v\n", moduleName)
if !exists(modulePath) {
warn("module %v does not exist, skipping\n", moduleName)
return
}
linkDirContents(opts, modulePath, targetdir)
}
func cleanupBrokenLinks(opts InstallOptions) {
v("looking for broken symlinks (disable with `--disable-check`)...")
defer v(" done\n")
walkOurSymlinks(opts.global.Base, opts.global.Target, func(filename, target string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
d("check symlink %v\n", target)
// if the target exists, do nothing
if exists(target) {
return nil
}
v("removing broken symlink %v\n", filename)
return os.Remove(filename)
})
}
func runInstall(cmd *cobra.Command, opts InstallOptions, args []string) error {
v("run install\n")
hostname, err := os.Hostname()
if err != nil {
return err
}
if !opts.DisableCheck {
cleanupBrokenLinks(opts)
}
if len(args) == 0 {
// install only the base module
install(opts, "base", opts.global.Target)
if exists(filepath.Join(opts.global.Base, "base_"+hostname)) {
install(opts, "base_"+hostname, opts.global.Target)
}
return nil
}
if args[0] == "all" {
// install all modules
for _, module := range subdirs(opts.global.Base) {
install(opts, module, opts.global.Target)
if exists(filepath.Join(opts.global.Base, module+"_"+hostname)) {
install(opts, module+"_"+hostname, opts.global.Target)
}
}
return nil
}
// otherwise install the listed modules
for _, module := range args {
// install all modules
install(opts, module, opts.global.Target)
if exists(filepath.Join(opts.global.Base, module+"_"+hostname)) {
install(opts, module+"_"+hostname, opts.global.Target)
}
}
return nil
}
func readlink(file string) string {
target, err := os.Readlink(file)
ok(err)
return target
}
func isSubdir(dir, subdir string) bool {
for {
if len(subdir) <= len(dir) {
return dir == subdir
}
subdir = filepath.Dir(subdir)
if subdir == "." {
return false
}
}
}