Skip to content

Commit

Permalink
Change encoding format to gob. Incompatible API change.
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed Feb 16, 2021
2 parents 7be5256 + 5ab34b0 commit d92ae31
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package main

import (
"bufio"
"encoding/gob"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/fatih/color"
)

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 <dir>] [--permanent | -u/--undo] file
Expand All @@ -28,10 +29,10 @@ Options:
-t/--set-trash <dir> 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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
dec := gob.NewDecoder(file)
err = dec.Decode(&lines)
if err != nil && err != io.EOF {
handleErr(err)
return lines
}
return lines
}
Expand All @@ -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 && err != io.EOF {
handleErr(err)
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -217,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
Expand All @@ -232,7 +224,7 @@ func trashFile(path string) {
handleErr(err)
return
}
m := parseLogFile()
m := getLogFile()
oldPath := path
i = 1
for ; existsInMap(m, path); i++ { // might be the same path as before
Expand All @@ -241,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 {
Expand Down

0 comments on commit d92ae31

Please sign in to comment.