Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Aug 4, 2023
1 parent 4634d72 commit a0efc55
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SAFE DELETE

安全的删除文件/文件夹

## Example
Expand All @@ -12,4 +13,14 @@ sudo chmod +x /usr/local/bin/rmf

# ~/.zshrc
alias rm="echo 'use rmf'; rmf"
```
```

## [Trash](https://command-not-found.com/trash-empty)

```bash
apt/yum/brew/dnf install trash-cli

trash-empty
```

43 changes: 35 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package main

import (
"fmt"
"github.com/Bios-Marcel/wastebasket"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

var (
recursive bool
force bool
interactive bool
verbose bool
)

var versionData string

var rootCmd = &cobra.Command{
Expand All @@ -15,32 +23,51 @@ var rootCmd = &cobra.Command{
Version: versionData,

Run: func(cmd *cobra.Command, args []string) {
log.Println(args)
for _, v := range args {
if _, err := os.Stat(v); err != nil {
if os.IsNotExist(err) {
log.Errorf("文件不存在: %s", v)
if !force {
log.Errorf("文件不存在: %s", v)
}
continue
} else {
if !force {
log.Errorf("文件不存在: %s", v)
}
}
log.Infof("文件不存在? : %s", v)
continue
}

if interactive {
log.Infof("确认删除文件: %s", v)
var input string
if _, err := fmt.Scanln(&input); err != nil {
log.Errorf("Error: %v", err)
return
}
if input != "y" && input != "Y" {
log.Infof("取消删除文件: %s", v)
continue
}
}
if err := wastebasket.Trash(v); err != nil {
log.Errorf("Error: %v", err)
return
}
log.Infof("Success: %v", v)
if verbose {
log.Infof("Success: %s", v)
}
}
},
Example: "rm -rfi [file1] [file2] [file3]",
}

func main() {
var temp string
rootCmd.Flags().StringVarP(&temp, "recursive", "r", "recursive", "which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in).")
rootCmd.Flags().StringVarP(&temp, "force", "f", "force", "which ignores non-existent files and overrides any confirmation prompts (effectively canceling -i), although it will not remove files from a directory if the directory is write-protected.")
rootCmd.Flags().StringVarP(&temp, "interactive", "i", "interactive", "which asks for every deletion to be confirmed.")
rootCmd.Flags().StringVarP(&temp, "directory", "d", "directory", "which deletes an empty directory, and only works if the specified directory is empty.")
rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in).")
rootCmd.Flags().BoolVarP(&force, "force", "f", false, "which ignores non-existent files and overrides any confirmation prompts (effectively canceling -i), although it will not remove files from a directory if the directory is write-protected.")
rootCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "which asks for every deletion to be confirmed.")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "which explains what is being done.")
rootCmd.Flags().StringVarP(&temp, "one-file-system", "", "one-file-system", "only removes files on the same file system as the argument, and will ignore mounted file systems.")
if err := rootCmd.Execute(); err != nil {
log.Errorf("Error: %v", err)
Expand Down

0 comments on commit a0efc55

Please sign in to comment.