-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
359 lines (289 loc) · 9.13 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
. "github.com/logrusorgru/aurora"
)
const logFilepath = "${HOME}/.dotbro/dotbro.log"
var debugLogger DebugLogger
var (
osfs = new(OSFS)
)
func main() {
var outputer = NewOutputer(OutputerModeNormal, os.Stdout, debugLogger)
initLogger(&outputer)
outputer.Logger = debugLogger
debugLogger.Write("Start.")
// Parse arguments
args, err := ParseArguments(nil)
if err != nil {
outputer.OutError("Error parsing aruments: %s", err)
exit(1)
}
debugLogger.Write("Arguments passed: %+v", args)
switch {
case args["--verbose"].(bool):
outputer.Mode = OutputerModeVerbose
case args["--quiet"].(bool):
outputer.Mode = OutputerModeQuiet
default:
outputer.Mode = OutputerModeNormal
}
// Process config
configPath := getConfigPath(args["--config"], &outputer)
debugLogger.Write("Parsing config file %s", configPath)
config, err := NewConfiguration(configPath)
if err != nil {
outputer.OutError("Cannot read configuration from file %s : %s.\n", configPath, err)
outputer.OutInfo("%s: Maybe you have renamed your config file?\nIf so, run dotbro with '--config' argument (see 'dotbro --help' for details).", Magenta("TIP"))
exit(1)
}
// Preparations
err = os.MkdirAll(config.Directories.Backup, 0700)
if err != nil && !os.IsExist(err) {
outputer.OutError("Error creating backup directory: %s", err)
exit(1)
}
outputer.OutVerbose("Dotfiles root: %s", Brown(config.Directories.Dotfiles))
outputer.OutVerbose("Dotfiles src: %s", Brown(config.Directories.Sources))
outputer.OutVerbose("Destination dir: %s", Brown(config.Directories.Destination))
// Select action
switch {
case args["add"]:
filename := args["<filename>"].(string)
if err = addAction(filename, config, &outputer); err != nil {
outputer.OutError("%s", err)
exit(1)
}
outputer.OutInfo("\n%s was successfully added to your dotfiles!", Brown(filename))
exit(0)
case args["clean"]:
if err = cleanAction(config, &outputer); err != nil {
outputer.OutError("%s", err)
exit(1)
}
outputer.OutInfo("\nCleaned!")
exit(0)
default:
// Default action: install
if err = installAction(config, &outputer); err != nil {
outputer.OutError("%s", err)
exit(1)
}
outputer.OutInfo("\nAll done (─‿‿─)")
exit(0)
}
}
func initLogger(outputer IOutputer) {
var filename = os.ExpandEnv(logFilepath)
if err := osfs.MkdirAll(filepath.Dir(filename), 0700); err != nil {
outputer.OutWarn("Cannot use log file %s. Reason: %s", filename, err)
return
}
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
outputer.OutWarn("Cannot use log file %s. Reason: %s", filename, err)
return
}
debugLogger = NewDebugLogger(log.New(f, "", log.Ldate|log.Ltime))
}
func addAction(filename string, config *Configuration, outputer IOutputer) error {
fileInfo, err := os.Lstat(filename)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s: no such file or directory", filename)
}
return err
}
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
return fmt.Errorf("Cannot add file %s - it is a symlink", filename)
}
if fileInfo.Mode().IsDir() {
return fmt.Errorf("Cannot add dir %s - directories are not supported yet.", filename)
}
outputer.OutVerbose("Adding file %s to dotfiles root %s", Brown(filename), Brown(config.Directories.Dotfiles))
// backup file
backupPath := config.Directories.Backup + "/" + path.Base(filename)
if err = Copy(osfs, filename, backupPath); err != nil {
return fmt.Errorf("Cannot backup file %s: %s", filename, err)
}
outputer.OutInfo(" %s backup %s to %s", Green("→"), Brown(filename), Brown(backupPath))
// Move file to dotfiles root
newPath := config.Directories.Dotfiles + "/" + path.Base(filename)
if err = os.Rename(filename, newPath); err != nil {
return err
}
linker := NewLinker(outputer, osfs)
// Add a symlink to the moved file
if err = linker.SetSymlink(newPath, filename); err != nil {
return err
}
// TODO: write to config file
return nil
}
func cleanAction(config *Configuration, outputer IOutputer) error {
cleaner := NewCleaner(outputer, osfs)
if err := cleaner.CleanDeadSymlinks(config.Directories.Destination); err != nil {
return fmt.Errorf("Error cleaning dead symlinks: %s", err)
}
return nil
}
func installAction(config *Configuration, outputer IOutputer) error {
// Default action: install
cleaner := NewCleaner(outputer, osfs)
err := cleaner.CleanDeadSymlinks(config.Directories.Destination)
if err != nil {
return fmt.Errorf("Error cleaning dead symlinks: %s", err)
}
srcDirAbs := config.Directories.Dotfiles
if config.Directories.Sources != "" {
if _, err = os.Stat(config.Directories.Sources); os.IsNotExist(err) {
return fmt.Errorf("Sources directory `%s' does not exist.", config.Directories.Sources)
}
if err != nil {
return fmt.Errorf("Error reading sources directory `%s': %s", config.Directories.Sources, err)
}
srcDirAbs += "/" + config.Directories.Sources
}
mapping := getMapping(config, srcDirAbs, outputer)
linker := NewLinker(outputer, osfs)
outputer.OutInfo("--> Installing dotfiles...")
// filter mapping:
// - non-existent files
// - already installed files
filterMapping(mapping, func(src, dst string) bool {
srcAbs := path.Join(srcDirAbs, src)
destAbs := path.Join(config.Directories.Destination, dst)
if _, err := osfs.Stat(srcAbs); err != nil {
if osfs.IsNotExist(err) {
outputer.OutWarn("Source file %s does not exist", srcAbs)
return false
}
outputer.OutError("Error processing source file %s: %s", src, err)
exit(1)
}
needSymlink, err := linker.NeedSymlink(srcAbs, destAbs)
if err != nil {
outputer.OutError("Error processing destination file %s: %s", destAbs, err)
exit(1)
}
return needSymlink
})
if len(mapping) == 0 {
return nil
}
outputer.OutInfo("From %s to %s :", Brown(srcDirAbs), Brown(config.Directories.Destination))
for src, dst := range mapping {
installDotfile(src, dst, linker, config, srcDirAbs, outputer)
}
return nil
}
func getConfigPath(configArg interface{}, outputer IOutputer) string {
var configPath string
if configArg != nil {
configPath = configArg.(string)
}
rc := NewRC()
var err error
// If config param is not passed to dotbro, read it from RC file.
if configPath == "" {
if err = rc.Load(); err != nil {
outputer.OutError("Error reading rc file: %s", err)
exit(1)
}
if rc.Config.Path == "" {
outputer.OutError("Config file not specified.")
exit(1)
}
outputer.OutVerbose("Got config path from file %s", Brown(RCFilepath))
return rc.Config.Path
}
// Save to RC file
configPath, err = filepath.Abs(configPath)
if err != nil {
outputer.OutError("Bad config path: %s", err)
exit(1)
}
rc.SetPath(configPath)
if err = rc.Save(); err != nil {
outputer.OutError("Cannot save rc file: %s", err)
exit(1)
}
outputer.OutVerbose("Saved config path to file %s", Brown(RCFilepath))
return rc.Config.Path
}
func getMapping(config *Configuration, srcDirAbs string, outputer IOutputer) map[string]string {
mapping := make(map[string]string)
if len(config.Mapping) == 0 {
// install all the things
outputer.OutVerbose("Mapping is not specified - install all the things")
dir, err := os.Open(srcDirAbs)
if err != nil {
outputer.OutError("Error reading dotfiles source dir: %s", err)
exit(1)
}
defer func() {
if err = dir.Close(); err != nil {
outputer.OutWarn("Error closing dir %s: $s", srcDirAbs, err.Error())
}
}()
files, err := dir.Readdir(0)
if err != nil {
outputer.OutError("Error reading dotfiles source dir: %s", err)
exit(1)
}
for _, fileInfo := range files {
mapping[fileInfo.Name()] = fileInfo.Name()
}
// filter excludes
for _, exclude := range config.Files.Excludes {
delete(mapping, exclude)
}
} else {
// install by mapping
if len(config.Files.Excludes) > 0 {
outputer.OutWarn("Excludes in config make no sense when mapping is specified, omitting them.")
}
mapping = config.Mapping
}
return mapping
}
func filterMapping(mapping map[string]string, callback func(src, dst string) bool) {
for src, dst := range mapping {
if !callback(src, dst) {
delete(mapping, src)
}
}
}
func installDotfile(src, dest string, linker Linker, config *Configuration, srcDirAbs string, outputer IOutputer) {
srcAbs := path.Join(srcDirAbs, src)
destAbs := path.Join(config.Directories.Destination, dest)
needBackup, err := linker.NeedBackup(destAbs)
if err != nil {
outputer.OutError("Error processing destination file %s: %s", destAbs, err)
exit(1)
}
if needBackup {
oldpath := destAbs
newpath := config.Directories.Backup + "/" + dest
err = linker.Move(oldpath, newpath)
if err != nil {
outputer.OutError("Error on file backup %s: %s", oldpath, err)
exit(1)
}
}
err = linker.SetSymlink(srcAbs, destAbs)
if err != nil {
outputer.OutError("Error creating symlink from %s to %s: %s", srcAbs, destAbs, err)
exit(1)
}
outputer.OutInfo(" %s set symlink %s -> %s", Green("+"), Brown(src), Brown(dest))
}
// exit actually calls os.Exit after logger logs exit message.
func exit(exitCode int) {
debugLogger.Write("Exit with code %d.", exitCode)
os.Exit(exitCode)
}