-
Notifications
You must be signed in to change notification settings - Fork 2
/
linker.go
104 lines (84 loc) · 2.06 KB
/
linker.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
package main
import (
"fmt"
"os"
"path"
. "github.com/logrusorgru/aurora"
)
type Linker struct {
outputer IOutputer
os OS
}
func NewLinker(outputer IOutputer, os OS) Linker {
return Linker{
outputer: outputer,
os: os,
}
}
// Move moves oldpath to newpath, creating target directories if need.
func (l *Linker) Move(oldpath, newpath string) error {
// check if oldpath file exists
_, err := l.os.Stat(oldpath)
if l.os.IsNotExist(err) {
return fmt.Errorf("File %s not exists", oldpath)
}
if err != nil {
return err
}
err = l.os.MkdirAll(path.Dir(newpath), 0700)
if err != nil {
return err
}
l.outputer.OutVerbose(" %s backup %s to %s", Green("→"), Brown(oldpath), Brown(newpath))
err = l.os.Rename(oldpath, newpath)
return err
}
// SetSymlink symlinks scrAbs to destAbs.
func (l *Linker) SetSymlink(srcAbs string, destAbs string) error {
dir := path.Dir(destAbs)
if err := l.os.MkdirAll(dir, 0700); err != nil {
return err
}
return l.os.Symlink(srcAbs, destAbs)
}
// NeedSymlink reports whether source file needs to be symlinked to destination path.
func (l *Linker) NeedSymlink(src, dest string) (bool, error) {
fi, err := l.os.Lstat(dest)
if l.os.IsNotExist(err) {
return true, nil
}
if err != nil {
return false, err
}
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
return true, nil
}
target, err := l.os.Readlink(dest)
if err != nil {
return false, err
}
if target == src {
l.outputer.OutVerbose(" %s %s is correct symlink", Green("✓"), Brown(dest))
return false, nil
}
// here dest is a wrong symlink
if err = l.os.Remove(dest); err != nil {
return false, err
}
l.outputer.OutInfo(" %s delete wrong symlink %s", Green("✓"), Brown(dest))
return true, nil
}
// NeedBackup reports whether destination path needs to be backed up.
func (l *Linker) NeedBackup(dest string) (bool, error) {
fi, err := l.os.Lstat(dest)
if l.os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
return true, nil
}
return false, nil
}