Implement deleting real actual files
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user