-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (119 loc) · 2.68 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2024 Shaolong Chen. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/pflag"
)
var (
recursive = pflag.BoolP(
"recursive",
"r",
false,
"remove directories and their contents recursively",
)
force = pflag.BoolP(
"force",
"f",
false,
"ignore nonexistent files and arguments, never prompt",
)
protectedPaths = pflag.StringSliceP(
"protect",
"p",
nil,
"protected `paths`, separated by comma",
)
help = pflag.BoolP("help", "h", false, "display this help and exit")
)
var exitCode = 0
func rm(name string, m map[string]struct{}) error {
path, err := filepath.Abs(name)
if err != nil {
return err
}
_, protected := m[path]
if protected {
log.Printf("skipping %q", name)
return nil
}
stat, err := os.Stat(name)
if err != nil {
if !*force {
log.Printf("cannot remove %q: %v", name, err)
exitCode = 1
}
return nil
}
if stat.IsDir() && !*recursive {
log.Printf("cannot remove %q: is a directory", name)
exitCode = 1
return nil
}
script := fmt.Sprintf(`tell application "Finder"
move POSIX file %q to the trash
end tell
`, path)
cmd := exec.Command("osascript", "-e", script)
cmd.Stdout = nil
var errbuf bytes.Buffer
cmd.Stderr = &errbuf
if err := cmd.Run(); err != nil {
return fmt.Errorf("%w: %s", err, errbuf.String())
}
return nil
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [FILE]...\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Move FILE(s) to the trash.\n\n")
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
By default, trash does not remove directories. Use the --recursive (-r)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
%[1]s -- -foo
%[1]s ./-foo
`, os.Args[0])
}
func checkPrivileges() {
if os.Getuid() == 0 {
fmt.Fprintf(os.Stderr, "This program must not be run as root!!!\n")
os.Exit(1)
}
}
func main() {
checkPrivileges()
log.SetPrefix("trash: ")
log.SetFlags(0)
pflag.Parse()
if *help {
usage()
return
}
if pflag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "rm: missing operand\n")
fmt.Fprintf(os.Stderr, "Try '%s --help' for more information.\n", os.Args[0])
os.Exit(2)
}
m := make(map[string]struct{}, len(*protectedPaths)+1)
for _, path := range *protectedPaths {
m[path] = struct{}{}
}
home, _ := os.UserHomeDir()
if home != "" {
m[home] = struct{}{}
}
for _, name := range pflag.Args() {
if err := rm(name, m); err != nil {
log.Fatal(err)
}
}
os.Exit(exitCode)
}