diff --git a/instruction.go b/instruction.go index b9182db..ebb8f6e 100644 --- a/instruction.go +++ b/instruction.go @@ -15,10 +15,11 @@ type LinkInstruction struct { Target string Force bool Hard bool + Delete bool } func (instruction *LinkInstruction) String() string { - return fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s", SourceColor, instruction.Source, DefaultColor, deliminer, TargetColor, instruction.Target, DefaultColor, deliminer, strconv.FormatBool(instruction.Force), deliminer, strconv.FormatBool(instruction.Hard)) + return fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s", SourceColor, instruction.Source, DefaultColor, deliminer, TargetColor, instruction.Target, DefaultColor, deliminer, strconv.FormatBool(instruction.Force), deliminer, strconv.FormatBool(instruction.Hard), deliminer, strconv.FormatBool(instruction.Delete)) } func ParseInstruction(line string) (LinkInstruction, error) { @@ -41,6 +42,11 @@ func ParseInstruction(line string) (LinkInstruction, error) { res, _ := regexp.MatchString(`^(?i)\s*T|TRUE\s*$`, parts[3]) instruction.Hard = res } + if len(parts) > 4 { + res, _ := regexp.MatchString(`^(?i)\s*T|TRUE\s*$`, parts[4]) + instruction.Delete = res + instruction.Force = res + } instruction.Source, _ = ConvertHome(instruction.Source) instruction.Target, _ = ConvertHome(instruction.Target) @@ -75,7 +81,14 @@ func (instruction *LinkInstruction) RunSync() error { return fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) } } else { - return fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor) + if !instruction.Delete { + return fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor) + } + log.Printf("Deleting %s%s%s", TargetColor, instruction.Target, DefaultColor) + err = os.RemoveAll(instruction.Target) + if err != nil { + return fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + } } } else { return fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor) @@ -140,8 +153,16 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) { return } } else { - status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor) - return + if !instruction.Delete { + status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor) + return + } + log.Printf("Deleting %s%s%s", TargetColor, instruction.Target, DefaultColor) + err = os.RemoveAll(instruction.Target) + if err != nil { + status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return + } } } else { status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor) diff --git a/util.go b/util.go index 83570e2..ae5df7c 100644 --- a/util.go +++ b/util.go @@ -29,7 +29,7 @@ func FileExists(path string) bool { func NormalizePath(input string) string { workingdirectory, _ := os.Getwd() - input = strings.ReplaceAll(input, "\\", "/") + input = filepath.ToSlash(input) input = strings.ReplaceAll(input, "\"", "") if !filepath.IsAbs(input) {