Skip to content

Commit

Permalink
Merge pull request #2 from Arkaeriit/XDG_complience
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed May 23, 2022
2 parents 31feb0c + 2eb9eaa commit 83a01ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- amd64
- arm
- arm64
- 386
- '386'
ldflags:
- -s -w -X main.version=v{{.Version}}
ignore: # problems with build
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ Simply remove the executable or use:
brew uninstall rem
```

Rem stores its trash by default at `~/.remTrash`.

## Usage

```text
Usage: rem [-t/--set-trash <dir>] [--disable-copy] [--permanent | -u/--undo] <file> ...
Usage: rem [-t/--set-dir <dir>] [--disable-copy] [--permanent | -u/--undo] <file> ...
rem [-d/--directory | --empty | -h/--help | -v/--version | -l/--list]
Options:
-u/--undo restore a file
-l/--list list files in trash
--empty empty the trash permanently
--permanent delete a file permanently
-d/--directory show path to trash
-t/--set-trash <dir> set trash to dir and continue
-d/--directory show path to the data dir
-t/--set-dir <dir> set the data dir and continue
--disable-copy if files are on a different fs, don't rename by copy
-h/--help print this help message
-v/--version print Rem version
```

Rem stores its data at `$XDG_DATA_HOME/rem` or `.local/share/rem` by default. Alternatively, set the data directory using `$REM_TRASH` or with the `-d` option.

## Thanks

Thanks to [u/skeeto](https://www.reddit.com/user/skeeto/) for helping me with race conditions and design [here](https://www.reddit.com/r/golang/comments/lixr6k/rem_the_trash_cli_that_makes_it_ridiculously_easy/gn7z86z?utm_source=share&utm_medium=web2x&context=3)
Expand Down
57 changes: 37 additions & 20 deletions rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ var (
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>] [--disable-copy] [--permanent | -u/--undo] <file> ...
Usage: rem [-t/--set-dir <dir>] [--disable-copy] [--permanent | -u/--undo] <file> ...
rem [-d/--directory | --empty | -h/--help | -v/--version | -l/--list]
Options:
-u/--undo restore a file
-l/--list list files in trash
--empty empty the trash permanently
--permanent delete a file permanently
-d/--directory show path to trash
-t/--set-trash <dir> set trash to dir and continue
-d/--directory show path to the data dir
-t/--set-dir <dir> set the data dir and continue
--disable-copy if files are on a different fs, don't rename by copy
-h/--help print this help message
-v/--version print Rem version`
home, _ = os.UserHomeDir()
trashDir = home + "/.remTrash"
dataDir string
logFileName = ".trash.log"
logFile map[string]string
renameByCopyIsAllowed = true
Expand All @@ -42,7 +41,6 @@ Options:
// TODO: Multiple Rem instances could clobber log file. Fix using either file locks or tcp port locks.

func main() {
trashDir, _ = filepath.Abs(trashDir)
if len(os.Args) == 1 {
handleErrStr("too few arguments")
fmt.Println(helpMsg)
Expand Down Expand Up @@ -70,29 +68,32 @@ func main() {
}
return
}
if hasOption, i := argsHaveOption("set-trash", "t"); hasOption {

dataDir, _ = filepath.Abs(chooseDataDir())

if hasOption, i := argsHaveOption("set-dir", "t"); hasOption {
if !(len(os.Args) > i+1) {
handleErrStr("Not enough arguments for --set-trash")
handleErrStr("Not enough arguments for --set-dir")
return
}
//fmt.Println("Using " + os.Args[i+1] + " as trash")
trashDir = os.Args[i+1]
dataDir = 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)
fmt.Println(dataDir)
return
}
if hasOption, _ := argsHaveOption("list", "l"); hasOption {
printFormattedList(listFilesInTrash())
return
}
if hasOption, _ := argsHaveOptionLong("empty"); hasOption {
color.Red("Warning, permanently deleting all files in " + trashDir)
color.Red("Warning, permanently deleting all files in " + dataDir + "/trash")
if promptBool("Confirm delete?") {
emptyTrash()
}
Expand Down Expand Up @@ -150,7 +151,7 @@ func trashFile(path string) {
var toMoveTo string
var err error
path = filepath.Clean(path)
toMoveTo = trashDir + "/" + filepath.Base(path)
toMoveTo = dataDir + "/trash/" + filepath.Base(path)
if path == toMoveTo { // small edge case when trashing a file from trash
handleErrStr(color.YellowString(path) + " is already in trash")
return
Expand Down Expand Up @@ -234,15 +235,16 @@ func listFilesInTrash() []string {
}

func emptyTrash() {
permanentlyDeleteFile(trashDir)
permanentlyDeleteFile(dataDir + "/trash")
permanentlyDeleteFile(dataDir + "/" + logFileName)
}

func getLogFile() map[string]string {
if logFile != nil {
return logFile
}
ensureTrashDir()
file, err := os.OpenFile(trashDir+"/"+logFileName, os.O_CREATE|os.O_RDONLY, 0644)
file, err := os.OpenFile(dataDir+"/"+logFileName, os.O_CREATE|os.O_RDONLY, 0644)
if err != nil {
handleErr(err)
return nil
Expand All @@ -260,7 +262,7 @@ func getLogFile() map[string]string {
func setLogFile(m map[string]string) {
//f, err := os.OpenFile(trashDir+"/"+logFileName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) // truncate to empty, create if not exist, write only
ensureTrashDir()
f, err := os.Create(trashDir + "/" + logFileName)
f, err := os.Create(dataDir + "/" + logFileName)
if err != nil {
handleErr(err)
return
Expand All @@ -285,21 +287,36 @@ func existsInLog(elem string) bool {
}

func ensureTrashDir() {
i, _ := os.Stat(trashDir)
if !exists(trashDir) {
err := os.MkdirAll(trashDir, os.ModePerm)
i, _ := os.Stat(dataDir + "/trash")
if !exists(dataDir + "/trash") {
err := os.MkdirAll(dataDir+"/trash", os.ModePerm)
if err != nil {
handleErr(err)
return
}
return
}
if !i.IsDir() {
permanentlyDeleteFile(trashDir) // not a dir so delete
ensureTrashDir() // then make it
permanentlyDeleteFile(dataDir + "/trash") // not a dir so delete
ensureTrashDir() // then make it
}
}

// chooseDataDir returns the best directory to store data based on $REM_TRASH and $XDG_DATA_HOME,
// using ~/.local/share/rem as the default if neither is set.
func chooseDataDir() string {
home, _ := os.UserHomeDir()
remEnv := os.Getenv("REM_DATADIR")
if remEnv != "" {
return remEnv
}
dataHome := os.Getenv("XDG_DATA_HOME")
if dataHome != "" {
return dataHome + "/rem/trash"
}
return home + "/.local/share/rem"
}

func permanentlyDeleteFile(fileName string) {
err := os.RemoveAll(fileName)
if err != nil {
Expand Down

0 comments on commit 83a01ea

Please sign in to comment.