-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlink.go
163 lines (139 loc) · 2.8 KB
/
unlink.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
package main
import (
"os"
"github.com/urfave/cli/v2"
)
type Unlink struct {
ctx *Context
}
func (me *Unlink) 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"},
},
}
return nil
}
type UnlinkOptions struct {
Pretend bool
AutoMode bool
}
func (me *Unlink) Run(c *cli.Context) error {
opts := &UnlinkOptions{}
configfiles := me.ctx.getConfigFiles(c)
opts.Pretend = c.Bool("pretend")
opts.AutoMode = c.Bool("auto")
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 *Unlink) RunFile(opts *UnlinkOptions, path string) error {
log.Tracef("runfile %s", path)
cfg := GetDefaultConfig()
err := cfg.LoadYaml(path)
if err != nil {
return err
}
if log.IsTrace() {
cfg.PrintConfig()
}
return me.RunConfig(opts, cfg, path)
}
func (me *Unlink) RunConfig(opts *UnlinkOptions, cfg *AppConfig, path string) error {
p := NewRunParamsConfig(cfg)
run, err := CompileRun(path, p)
if err != nil {
return err
}
err = Mkdirs(run.HomeDir, cfg.Mkdirs)
if err != nil {
return err
}
err = DoUnlinks(opts, run)
if err != nil {
return err
}
return nil
}
func DoUnlinks(opts *UnlinkOptions, run *Run) error {
var (
err error
removed int
)
for _, li := range run.Links {
if opts.Pretend {
if li.NeedsCreate == false {
log.Infof("unlink %q %q", li.AbsLink, li.Target)
}
continue
}
if li.NeedsCreate == false {
log.Tracef("Remove %s", li.Target)
err = os.Remove(li.Target)
if err != nil {
log.Warnf("Remove %s: %s", li.Target, err)
continue
}
removed++
}
}
if removed > 0 {
log.Infof("removed %d links", removed)
}
return nil
}
func (me *Unlink) RunAutoMode(opts *UnlinkOptions) 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 = DoUnlinks(opts, run)
if err != nil {
return err
}
return nil
}