-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
220 lines (198 loc) · 6.75 KB
/
main.go
File metadata and controls
220 lines (198 loc) · 6.75 KB
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
// This package ("main" package) is not supposed to be imported by another program.
// To use the functionality of ls-having, the "lsh" package
// ("github.com/handy-common-utils/ls-having/lsh" package) can be imported.
// See the documentation of "lsh" package for details.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gobwas/glob"
"github.com/handy-common-utils/ls-having/lsh"
"github.com/josephvusich/go-getopt"
)
func main() {
setupFlags() // this function can't be called more than one time globally
doMain(printToStdout, handleError) // this function is also called in every test case
}
const OPT_ERROR_PANIC = "panic"
const OPT_ERROR_IGNORE = "ignore"
const OPT_ERROR_PRINT = "print"
const DEFAULT_DEPTH = 5
const DEFAULT_CHECK_REGEXP = ".*"
const DEFAULT_ERROR = OPT_ERROR_IGNORE
const DEFAULT_EXIT_CODE_WHEN_ERROR = 1
var optHelp *bool
var optDepth *int
var optFlagFiles arrayFlag
var optMatchAllFlagFiles *bool
var optCheckFile *string
var optCheckRegexp *string
var optCheckInverse *bool
var optExcludes arrayFlag
var optNoDefaultExcludes *bool
var optOnlySubdirectories *bool
var optPrint0 *bool
var optError *string
func setupFlags() {
optHelp = flag.Bool("help", false, "show help information")
optDepth = flag.Int("depth", DEFAULT_DEPTH, "how deep to look into subdirectories, 0 means only look at root directory, -1 means no limit")
flag.Var(&optFlagFiles, "flag-file", "name or `glob` of the flag file, this option can appear multiple times")
optMatchAllFlagFiles = flag.Bool("match-all-flag-files", false, "require all (instead of any) of the flag file names/globs to be matched")
optCheckFile = flag.String("check-file", "", "`name` of the additional file to check")
optCheckRegexp = flag.String("check-regexp", DEFAULT_CHECK_REGEXP, "regular `expression` for testing the content of the check file")
optCheckInverse = flag.Bool("check-inverse", false, "regard regular expression not matching as positive")
flag.Var(&optExcludes, "exclude", "`glob` of the directories to exclude, this option can appear multiple times")
optNoDefaultExcludes = flag.Bool("no-default-excludes", false, "don't apply default excludes")
optOnlySubdirectories = flag.Bool("subdirectories-only", false, "don't return root directory even if it meets conditions")
optPrint0 = flag.Bool("print0", false, "separate paths in the output with null characters (instead of newline characters)")
optError = flag.String("error", DEFAULT_ERROR, "how (`ignore|panic|print`) to handle errors such like non-existing directory, no access permission, etc.")
getopt.Aliases(
"h", "help",
"d", "depth",
"f", "flag-file",
"a", "match-all-flag-files",
"c", "check-file",
"e", "check-regexp",
"i", "check-inverse",
"x", "exclude",
"n", "no-default-excludes",
"s", "subdirectories-only",
"0", "print0",
"r", "error",
)
flag.Usage = func() {
// do nothing, just to avoid getopt to show usage after warning/error info
}
}
func parseFlags() {
// It seems that getopt.Parse() does not reset flags in case it is called more than one time
*optHelp = false
*optDepth = DEFAULT_DEPTH
optFlagFiles = nil
*optMatchAllFlagFiles = false
*optCheckFile = ""
*optCheckRegexp = DEFAULT_CHECK_REGEXP
*optCheckInverse = false
optExcludes = nil
*optNoDefaultExcludes = false
*optOnlySubdirectories = false
*optPrint0 = false
getopt.Parse()
}
func doMain(printOutput func(text string), handleError func(errors []string, printUsage bool, exitCode int)) {
parseFlags()
if *optHelp {
handleError(nil, true, 0)
return
}
var optRootDir = getopt.CommandLine.Arg(0)
if optRootDir == "" {
optRootDir = "."
}
if len(optFlagFiles) == 0 {
if len(*optCheckFile) == 0 {
handleError([]string{"flag file or check file must be specified"}, true, DEFAULT_EXIT_CODE_WHEN_ERROR)
return
} else {
// assuming the check file is also the flag file
optFlagFiles = append(optFlagFiles, *optCheckFile)
}
}
if !*optNoDefaultExcludes {
optExcludes = append(optExcludes,
".git",
filepath.Join("**", ".git"),
"node_modules",
filepath.Join("**", "node_modules"),
"testdata",
filepath.Join("**", "testdata"),
)
}
var options = lsh.Options{
Depth: *optDepth,
Excludes: compileGlobs(optExcludes, filepath.Separator),
ExcludeRoot: *optOnlySubdirectories,
FlagFiles: compileGlobs(optFlagFiles, filepath.Separator),
MatchAllFlagFiles: *optMatchAllFlagFiles,
CheckFile: *optCheckFile,
CheckRegexp: regexp.MustCompile(*optCheckRegexp),
CheckInverse: *optCheckInverse,
PanicOnError: *optError == OPT_ERROR_PANIC,
}
var dirs, errors = lsh.LsHaving(&options, optRootDir)
if errors != nil {
switch *optError {
case OPT_ERROR_PANIC:
handleError(errors, false, DEFAULT_EXIT_CODE_WHEN_ERROR)
return
case OPT_ERROR_PRINT:
handleError(errors, false, 0)
// continue to print out results
default:
// do nothing
// continue to print out results
}
}
if len(dirs) > 0 {
separator := "\n"
if *optPrint0 {
separator = string([]byte{0})
}
printOutput(strings.Join(dirs, separator))
printOutput(separator)
}
}
func compileGlobs(globStrings []string, separator rune) []glob.Glob {
result := make([]glob.Glob, len(globStrings))
for i, globString := range globStrings {
result[i] = glob.MustCompile(globString, separator)
}
return result
}
type arrayFlag []string
func (i *arrayFlag) String() string {
return strings.Join(*i, ",")
}
func (i *arrayFlag) Set(value string) error {
*i = append(*i, value)
return nil
}
func printToStdout(text string) {
fmt.Print(text)
}
// Handle error situation.
// Depending on the parameters passed in, this function could
// - print out error messages to stderr
// - print out usage/help info to stdout
// - exit the program with a non-zero exit code
//
// Parameters:
// - errors: nil or an array of error messages which would be printed to stderr
// - printUsage: true if usage/help info should be printed to stdout
// - exitCode: non-zero exit code if os.Exit should be called, or zero if this function should return normally
func handleError(errors []string, printUsage bool, exitCode int) {
var writer = os.Stdout
if len(errors) > 0 {
writer = os.Stderr
for _, errorString := range errors {
fmt.Fprintln(writer, "Error: "+errorString)
}
}
if printUsage {
fmt.Println("Usage: ls-having -f name-or-glob [options] [root-dir]")
fmt.Println("Options:")
getopt.CommandLine.SetOutput(os.Stdout)
getopt.PrintDefaults()
fmt.Println("References:")
fmt.Println(" Glob syntax: https://github.com/gobwas/glob#example")
fmt.Println(" Regexp syntax: https://pkg.go.dev/regexp/syntax")
fmt.Println(" Home page: https://github.com/handy-common-utils/ls-having")
}
if exitCode != 0 {
os.Exit(exitCode)
}
}