Implement deleting real actual files
This commit is contained in:
@@ -15,10 +15,11 @@ type LinkInstruction struct {
|
|||||||
Target string
|
Target string
|
||||||
Force bool
|
Force bool
|
||||||
Hard bool
|
Hard bool
|
||||||
|
Delete bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (instruction *LinkInstruction) String() string {
|
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) {
|
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])
|
res, _ := regexp.MatchString(`^(?i)\s*T|TRUE\s*$`, parts[3])
|
||||||
instruction.Hard = res
|
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.Source, _ = ConvertHome(instruction.Source)
|
||||||
instruction.Target, _ = ConvertHome(instruction.Target)
|
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)
|
return fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
return fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor)
|
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
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
if !instruction.Delete {
|
||||||
return
|
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 {
|
} else {
|
||||||
status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor)
|
status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor)
|
||||||
|
2
util.go
2
util.go
@@ -29,7 +29,7 @@ func FileExists(path string) bool {
|
|||||||
|
|
||||||
func NormalizePath(input string) string {
|
func NormalizePath(input string) string {
|
||||||
workingdirectory, _ := os.Getwd()
|
workingdirectory, _ := os.Getwd()
|
||||||
input = strings.ReplaceAll(input, "\\", "/")
|
input = filepath.ToSlash(input)
|
||||||
input = strings.ReplaceAll(input, "\"", "")
|
input = strings.ReplaceAll(input, "\"", "")
|
||||||
|
|
||||||
if !filepath.IsAbs(input) {
|
if !filepath.IsAbs(input) {
|
||||||
|
Reference in New Issue
Block a user