-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
249 lines (236 loc) · 7.98 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// © Ben Garrett https://github.com/bengarrett/zipcmt
// Zipcmt is the super-fast, batch, zip file comment viewer, and extractor.
package main
import (
_ "embed"
"flag"
"fmt"
"io"
"os"
"runtime"
"strings"
"text/tabwriter"
"github.com/bengarrett/zipcmt/internal/misc"
zipcmt "github.com/bengarrett/zipcmt/pkg"
"github.com/gookit/color"
)
var (
//go:embed embed/logo.txt
brand string
version = "0.0.0"
commit = "unset"
date = "unset"
)
const winOS = "windows"
func main() {
const ellipsis = "\u2026"
var c zipcmt.Config
var noprint bool
c.SetTimer()
flag.BoolVar(&noprint, "noprint", false,
"do not print comments to the terminal to improve the performance of the scan")
flag.BoolVar(&c.NoWalk, "norecursive", false,
"do not recursively walk through any subdirectories while scanning for zip archives")
flag.BoolVar(&c.Export, "export", false,
fmt.Sprintf("save the comments as text files stored alongside the zip files (%s)",
color.Danger.Sprint("use at your own risk")))
flag.BoolVar(&c.Dupes, "all", false,
"show all comments, including duplicates in multiple zips")
flag.BoolVar(&c.Now, "now", false,
"do not use the last modification date sourced from the zip files")
flag.BoolVar(&c.Log, "log", false,
"create a logfile for debugging")
flag.BoolVar(&c.Overwrite, "overwrite", false,
"overwrite any previously exported comment text files")
flag.BoolVar(&c.Quiet, "quiet", false,
"suppress zipcmt feedback except for errors")
flag.BoolVar(&c.Raw, "raw", false,
"use the original comment text encoding (CP437, ISO-8859"+ellipsis+") instead of Unicode")
flag.StringVar(&c.SaveName, "save", "",
"save the comments to uniquely named textfiles in this directory")
ver := flag.Bool("version", false,
"version and information for this program")
aliasA := flag.Bool("a", false, "alias for all")
aliasO := flag.Bool("o", false, "alias for overwrite")
aliasQ := flag.Bool("q", false, "alias for quiet")
aliasR := flag.Bool("r", false, "alias for norecursive")
aliasS := flag.String("s", "", "alias for save")
aliasU := flag.Bool("p", false, "alias for noprint")
aliasV := flag.Bool("v", false, "alias for version")
flag.Usage = func() {
help(os.Stderr, true)
}
flag.Parse()
flags(ver, aliasV, aliasQ)
// parse aliases
if *aliasR {
c.NoWalk = true
}
if *aliasU || noprint {
c.Print = false
} else {
c.Print = true
}
if *aliasS != "" {
c.SaveName = *aliasS
}
if *aliasO {
c.Overwrite = true
}
if *aliasQ {
c.Quiet = true
}
if *aliasA {
c.Dupes = true
}
// directories to scan
c.Dirs = flag.Args()
// file and directory scan
c.WalkDirs()
// summaries
fmt.Fprintln(os.Stdout, c.Status())
if s := c.LogName(); s != "" {
fmt.Fprintf(os.Stdout, "%s %s\n", "The log is found at", color.Primary.Sprint(s))
}
}
func flags(ver, aliasV, quiet *bool) {
// convenience for when a help or version flag is passed as an argument
for _, arg := range flag.Args() {
showLogo := !*quiet
switch strings.ToLower(arg) {
case "-h", "-help", "--help":
help(os.Stderr, showLogo)
os.Exit(0)
case "-v", "-version", "--version":
info(os.Stdout, quiet)
os.Exit(0)
}
}
// print version information
if *ver || *aliasV {
info(os.Stdout, quiet)
os.Exit(0)
}
// print help if no arguments are given
w := os.Stderr
if len(flag.Args()) == 0 {
s := "zipcmt requires at least one directory to scan"
if runtime.GOOS == winOS {
s = "zipcmt requires at least one directory or drive letter to scan"
}
fmt.Fprintln(w, color.Warn.Sprint(s)+"\n")
help(w, false)
os.Exit(0)
}
}
func helpPosix(w io.Writer) {
const ps = string(os.PathSeparator)
fmt.Fprintln(w, " zipcmt [options] <directories>")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Examples:")
fmt.Fprint(w, color.Info.Sprint(" zipcmt .\t\t\t\t"))
fmt.Fprintln(w,
color.Note.Sprint("# scan the current directory and subdirectories for unique comments"))
fmt.Fprint(w, color.Info.Sprintf(" zipcmt -save=~%swork ~%sDownloads\t", ps, ps))
fmt.Fprintln(w,
color.Note.Sprint("# scan the user downloads directory, then save unique comments to a directory"))
fmt.Fprint(w, color.Info.Sprintf(" zipcmt -a -s=~%swork ~%sDownloads\t", ps, ps))
fmt.Fprintln(w,
color.Note.Sprint("# scan the user downloads directory, then save all comments to a directory"))
fmt.Fprint(w, color.Info.Sprintf(" zipcmt -quiet %s | less\t\t", ps))
fmt.Fprintln(w,
color.Note.Sprint("# scan the whole system to view the unique comments in a page reader"))
}
func helpWin(w io.Writer) {
const ps = string(os.PathSeparator)
fmt.Fprintln(w, " zipcmt [options] <directories or drive letters>")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Examples:")
fmt.Fprint(w, color.Info.Sprint(" zipcmt .\t\t\t"))
fmt.Fprintln(w,
color.Note.Sprint("# scan the current directory and subdirectories for unique comments"))
if hd, err := os.UserHomeDir(); err == nil {
fmt.Fprintln(w, color.Info.Sprintf(" zipcmt -save=C:\\work %s%sDownloads\t\t", hd, ps))
fmt.Fprintln(w, color.Note.Sprint("\t\t\t\t# scan the files and directories in Downloads"+
" and save the unique comments to 'C:\\work'"))
}
fmt.Fprint(w, color.Info.Sprint(" zipcmt -save=C:\\work C:\t"))
fmt.Fprintln(w,
color.Note.Sprint("# scan the 'C' drive and save the unique comments to the 'C:\\work' directory"))
fmt.Fprint(w, color.Info.Sprint(" zipcmt -quiet C: D: | more\t"))
fmt.Fprintln(w,
color.Note.Sprint("# scan the 'C' and 'D' drives to view the unique comments in a page reader"))
}
// Help, usage and examples.
func help(w io.Writer, logo bool) {
var f *flag.Flag
if logo {
fmt.Fprintln(w, brand)
fmt.Fprint(w, " Zip Comment is the super-fast, batch zip file-comment viewer and extractor.\n"+
" Using a modern PC, zipcmt handles many thousands of archives per second.\n\n")
}
fmt.Fprintln(w, "Usage:")
if runtime.GOOS == winOS {
helpWin(w)
} else {
helpPosix(w)
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Options:")
const padding = 4
tw := tabwriter.NewWriter(w, 0, 0, padding, ' ', 0)
f = flag.Lookup("save")
fmt.Fprintf(tw, " -%v, -%v=DIRECTORY\t%v\n", "s", f.Name, f.Usage)
f = flag.Lookup("overwrite")
fmt.Fprintf(tw, " -%v, -%v\t%v\n", f.Name[:1], f.Name, f.Usage)
f = flag.Lookup("noprint")
fmt.Fprintf(tw, " -p, -%v\t%v\n", f.Name, f.Usage)
fmt.Fprintln(tw, " \t")
f = flag.Lookup("norecursive")
fmt.Fprintf(tw, " -%v, -%v\t%v\n", "r", f.Name, f.Usage)
f = flag.Lookup("all")
fmt.Fprintf(tw, " -%v, -%v\t%v\n", f.Name[:1], f.Name, f.Usage)
f = flag.Lookup("now")
fmt.Fprintf(tw, " -%v\t%v\n", f.Name, f.Usage)
f = flag.Lookup("raw")
fmt.Fprintf(tw, " -%v\t%v\n", f.Name, f.Usage)
fmt.Fprintln(tw, " \t")
f = flag.Lookup("export")
fmt.Fprintf(tw, " -%v\t%v\n", f.Name, f.Usage)
fmt.Fprintln(tw, " \t")
f = flag.Lookup("quiet")
fmt.Fprintf(tw, " -%v, -%v\t%v\n", f.Name[:1], f.Name, f.Usage)
f = flag.Lookup("version")
fmt.Fprintf(tw, " -%v, -%v\t%v\n", f.Name[:1], f.Name, f.Usage)
fmt.Fprintln(tw, " -h, -help\tshow this list of options")
fmt.Fprintln(tw)
optimial(tw)
tw.Flush()
}
func optimial(tw *tabwriter.Writer) {
if runtime.GOOS != winOS {
return
}
fmt.Fprintln(tw, "For optimal performance Windows users may wish to temporarily disable"+
" the Virus & threat 'Real-time protection' under Windows Security.")
fmt.Fprintln(tw, "Or create Windows Security Exclusions for the directories to be scanned.")
fmt.Fprintln(tw, "https://support.microsoft.com/en-us/windows/"+
"add-an-exclusion-to-windows-security-811816c0-4dfd-af4a-47e4-c301afe13b26")
}
// Info prints out the program information and version.
func info(w io.Writer, quiet *bool) {
const copyright = "\u00A9"
if !*quiet {
fmt.Fprintln(w, brand)
}
fmt.Fprintf(w, "zipcmt v%s\n%s 2021-24 Ben Garrett, logo by sensenstahl\n",
version, copyright)
fmt.Fprintf(w, "https://github.com/bengarrett/zipcmt\n\n")
fmt.Fprintf(w, "build: %s (%s)\n", commit, date)
exe, err := misc.Self()
if err != nil {
fmt.Fprintf(w, "path: %s\n", err)
return
}
fmt.Fprintf(w, "path: %s\n", exe)
}