-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotm.go
273 lines (231 loc) · 5.82 KB
/
dotm.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
package dotm
import (
"context"
"errors"
"os"
"sort"
)
// New creates a new dotfile profile.
func New(p *Profile) error {
c, err := LoadOrCreateConfig()
if err != nil {
return err
}
p, err = c.AddProfile(p)
if err != nil {
return err
}
err = p.create()
if err != nil {
return err
}
return c.Write()
}
// InitOptions are the options used to initialize a dotfile profile.
type InitOptions struct {
LinkOptions
}
// Init initializes a new dotfile profile from an existing local path.
func Init(p *Profile, opts *InitOptions) error {
c, err := LoadOrCreateConfig()
if err != nil {
return err
}
p, err = c.AddProfileFromExistingPath(p)
if err != nil {
return err
}
err = p.link(opts.LinkOptions)
if err != nil {
return err
}
// Ignore error since remote detection is optional.
remoteURL, _ := p.detectRemote()
p.Remote = remoteURL
return c.Write()
}
// InstallOptions are the options used to install a dotfile profile.
type InstallOptions struct {
LinkOptions
}
// Install calls InstallWithContext with the background context.
func Install(p *Profile, opts *InstallOptions) error {
return InstallWithContext(context.Background(), p, opts)
}
// InstallWithContext installs a new dotfile profile by cloning the remote
// repository to the local path. The clone operation can be canceled with the
// passed context.
func InstallWithContext(ctx context.Context, p *Profile, opts *InstallOptions) error {
c, err := LoadOrCreateConfig()
if err != nil {
return err
}
p, err = c.AddProfile(p)
if err != nil {
return err
}
err = p.cloneRemote(ctx)
if err != nil {
return err
}
err = p.link(opts.LinkOptions)
if err != nil {
return err
}
return c.Write()
}
// Add adds the given file to the profile under the given top level dir.
// If the file already exists under $HOME/path, A backup is created and then
// copied to the profile.
func Add(profile, dir, path string) error {
c, err := LoadConfig()
if err != nil {
return err
}
p, err := c.Profile(profile)
if err != nil {
return err
}
return p.addFile(dir, path)
}
// UpdateOptions are the options used to update a dotfile profile.
type UpdateOptions struct {
FromRemote bool
ExecHooks bool
LinkOptions
}
// Update calls UpdateWithContext with the background context.
func Update(profile string, opts *UpdateOptions) error {
return UpdateWithContext(context.Background(), profile, opts)
}
// UpdateWithContext updates the symlinks for the given profile.
//
// When the given profile is empty all profiles get updated.
func UpdateWithContext(ctx context.Context, profile string, opts *UpdateOptions) error {
c, err := LoadConfig()
if err != nil {
return err
}
opts.LinkOptions.TraversalOptions.IgnorePrefix = c.IgnorePrefix
// When the profile name is empty update all profiles.
if profile == "" {
// The profiles are updated in ascending order sorted by the profile
// name.
profileNames := make([]string, 0, len(c.Profiles))
for name := range c.Profiles {
profileNames = append(profileNames, name)
}
sort.Strings(profileNames)
for _, name := range profileNames {
err = update(ctx, c.Profiles[name], opts)
if err != nil {
return err
}
}
return nil
}
p, err := c.Profile(profile)
if err != nil {
return err
}
return update(ctx, p, opts)
}
// update updates the symlinks for a given profile.
//
// When opts.FromRemote is set to true it first pull updates from the remote
// repository. This operation can be canceled with the passed context.
// When opts.ExecHooks is passed, pre and post update hooks get executed.
func update(ctx context.Context, p *Profile, opts *UpdateOptions) (err error) {
var hooks *Hooks
if opts.ExecHooks && p.HooksEnabled {
hooks, err = p.findHooks(&opts.TraversalOptions)
if err != nil {
return err
}
err = hooks.PreUpdate.Exec(ctx)
if err != nil {
return err
}
}
if opts.FromRemote {
err = p.pullRemote(ctx)
if err != nil {
return err
}
}
err = p.link(opts.LinkOptions)
if err != nil {
return err
}
if opts.ExecHooks && p.HooksEnabled {
err = hooks.PostUpdate.Exec(ctx)
if err != nil {
return err
}
}
return nil
}
// UninstallOptions are the options used to uninstall a dotfile profile.
type UninstallOptions struct {
Dry bool // Dry performes a dry run
Clean bool // Clean also removes the local dotfile path and the config entry
}
// Uninstall unlinks a dotfile profile. If any backup files exist, they get
// restored.
func Uninstall(profile string, opts *UninstallOptions) error {
c, err := LoadOrCreateConfig()
if err != nil {
return err
}
p, err := c.Profile(profile)
if err != nil {
return err
}
err = p.unlink(opts.Dry)
if err != nil {
return err
}
if opts.Clean {
if err := os.RemoveAll(p.expandedPath); err != nil {
return err
}
delete(c.Profiles, profile)
return c.Write()
}
return nil
}
var oldConfigPath = os.ExpandEnv("$HOME/.dotfiles/dotm.toml")
// Fix tries to fix the configuration file, after breaking changes were
// introduced.
//
// List of things that get fixed:
// - [0.3.0] move config from olf location at $HOME/.dotfiles/dotm.toml
// - [0.3.0] set ignore_prefix to "_", when not set
// - [0.4.0] set hooks_enabled to true, when not set
func Fix() error {
c, meta, err := loadConfigWithMetaData(configPath)
if err != nil {
// When the config file does not exist, try to load the old config file.
var e *os.PathError
if errors.As(err, &e) {
c, meta, err = loadConfigWithMetaData(oldConfigPath)
if err != nil {
return err
}
defer os.Remove(oldConfigPath)
} else {
return err
}
}
// Set missing ignore_prefix to "_".
if !meta.IsDefined("ignore_prefix") {
c.IgnorePrefix = "_"
}
// Set missing "hooks_enabled" to true for all profiles.
for name, p := range c.Profiles {
if !meta.IsDefined("profiles." + name + ".hooks_enabled") {
p.HooksEnabled = true
}
}
return c.Write()
}