Skip to content

Commit 9b817cc

Browse files
committed
Remove gnu rm command as a dependency
1 parent fee5c27 commit 9b817cc

File tree

5 files changed

+84
-33
lines changed

5 files changed

+84
-33
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ A wrapper for the "rm" command with soft-deletes, config-based deletion, debug i
1414
- `--bypass-protected` Using this flag will allow you to delete a file protected by the 2rm config
1515
- `--notify` Send a system notification once deletion is complete
1616

17+
## Unsupported command line arguments
18+
19+
- `-r`, `-R`, `--recursive`
20+
- `-d`
21+
- `-v`
22+
- `--version`
23+
- `--help`
24+
- `--interactive[=WHEN]`
25+
- `--one-file-system`
26+
- `-f`, `--force` (partially implemented)
27+
1728
## Features
1829

1930
### Removes the ability to remove your root directory

docs/todo.md

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ You can override this behavior with the `-f` or `--force` flags.
4747
```yml
4848
# ~/.local/share/2rm/config.yml
4949

50+
# extend a config file that is based in the cloud
51+
# this allows you to use the same config across multiple machines
52+
extend: https://www.my-website.com/2rm.yml
53+
5054
# while the backup config option is already supported, we should support
5155
# uploading backups to cloud locations such as s3
5256
backups: s3://my-bucket/backups

src/cli/args.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package cli
22

3+
// 2rm CLI arguments
34
const HARD_DELETE_CLA = "--hard"
45
const SOFT_DELETE_CLA = "--soft"
56
const SILENT_CLA = "--silent"
67
const DRY_RUN_CLA = "--dry-run"
78
const BYPASS_PROTECTED_CLA = "--bypass-protected"
89
const OVERWRITE_CLA = "--overwrite"
910
const NOTIFICATION_CLA = "--notify"
11+
12+
// gnu rm CLI arguments
13+
const INTERACTIVE_CLA = "-i"

src/patches/rm.go

+42-33
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func RmPatch(arguments []string, config models.Config) {
3636
}
3737

3838
filePaths := extractFilePaths(actionedArgs)
39-
extractedArguments := extractArguments(actionedArgs)
4039

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

79-
if isTmp || forceHardDelete || isConfigHardDelete && !isConfigSoftDelete && !forceSoftDelete {
80-
hardDelete([]string{path}, extractedArguments)
81-
} else {
82-
// this function will return the default soft delete directory
83-
// if the user has not specified one in their config file
84-
softDeleteDir := config.SoftDeleteDir()
85-
softDelete([]string{path}, extractedArguments, softDeleteDir)
86-
}
78+
shouldHardDelete := isTmp || forceHardDelete || isConfigHardDelete && !isConfigSoftDelete && !forceSoftDelete
79+
80+
deletePath(absolutePath, shouldHardDelete, config, arguments)
8781
}
8882

8983
if shouldNotify {
@@ -160,18 +154,6 @@ func extractFilePaths(input []string) []string {
160154
return filePaths
161155
}
162156

163-
func extractArguments(input []string) []string {
164-
arguments := []string{}
165-
166-
for _, str := range input {
167-
if strings.HasPrefix(str, "-") {
168-
arguments = append(arguments, str)
169-
}
170-
}
171-
172-
return arguments
173-
}
174-
175157
func isTmpPath(absolutePath string) bool {
176158
return strings.HasPrefix(absolutePath, "/tmp")
177159
}
@@ -192,10 +174,34 @@ func backupFileName(path string) string {
192174
return result + ".bak"
193175
}
194176

177+
func deletePath(path string, hard bool, config models.Config, arguments []string) {
178+
isInteractive := util.InArray(arguments, cli.INTERACTIVE_CLA)
179+
180+
if isInteractive {
181+
fmt.Println("Are you sure you want to delete", path, "? (y/n)")
182+
var response string
183+
fmt.Scanln(&response)
184+
185+
if response != "y" && response != "yes" {
186+
fmt.Println("Exiting without removing file(s).")
187+
return
188+
}
189+
}
190+
191+
if hard {
192+
hardDelete(path)
193+
} else {
194+
// this function will return the default soft delete directory
195+
// if the user has not specified one in their config file
196+
softDeletePath := config.SoftDeleteDir()
197+
softDelete(path, softDeletePath)
198+
}
199+
}
200+
195201
// by default, we want to delete files to /tmp/2rm
196202
// however, if the user has specified a different directory in their config file
197203
// we use that instead
198-
func softDelete(filePaths []string, arguments []string, tempDir string) {
204+
func softDelete(filePath string, tempDir string) {
199205
deletedTimestamp := time.Now().Format(time.RFC3339)
200206
backupDirectory := tempDir + deletedTimestamp
201207

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

219-
commandArguments := strings.Join(arguments, " ")
225+
absoluteSrcPath := relativeToAbsolute(filePath)
220226

221-
for _, path := range filePaths {
222-
absoluteSrcPath := relativeToAbsolute(path)
223-
224-
backupFileName := backupFileName(path)
225-
backupLocation := backupDirectory + "/" + backupFileName
227+
backupFileName := backupFileName(filePath)
228+
backupLocation := backupDirectory + "/" + backupFileName
226229

227-
moveCommand := "mv " + commandArguments + " " + absoluteSrcPath + " " + backupLocation
228-
util.Execute(moveCommand)
230+
err = util.CopyFile(absoluteSrcPath, backupLocation)
231+
if err != nil {
232+
fmt.Println("Error moving file to trash:", err)
233+
return
229234
}
235+
236+
err = os.Remove(absoluteSrcPath)
230237
}
231238

232-
func hardDelete(filePaths []string, arguments []string) {
233-
command := "rm -r " + strings.Join(arguments, " ") + " " + strings.Join(filePaths, " ")
234-
util.Execute(command)
239+
func hardDelete(filePath string) {
240+
err := os.RemoveAll(filePath)
241+
if err != nil {
242+
fmt.Println("Error deleting file:", err)
243+
}
235244
}
236245

237246
func overwriteFile(filePath string) {

src/util/files.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package util
2+
3+
import (
4+
"io"
5+
"os"
6+
)
7+
8+
func CopyFile(src, dst string) error {
9+
sourceFile, err := os.Open(src)
10+
if err != nil {
11+
return err
12+
}
13+
defer sourceFile.Close()
14+
15+
destFile, err := os.Create(dst)
16+
if err != nil {
17+
return err
18+
}
19+
defer destFile.Close()
20+
21+
_, err = io.Copy(destFile, sourceFile)
22+
return err
23+
}

0 commit comments

Comments
 (0)