This repository was archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (68 loc) · 2.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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{
Short: "Safe Alternative System rm",
Long: "Safe Alternative System rm",
Version: versionData,
Run: func(cmd *cobra.Command, args []string) {
for _, v := range args {
if _, err := os.Stat(v); err != nil {
if os.IsNotExist(err) {
if !force {
log.Errorf("文件不存在: %s", v)
}
continue
} else {
if !force {
log.Errorf("文件不存在: %s", v)
}
}
continue
}
if interactive {
log.Infof("确认删除文件: %s", v)
var input string
if _, err := fmt.Scanln(&input); err != nil {
log.Errorf("获取输入失败: %v", err)
return
}
if input != "y" && input != "Y" {
log.Infof("取消删除文件: %s", v)
continue
}
}
if err := wastebasket.Trash(v); err != nil {
log.Errorf("删除文件失败: %v err:%v", v, err)
return
}
if verbose {
log.Infof("Success: %s", v)
}
}
},
Example: "rm -rfi [file1] [file2] [file3]",
}
func main() {
var temp string
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)
}
}