Implement undoing

This commit is contained in:
2025-03-10 18:52:55 +01:00
parent 8c5d783d2c
commit ae1986cb81
2 changed files with 35 additions and 3 deletions

View File

@@ -49,12 +49,36 @@ func (instruction *LinkInstruction) String() string {
flagsStr = " [" + strings.Join(flags, ", ") + "]"
}
return fmt.Sprintf("%s%s%s → %s%s%s%s",
SourceColor, instruction.Source, DefaultColor,
TargetColor, instruction.Target, DefaultColor,
return fmt.Sprintf("%s%s%s",
FormatSourcePath(instruction.Source),
FormatTargetPath(instruction.Target),
flagsStr)
}
func (instruction *LinkInstruction) Undo() {
if !FileExists(instruction.Target) {
return
}
isSymlink, err := IsSymlink(instruction.Target)
if err != nil {
LogError("could not determine whether %s is a sym link or not, stopping; err: %v",
FormatTargetPath(instruction.Target), err)
return
}
if isSymlink {
LogInfo("Removing symlink at %s", FormatTargetPath(instruction.Target))
err = os.Remove(instruction.Target)
if err != nil {
LogError("could not remove symlink at %s; err: %v",
FormatTargetPath(instruction.Target), err)
}
} else {
LogInfo("%s is not a symlink, skipping", FormatTargetPath(instruction.Target))
}
}
func ParseInstruction(line, workdir string) (LinkInstruction, error) {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
@@ -131,6 +155,11 @@ func isTrue(value string) bool {
func (instruction *LinkInstruction) RunAsync(status chan (error)) {
defer close(status)
if undo {
instruction.Undo()
return
}
if !FileExists(instruction.Source) {
status <- fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source))
return