+27
-11
src/dotfiles.gleam
+27
-11
src/dotfiles.gleam
···
23
23
FailedToCloneRepo(#(Int, String))
24
24
FailedToInitRepo(#(Int, String))
25
25
FailedToCreateDir(path: String, error: simplifile.FileError)
26
+
InsufficientPermissions(path: String, error: simplifile.FileError)
27
+
FileWasSymlink(String)
26
28
}
27
29
28
30
fn describe_error(error: InternalError) {
···
55
57
<> path
56
58
<> " "
57
59
<> simplifile.describe_error(inner)
60
+
InsufficientPermissions(path, inner) ->
61
+
string.inspect(error)
62
+
<> " "
63
+
<> path
64
+
<> " "
65
+
<> simplifile.describe_error(inner)
58
66
FailedToInitRepo(_) -> string.inspect(error)
67
+
FileWasSymlink(path) -> path <> " is a symlink. It will be skipped."
59
68
}
60
69
}
61
70
···
196
205
/// Returns the original spec
197
206
///
198
207
fn move_config_to_dotfiles(spec: Spec, home) {
199
-
// make sure the dotfiles path exists
200
-
let _ =
201
-
simplifile.create_directory_all(
202
-
filepath.directory_name(filepath.join(home, spec.dotfiles_path)),
203
-
)
208
+
let full_target_path = filepath.join(home, spec.target_path)
209
+
let full_dotfiles_path = filepath.join(home, spec.dotfiles_path)
210
+
211
+
case simplifile.is_symlink(full_target_path) {
212
+
Error(err) -> InsufficientPermissions(full_target_path, err) |> Error
213
+
Ok(True) -> FileWasSymlink(full_target_path) |> Error
214
+
Ok(False) -> {
215
+
// make sure the dotfiles path exists
216
+
let _ =
217
+
simplifile.create_directory_all(filepath.directory_name(
218
+
full_target_path,
219
+
))
204
220
205
-
simplifile.rename(
206
-
filepath.join(home, spec.target_path),
207
-
filepath.join(home, spec.dotfiles_path),
208
-
)
209
-
|> result.map_error(FailedToCopy(filepath.join(home, spec.dotfiles_path), _))
210
-
|> result.replace(spec)
221
+
// move the original config(target) to the new dotfiles path
222
+
simplifile.rename(full_target_path, full_dotfiles_path)
223
+
|> result.map_error(FailedToCopy(full_target_path, _))
224
+
|> result.replace(spec)
225
+
}
226
+
}
211
227
}
212
228
213
229
/// loads specs from spec.json and tries to create the symlinks based on it