Skip to content

Commit

Permalink
Fix set-trash
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed Feb 10, 2021
1 parent 6594180 commit c25b62a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var (
Rem is a CLI Trash
Usage: rem [<option>]
rem [-t/--set-trash <dir>] [-u/--undo | --permanent] file
Options:
-t/--set-trash <dir> set trash to dir and continue
-u/--undo restore a file
Expand All @@ -34,6 +33,7 @@ Options:
)

func main() {
trashDir, _ = filepath.Abs(trashDir)
if len(os.Args) == 1 {
handleErrStr("too few arguments")
fmt.Println(helpMsg)
Expand All @@ -47,12 +47,18 @@ func main() {
fmt.Println("Rem " + version)
return
}
if hasOption, _ := argsHaveOption("directory", "d"); hasOption {
fmt.Println(trashDir)
return
}
if hasOption, _ := argsHaveOption("list", "l"); hasOption {
printFormattedList(listFilesInTrash())
if hasOption, i := argsHaveOptionLong("permanent"); hasOption {
if !(len(os.Args) > i+1) {
handleErrStr("not enough arguments for --permanent")
return
}
color.Red("Warning, permanently deleting: ")
printFormattedList(os.Args[i+1:])
if promptBool("Confirm delete?") {
for _, filePath := range os.Args[i+1:] {
permanentlyDeleteFile(filePath)
}
}
return
}
if hasOption, i := argsHaveOption("set-trash", "t"); hasOption {
Expand All @@ -61,10 +67,20 @@ func main() {
return
}
//fmt.Println("Using " + os.Args[i+1] + " as trash")
trashDir = os.Args[i+1]
os.Args = removeElemFromSlice(os.Args, i+1) // remove the specified dir too
os.Args = removeElemFromSlice(os.Args, i)
main()
return
}
if hasOption, _ := argsHaveOption("directory", "d"); hasOption {
fmt.Println(trashDir)
return
}
if hasOption, _ := argsHaveOption("list", "l"); hasOption {
printFormattedList(listFilesInTrash())
return
}
if hasOption, _ := argsHaveOptionLong("empty-trash"); hasOption {
color.Red("Warning, permanently deleting these files in trash: ")
printFormattedList(listFilesInTrash())
Expand All @@ -73,28 +89,13 @@ func main() {
}
return
}
if hasOption, i := argsHaveOptionLong("permanent"); hasOption {
if !(len(os.Args) > i+1) {
handleErrStr("not enough arguments for --permanent")
return
}
color.Red("Warning, permanently deleting: ")
printFormattedList(os.Args[i+1:])
if promptBool("Confirm delete?") {
for _, filePath := range os.Args[i+1:] {
permanentlyDeleteFile(filePath)
}
}
return
}

if hasOption, i := argsHaveOption("undo", "u"); hasOption {
if !(len(os.Args) > i+1) {
handleErrStr("not enough arguments for --undo")
return
}
for _, filePath := range os.Args[i+1:] {
putBack(filePath)
restore(filePath)
}
return
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func setLogFile(m map[string]string) {
}
}

func putBack(path string) {
func restore(path string) {
path, err := filepath.Abs(path)
if err != nil {
handleErr(err)
Expand All @@ -171,7 +172,7 @@ func putBack(path string) {
return
}
} else {
handleErrStr("file not in trash or missing put back data")
handleErrStr("file not in trash or missing restore data")
return
}
delete(logFile, path)
Expand All @@ -195,6 +196,7 @@ func trashFile(path string) {
handleErrStr(color.YellowString(path) + " does not exist")
return
}
//TODO: add a timestamp so files can't clash
if i, err := os.Stat(toMoveTo); !(os.IsNotExist(err)) {
handleErrStr("file with name " + color.YellowString(i.Name()) + " already in trash at " + color.YellowString(toMoveTo)) // as helpful as possible
return
Expand Down
Empty file added test-set-trash/.trash.log
Empty file.

0 comments on commit c25b62a

Please sign in to comment.