Skip to content

Commit

Permalink
Remove gnu rm command as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Oct 29, 2024
1 parent fee5c27 commit 9b817cc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 33 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug i
- `--bypass-protected` Using this flag will allow you to delete a file protected by the 2rm config
- `--notify` Send a system notification once deletion is complete

## Unsupported command line arguments

- `-r`, `-R`, `--recursive`
- `-d`
- `-v`
- `--version`
- `--help`
- `--interactive[=WHEN]`
- `--one-file-system`
- `-f`, `--force` (partially implemented)

## Features

### Removes the ability to remove your root directory
Expand Down
4 changes: 4 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ You can override this behavior with the `-f` or `--force` flags.
```yml
# ~/.local/share/2rm/config.yml

# extend a config file that is based in the cloud
# this allows you to use the same config across multiple machines
extend: https://www.my-website.com/2rm.yml

# while the backup config option is already supported, we should support
# uploading backups to cloud locations such as s3
backups: s3://my-bucket/backups
Expand Down
4 changes: 4 additions & 0 deletions src/cli/args.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cli

// 2rm CLI arguments
const HARD_DELETE_CLA = "--hard"
const SOFT_DELETE_CLA = "--soft"
const SILENT_CLA = "--silent"
const DRY_RUN_CLA = "--dry-run"
const BYPASS_PROTECTED_CLA = "--bypass-protected"
const OVERWRITE_CLA = "--overwrite"
const NOTIFICATION_CLA = "--notify"

// gnu rm CLI arguments
const INTERACTIVE_CLA = "-i"
75 changes: 42 additions & 33 deletions src/patches/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func RmPatch(arguments []string, config models.Config) {
}

filePaths := extractFilePaths(actionedArgs)
extractedArguments := extractArguments(actionedArgs)

// a debug statement is useful for scripts, it provides explicit feedback
// and prints exactly what files were backed up / moved to the trash can
Expand Down Expand Up @@ -76,14 +75,9 @@ func RmPatch(arguments []string, config models.Config) {
overwriteFile(absolutePath)
}

if isTmp || forceHardDelete || isConfigHardDelete && !isConfigSoftDelete && !forceSoftDelete {
hardDelete([]string{path}, extractedArguments)
} else {
// this function will return the default soft delete directory
// if the user has not specified one in their config file
softDeleteDir := config.SoftDeleteDir()
softDelete([]string{path}, extractedArguments, softDeleteDir)
}
shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && !isConfigSoftDelete && !forceSoftDelete

deletePath(absolutePath, shouldHardDelete, config, arguments)
}

if shouldNotify {
Expand Down Expand Up @@ -160,18 +154,6 @@ func extractFilePaths(input []string) []string {
return filePaths
}

func extractArguments(input []string) []string {
arguments := []string{}

for _, str := range input {
if strings.HasPrefix(str, "-") {
arguments = append(arguments, str)
}
}

return arguments
}

func isTmpPath(absolutePath string) bool {
return strings.HasPrefix(absolutePath, "/tmp")
}
Expand All @@ -192,10 +174,34 @@ func backupFileName(path string) string {
return result + ".bak"
}

func deletePath(path string, hard bool, config models.Config, arguments []string) {
isInteractive := util.InArray(arguments, cli.INTERACTIVE_CLA)

if isInteractive {
fmt.Println("Are you sure you want to delete", path, "? (y/n)")
var response string
fmt.Scanln(&response)

if response != "y" && response != "yes" {
fmt.Println("Exiting without removing file(s).")
return
}
}

if hard {
hardDelete(path)
} else {
// this function will return the default soft delete directory
// if the user has not specified one in their config file
softDeletePath := config.SoftDeleteDir()
softDelete(path, softDeletePath)
}
}

// by default, we want to delete files to /tmp/2rm
// however, if the user has specified a different directory in their config file
// we use that instead
func softDelete(filePaths []string, arguments []string, tempDir string) {
func softDelete(filePath string, tempDir string) {
deletedTimestamp := time.Now().Format(time.RFC3339)
backupDirectory := tempDir + deletedTimestamp

Expand All @@ -216,22 +222,25 @@ func softDelete(filePaths []string, arguments []string, tempDir string) {
}
}

commandArguments := strings.Join(arguments, " ")
absoluteSrcPath := relativeToAbsolute(filePath)

for _, path := range filePaths {
absoluteSrcPath := relativeToAbsolute(path)

backupFileName := backupFileName(path)
backupLocation := backupDirectory + "/" + backupFileName
backupFileName := backupFileName(filePath)
backupLocation := backupDirectory + "/" + backupFileName

moveCommand := "mv " + commandArguments + " " + absoluteSrcPath + " " + backupLocation
util.Execute(moveCommand)
err = util.CopyFile(absoluteSrcPath, backupLocation)
if err != nil {
fmt.Println("Error moving file to trash:", err)
return
}

err = os.Remove(absoluteSrcPath)
}

func hardDelete(filePaths []string, arguments []string) {
command := "rm -r " + strings.Join(arguments, " ") + " " + strings.Join(filePaths, " ")
util.Execute(command)
func hardDelete(filePath string) {
err := os.RemoveAll(filePath)
if err != nil {
fmt.Println("Error deleting file:", err)
}
}

func overwriteFile(filePath string) {
Expand Down
23 changes: 23 additions & 0 deletions src/util/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"io"
"os"
)

func CopyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()

_, err = io.Copy(destFile, sourceFile)
return err
}

0 comments on commit 9b817cc

Please sign in to comment.