-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
386 lines (330 loc) · 7.42 KB
/
link.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
type Link struct {
ctx *Context
}
func (me *Link) Flags() []cli.Flag {
return []cli.Flag{
&cli.StringSliceFlag{
Name: "config",
Usage: "config file",
Aliases: []string{"c"},
},
&cli.BoolFlag{
Name: "pretend",
Usage: "pretend mode",
Aliases: []string{"p"},
},
&cli.BoolFlag{
Name: "auto",
Usage: "auto mode",
Aliases: []string{"a"},
},
&cli.BoolFlag{
Name: "copy",
Usage: "copy file insetad of link",
// Aliases: []string{""},
},
}
return nil
}
type LinkOptions struct {
Pretend bool
AutoMode bool
Copy bool
}
func (me *Link) Run(c *cli.Context) error {
opts := &LinkOptions{}
configfiles := me.ctx.getConfigFiles(c)
opts.Pretend = c.Bool("pretend")
opts.AutoMode = c.Bool("auto")
opts.Copy = c.Bool("copy")
log.Tracef("%d files to execute", len(configfiles))
if len(configfiles) == 0 && opts.AutoMode == false {
log.Warnf("Nothing to do, try passing -c dotbot.yaml ")
return nil
}
for _, filename := range configfiles {
err := me.RunFile(opts, filename)
if err != nil {
log.Warnf("RunFile %s: %s", filename, err)
}
}
if opts.AutoMode {
err := me.RunAutoMode(opts)
if err != nil {
log.Warnf("RunAutoMode: %s", err)
}
}
return nil
}
func (me *Link) RunFile(opts *LinkOptions, path string) error {
log.Tracef("runfile %s", path)
cfg := GetDefaultConfig()
err := cfg.LoadYaml(path)
if err != nil {
return err
}
if log.IsTrace() {
cfg.PrintConfig()
}
err = me.RunConfig(path, opts, cfg)
if err != nil {
log.Errorf("RunConfig %s: %s", path, err)
return err
}
return nil
}
func (me *Link) RunConfig(path string, opts *LinkOptions, cfg *AppConfig) error {
// compile run
p := NewRunParamsConfig(cfg)
run, err := CompileRun(path, p)
if err != nil {
return err
}
err = Mkdirs(run.HomeDir, run.Mkdir)
if err != nil {
return err
}
err = DoCreateLinks(opts, run)
if err != nil {
return err
}
err = CleanLinks(opts, cfg.Clean, run.HomeDir)
if err != nil {
return err
}
return nil
}
func DoCreateLinks(opts *LinkOptions, run *Run) error {
err := RunScripts(opts, run, "pre")
if err != nil {
return err
}
err = CreateLinks(opts, run)
if err != nil {
return err
}
err = RunScripts(opts, run, "post")
if err != nil {
return err
}
return nil
}
func CreateLinks(opts *LinkOptions, run *Run) error {
var (
err error
created int
)
for _, li := range run.Links {
// validate we are not linking to ourselves
if li.AbsLink == li.Target {
log.Debugf("Ignoring symlink to self %s", li.AbsLink)
continue
}
if opts.Pretend {
if li.NeedsCreate {
log.Infof("create link %q %q", li.AbsLink, li.Target)
}
continue
}
if li.NeedsCreate {
if opts.Copy {
log.Infof("copy %q -> %q", li.Target, li.AbsLink)
err = Copy(li.Target, li.AbsLink)
if err != nil {
log.Warnf("Copy %s", err)
continue
}
} else {
if li.DestExists {
os.Remove(li.Target)
}
log.Infof("symlink %s", li.Target)
err = os.Symlink(li.AbsLink, li.Target)
if err != nil {
log.Warnf("Symlink %s", err)
continue
}
}
created++
}
}
if created > 0 {
log.Infof("created %d links", created)
}
return nil
}
func Copy(srcpath, dstpath string) (err error) {
r, err := os.Open(srcpath)
if err != nil {
return err
}
defer r.Close() // ignore error: file was opened read-only.
w, err := os.Create(dstpath)
if err != nil {
return err
}
defer func() {
// Report the error from Close, if any,
// but do so only if there isn't already
// an outgoing error.
if c := w.Close(); c != nil && err == nil {
err = c
}
}()
_, err = io.Copy(w, r)
return err
}
func Mkdirs(homedir string, paths []string) error {
dirMode := os.FileMode(0755)
for _, path := range paths {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(homedir, path[2:])
}
st, err := os.Stat(path)
if err == nil && st.Mode().IsRegular() {
log.Warnf("file exists for mkdir %s", path)
continue
}
if err != nil {
log.Infof("mkdir %s", path)
os.MkdirAll(path, dirMode)
}
}
return nil
}
func (me *Link) RunAutoMode(opts *LinkOptions) error {
cfg := GetDefaultConfig()
cwd, err := os.Getwd()
if err != nil {
return err
}
filenames, err := ListDir(cwd)
if err != nil {
return err
}
// build config using current directory listing
for _, filename := range filenames {
if filename == ".git" {
continue
}
cfg.Symlinks["~/"+filename] = filename
}
p := NewRunParamsConfig(cfg)
run, err := CompileRun("", p)
if err != nil {
return err
}
err = DoCreateLinks(opts, run)
if err != nil {
return err
}
return nil
}
func RunScripts(opts *LinkOptions, run *Run, stype string) error {
log.Tracef("running scripts of type %s", stype)
for _, script := range run.Script {
if script.Type != stype {
log.Tracef("skip script %s type %s", script.Id, script.Type)
continue
}
err := script.Validate()
if err != nil {
log.Warnf("%s-script %s validate: %s", script.Type, script.Id, err)
continue
}
sres, err := script.Run()
if err != nil {
log.Warnf("%s-script %s: run: %s", script.Type, script.Id, err)
continue
}
log.Tracef("%s-script %s returned %s", script.Type, script.Id, sres)
}
return nil
}
// go through each glob and ensure its a directory
func CleanLinks(opts *LinkOptions, dirs []string, homedir string) error {
for _, glob_pattern := range dirs {
if strings.HasPrefix(glob_pattern, "~") {
glob_pattern = filepath.Join(homedir, glob_pattern[1:])
}
matches, err := filepath.Glob(glob_pattern)
if err != nil {
return err
}
err = CleanLinksGlob(opts, glob_pattern, matches)
if err != nil {
log.Warnf("Glob %s: %s", glob_pattern, err)
continue
}
}
return nil
}
func CleanLinksGlob(opts *LinkOptions, dir_pattern string, matches []string) error {
log.Tracef("CleanLinksGlob %s", dir_pattern)
for _, filename := range matches {
log.Tracef("filename %s", filename)
st, err := os.Stat(filename)
if err != nil {
log.Warnf("Stat %s: %s", filename, err)
continue
}
if st.IsDir() == false {
continue // we only want directories
}
dir := filename
ls, err := ListDir(dir)
if err != nil {
log.Warnf("ListDir %s: %s", dir, err)
continue
}
// go through each file and ensure it's a symlink and valid
for _, filename := range ls {
fullpath := filepath.Join(dir, filename)
log.Tracef("clean symlink %s", fullpath)
// stat the link
st, err := os.Lstat(fullpath)
if err != nil {
log.Warnf("Lstat %s: %s", fullpath, err)
}
is_symlink := false
if err == nil && st.Mode()&os.ModeSymlink == os.ModeSymlink {
is_symlink = true
}
// stat the file (and what the link points to)
_, err = os.Stat(fullpath)
stat_ok := (err == nil)
// if it's a symlink, get what it points to
var link string
if is_symlink {
link, err = os.Readlink(fullpath)
if err != nil {
log.Warnf("Readlink %s", err)
}
}
log.Tracef("points to %s", link)
// check if the symlink points to something invalid
dangling := false
if is_symlink == true && link != "" {
_, err := os.Stat(link)
if err != nil {
dangling = true
}
}
if dangling && stat_ok == false {
log.Tracef("dangling symlink %s is invalid, points to %s",
fullpath, link)
log.Infof("Remove dangling symlink %s", fullpath)
os.Remove(fullpath)
}
}
}
return nil
}