From 9dd2350be95bcc931c6d4efca793217da388bd15 Mon Sep 17 00:00:00 2001 From: Ishan Goel Date: Mon, 15 Feb 2021 10:08:58 +0400 Subject: [PATCH 1/3] Use encoding/gob as format for log file and fix a bug. Incompatible change. --- rem.go | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/rem.go b/rem.go index f658bd1..bdc649a 100644 --- a/rem.go +++ b/rem.go @@ -2,12 +2,13 @@ package main import ( "bufio" + "encoding/gob" "fmt" + "io" "math/rand" "os" "path/filepath" "strconv" - "strings" "time" "github.com/fatih/color" @@ -28,10 +29,10 @@ Options: -t/--set-trash set trash to dir and continue -h/--help print this help message -v/--version print Rem version` - home, _ = os.UserHomeDir() - trashDir = home + "/.remTrash" - logFileName = ".trash.log" - logSeparator = "\t==>\t" + home, _ = os.UserHomeDir() + trashDir = home + "/.remTrash" + logFileName = ".trash.log" + //logSeparator = "\t==>\t" ) func main() { @@ -109,7 +110,7 @@ func main() { } func listFilesInTrash() []string { - m := parseLogFile() + m := getLogFile() s := make([]string, 0, 10) for key := range m { s = append(s, key) @@ -121,7 +122,7 @@ func emptyTrash() { permanentlyDeleteFile(trashDir) } -func parseLogFile() map[string]string { +func getLogFile() map[string]string { ensureTrashDir() file, err := os.OpenFile(trashDir+"/"+logFileName, os.O_CREATE|os.O_RDONLY, 0644) if err != nil { @@ -130,17 +131,10 @@ func parseLogFile() map[string]string { } defer file.Close() lines := make(map[string]string) - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := scanner.Text() - lastLogSeparator := strings.LastIndex(line, logSeparator) - from := line[:lastLogSeparator] // up to last logSeparator - pathInTrash := line[lastLogSeparator+1:] // after last logSeparator - lines[from] = pathInTrash - } - if scanner.Err() != nil { + enc := gob.NewDecoder(file) + err = enc.Decode(&lines) + if err != nil && err != io.EOF { handleErr(err) - return lines } return lines } @@ -154,12 +148,10 @@ func setLogFile(m map[string]string) { return } defer f.Close() - - for key, value := range m { - if _, err = f.WriteString(key + logSeparator + value + "\n"); err != nil { - handleErr(err) - return - } + enc := gob.NewEncoder(f) + err = enc.Encode(m) + if err != nil { + handleErr(err) } } @@ -169,7 +161,7 @@ func restore(path string) { handleErr(err) return } - logFile := parseLogFile() + logFile := getLogFile() fileInTrash, ok := logFile[path] if ok { err = os.Rename(fileInTrash, path) @@ -232,13 +224,13 @@ func trashFile(path string) { handleErr(err) return } - m := parseLogFile() + m := getLogFile() oldPath := path - i = 1 + i = 0 for ; existsInMap(m, path); i++ { // might be the same path as before path = oldPath + " " + strconv.Itoa(i) } - if i == 1 { + if i != 0 { fmt.Println("A file of this exact path was deleted earlier. To avoid conflicts, this file will now be called " + color.YellowString(path)) } m[path] = toMoveTo // logfile format is path where it came from ==> path in trash From 7678d14951e4b75a2384b4346c20db0453db4d65 Mon Sep 17 00:00:00 2001 From: Ishan Goel Date: Mon, 15 Feb 2021 10:23:35 +0400 Subject: [PATCH 2/3] Update --- rem.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rem.go b/rem.go index bdc649a..89dcfc0 100644 --- a/rem.go +++ b/rem.go @@ -15,7 +15,7 @@ import ( ) var ( - version = "dev" + version = "dev" // this is set on release build (check .goreleaser.yml) helpMsg = `Rem - Get some rem sleep knowing your files are safe Rem is a CLI Trash Usage: rem [-t/--set-trash ] [--permanent | -u/--undo] file @@ -131,8 +131,8 @@ func getLogFile() map[string]string { } defer file.Close() lines := make(map[string]string) - enc := gob.NewDecoder(file) - err = enc.Decode(&lines) + dec := gob.NewDecoder(file) + err = dec.Decode(&lines) if err != nil && err != io.EOF { handleErr(err) } @@ -150,7 +150,7 @@ func setLogFile(m map[string]string) { defer f.Close() enc := gob.NewEncoder(f) err = enc.Encode(m) - if err != nil { + if err != nil && err != io.EOF { handleErr(err) } } @@ -226,11 +226,11 @@ func trashFile(path string) { } m := getLogFile() oldPath := path - i = 0 + i = 1 for ; existsInMap(m, path); i++ { // might be the same path as before path = oldPath + " " + strconv.Itoa(i) } - if i != 0 { + if i != 1 { fmt.Println("A file of this exact path was deleted earlier. To avoid conflicts, this file will now be called " + color.YellowString(path)) } m[path] = toMoveTo // logfile format is path where it came from ==> path in trash From 5ab34b00161a33c7b31db018067ebf427f300079 Mon Sep 17 00:00:00 2001 From: Ishan Goel Date: Mon, 15 Feb 2021 10:56:43 +0400 Subject: [PATCH 3/3] Filenames with spaces should have quotes around them --- rem.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rem.go b/rem.go index 89dcfc0..c961360 100644 --- a/rem.go +++ b/rem.go @@ -209,7 +209,7 @@ func trashFile(path string) { toMoveTo = trashDir + "/" + filepath.Base(path) + " Deleted at " + time.Now().Format(time.StampNano) fmt.Println("You are a god.") case 4: - rand.Seed(time.Now().UTC().UnixNano()) // prep for default + rand.Seed(time.Now().UTC().UnixNano()) // prep for default case default: // nano-freaking-seconds aren't enough for this guy fmt.Println("(speechless)") if i == 4 { // seed once @@ -233,9 +233,9 @@ func trashFile(path string) { if i != 1 { fmt.Println("A file of this exact path was deleted earlier. To avoid conflicts, this file will now be called " + color.YellowString(path)) } - m[path] = toMoveTo // logfile format is path where it came from ==> path in trash + m[path] = toMoveTo // format is path where it came from ==> path in trash setLogFile(m) - fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+path)) + fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo \""+path+"\"")) } func exists(path string) bool {