/
rmext.go
executable file
·220 lines (161 loc) · 3.81 KB
/
rmext.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"flag"
"fmt"
"log"
"mime"
"os"
"path/filepath"
"strings"
"github.com/GreenRaccoon23/slices"
)
var (
PrintBase bool
PrintFull bool
Paths []string
)
func init() {
if helpNeeded() {
help(1)
} else if helpWanted() {
help(0)
}
flags()
if !validArgs() {
help(1)
}
if err := modPaths(); err != nil {
log.Fatal(err)
}
}
func main() {
defer os.Exit(0)
for _, fpath := range Paths {
name, _ := splitExt(fpath)
fmt.Println(name)
}
}
func helpNeeded() bool {
// fmt.Println("helpNeeded()")
if noArgs := (len(os.Args) == 1); noArgs {
return true
}
return false
}
func helpWanted() bool {
// fmt.Println("helpWanted()")
switch os.Args[1] {
case "-h", "h", "help", "--help", "-H", "H", "HELP", "--HELP", "-help", "--h", "--H":
return true
}
return false
}
func help(status int) {
// fmt.Println("help()")
// fmt.Printf(" status: %v\n", status)
defer os.Exit(status)
fmt.Printf(
"%s\n\n %s\n\n %s\n%s\n\n %s\n%s\n%s\n%s\n%s\n\n %s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
"rmext",
"Usage: rmext file...",
"Description:",
" Print the basename of files WITHOUT their extension.",
"Options:",
" -b Print the basename of files without their path",
" -f Print the full path of files",
" (default) Print the path of files exactly as they were entered",
" (minus any file extensions)",
"Notes:",
" This program relies on the system's list of known mimetypes.",
" On Linux, usually the file '/etc/mime.types' stores all",
" MIME types known by the system.",
" On Windows, the registry mess stores them.",
" In rare cases, the system may not have a mime entry for an",
" uncommon file type, and this program will not remove the",
" extension associated with that file type.",
" An example is '.go' files; Linux distributions usually do not",
" include a mime type for '.go' files by default (yet).",
)
}
func flags() {
// fmt.Println("flags()")
bools := []string{"-b", "-f"}
flag.BoolVar(&PrintBase, "b", false, "")
flag.BoolVar(&PrintFull, "f", false, "")
flag.Parse()
Paths = os.Args[1:]
Paths = slices.Filter(Paths, bools...)
}
func validArgs() bool {
// fmt.Println("validArgs()")
if PrintBase && PrintFull {
return false
}
if len(Paths) == 0 {
return false
}
return true
}
func modPaths() error {
// fmt.Println("modPaths()")
if PrintBase {
Paths = basepaths(Paths)
return nil
} else if PrintFull {
var err error
Paths, err = abspaths(Paths)
return err
} else {
return nil
}
}
func basepaths(paths []string) []string {
// fmt.Println("basepaths()")
// fmt.Printf(" paths: %v\n", paths)
var based []string
for _, fpath := range paths {
basename := filepath.Base(fpath)
based = append(based, basename)
}
return based
}
func abspaths(paths []string) ([]string, error) {
// fmt.Println("abspaths()")
// fmt.Printf(" paths: %v\n", paths)
var absed []string
for _, fpath := range paths {
abs, err := filepath.Abs(fpath)
if err != nil {
return nil, err
}
absed = append(absed, abs)
}
return absed, nil
}
func splitExt(filename string) (name, ext string) {
// fmt.Println("splitExt()")
// fmt.Printf(" filename: %v\n", filename)
name = filepath.Clean(filename)
for {
extTest := filepath.Ext(name)
mimeType := mime.TypeByExtension(extTest)
// base case
if noMoreExts := (extTest == "" || mimeType == ""); noMoreExts {
return
}
// recursive case
ext = concat(extTest, ext) // ".tar" + ".xz"
name = strings.TrimSuffix(name, extTest) // "file.tar" > "file"
}
}
func concat(slc ...string) string {
// fmt.Println("concat()")
// fmt.Printf(" slc: %v\n", slc)
b := bytes.NewBuffer(nil)
defer b.Reset()
for _, s := range slc {
b.WriteString(s)
}
return b.String()
}