-
Notifications
You must be signed in to change notification settings - Fork 6
/
linking.go
189 lines (156 loc) · 4.62 KB
/
linking.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
// Copyright 2018 Mathew Robinson <chasinglogic@gmail.com>. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package linking
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/chasinglogic/dfm/config"
"github.com/chasinglogic/dfm/filemap"
)
type Linker interface {
Link(mappings filemap.Mappings, config Config) error
}
// Config changes the behavior of linking functions, it acts as a
// store for common functoin arguments
type Config struct {
DryRun bool
Overwrite bool
}
// LinkInfo holds the src and destination for our symlink.
type LinkInfo struct {
Src string
Dest string
}
func (l *LinkInfo) String() string {
return fmt.Sprintf("%s -> %s", l.Dest, l.Src)
}
func (l *LinkInfo) Link(config Config) error {
if config.DryRun {
fmt.Println("Creating symlink", l)
}
e := removeIfNeeded(l, config)
if e != nil && strings.Contains(e.Error(), "cowardly") {
fmt.Println(e)
} else if e != nil {
return e
}
if !config.DryRun {
if e := os.Symlink(l.Src, l.Dest); e != nil {
return e
}
}
return nil
}
// getTargetName determines if we need to add a dot to the destination or not.
func getTargetName(n string) string {
if !strings.HasPrefix(n, ".") {
return "." + n
}
return n
}
// GenerateSymlink will create a LinkInfo with the appropriate destination,
// handling the XDG_config.CONFIG_HOME special case.
func GenerateSymlink(sourceDir, targetDir string, file os.FileInfo) LinkInfo {
target := getTargetName(file.Name())
if targetDir != os.Getenv("HOME") {
target = file.Name()
}
ln := LinkInfo{
filepath.Join(sourceDir, file.Name()),
filepath.Join(targetDir, target),
}
return ln
}
// removeIfNeeded will check if the link destination exists and delete it if
// appropriate.
func removeIfNeeded(link *LinkInfo, config Config) error {
info, err := os.Lstat(link.Dest)
if err == nil && (config.Overwrite || info.Mode()&os.ModeSymlink == os.ModeSymlink) {
if config.DryRun {
fmt.Printf("%s already exists, removing.\n", link.Dest)
}
if !config.DryRun {
if rmerr := os.Remove(link.Dest); rmerr != nil {
return fmt.Errorf("Unable to remove %s: %s",
link.Dest,
rmerr.Error())
}
}
} else if err == nil {
msg := fmt.Sprintf(
"%s already exists and is not a symlink, cowardly refusing to remove",
link.Dest,
)
return errors.New(msg)
}
return nil
}
// CreateSymlinks will read all of the files at sourceDir and link them to the
// appropriate location in targetDir, if there is a folder named config in
// sourceDir CreateSymlinks will run itself using that folder as sourceDir and
// targetDir as XDG_config.CONFIG_HOME or HOME/.config if XDG_config.CONFIG_HOME is not set.
func CreateSymlinks(sourceDir, targetDir string, config Config, mappings filemap.Mappings) error {
sourceDir, err := filepath.Abs(sourceDir)
if err != nil {
return err
}
links, err := GenerateSymlinks(sourceDir, targetDir, mappings)
if err != nil {
return err
}
for _, link := range links {
err := link.Link(config)
if err != nil {
return err
}
}
return nil
}
// GenerateSymlinks will create the symlinks so we know what they were supposed
// to be prior to removing the profile.
func GenerateSymlinks(profileDir, target string, mappings filemap.Mappings) ([]LinkInfo, error) {
var lnks []LinkInfo
files, err := ioutil.ReadDir(profileDir)
if err != nil {
return lnks, err
}
for _, file := range files {
// Handle XDG_config.CONFIG_HOME special case.
mapping := mappings.Matches(file.Name())
if mapping != nil {
if mapping.Skip {
continue
} else if mapping.IsDir {
newTargetDir := config.ExpandFilePath(mapping.Dest)
multiLinks, err := GenerateSymlinks(filepath.Join(profileDir, file.Name()), newTargetDir, mappings)
if err != nil {
return lnks, err
}
lnks = append(lnks, multiLinks...)
} else {
link := GenerateSymlink(profileDir, mapping.Dest, file)
lnks = append(lnks, link)
}
continue
}
link := GenerateSymlink(profileDir, target, file)
lnks = append(lnks, link)
}
return lnks, nil
}