/
find-main.go
286 lines (245 loc) · 8.94 KB
/
find-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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* MinIO Client (C) 2017-2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"context"
"strings"
humanize "github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio/pkg/console"
)
// List of all flags supported by find command.
var (
findFlags = []cli.Flag{
cli.StringFlag{
Name: "exec",
Usage: "spawn an external process for each matching object (see FORMAT)",
},
cli.StringFlag{
Name: "ignore",
Usage: "exclude objects matching the wildcard pattern",
},
cli.StringFlag{
Name: "name",
Usage: "find object names matching wildcard pattern",
},
cli.StringFlag{
Name: "newer-than",
Usage: "match all objects newer than L days, M hours and N minutes",
},
cli.StringFlag{
Name: "older-than",
Usage: "match all objects older than L days, M hours and N minutes",
},
cli.StringFlag{
Name: "path",
Usage: "match directory names matching wildcard pattern",
},
cli.StringFlag{
Name: "print",
Usage: "print in custom format to STDOUT (see FORMAT)",
},
cli.StringFlag{
Name: "regex",
Usage: "match directory and object name with PCRE regex pattern",
},
cli.StringFlag{
Name: "larger",
Usage: "match all objects larger than specified size in units (see UNITS)",
},
cli.StringFlag{
Name: "smaller",
Usage: "match all objects smaller than specified size in units (see UNITS)",
},
cli.UintFlag{
Name: "maxdepth",
Usage: "limit directory navigation to specified depth",
},
cli.BoolFlag{
Name: "watch",
Usage: "monitor a specified path for newly created object(s)",
},
}
)
var findCmd = cli.Command{
Name: "find",
Usage: "search for objects",
Action: mainFind,
Before: setGlobalsFromContext,
Flags: append(findFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} PATH [FLAGS]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
UNITS
--smaller, --larger flags accept human-readable case-insensitive number
suffixes such as "k", "m", "g" and "t" referring to the metric units KB,
MB, GB and TB respectively. Adding an "i" to these prefixes, uses the IEC
units, so that "gi" refers to "gibibyte" or "GiB". A "b" at the end is
also accepted. Without suffixes the unit is bytes.
--older-than, --newer-than flags accept the string for days, hours and minutes
i.e. 1d2h30m states 1 day, 2 hours and 30 minutes.
FORMAT
Support string substitutions with special interpretations for following keywords.
Keywords supported if target is filesystem or object storage:
{} --> Substitutes to full path.
{base} --> Substitutes to basename of path.
{dir} --> Substitutes to dirname of the path.
{size} --> Substitutes to object size of the path.
{time} --> Substitutes to object modified time of the path.
Keywords supported if target is object storage:
{url} --> Substitutes to a shareable URL of the path.
EXAMPLES:
01. Find all "foo.jpg" in all buckets under "s3" account.
{{.Prompt}} {{.HelpName}} s3 --name "foo.jpg"
02. Find all objects with ".txt" extension under "s3/mybucket".
{{.Prompt}} {{.HelpName}} s3/mybucket --name "*.txt"
03. Find only the object names without the directory component under "s3/mybucket".
{{.Prompt}} {{.HelpName}} s3/mybucket --name "*" -print {base}
04. Find all images with ".jpg" extension under "s3/photos", prefixed with "album".
{{.Prompt}} {{.HelpName}} s3/photos --name "*.jpg" --path "*/album*/*"
05. Find all images with ".jpg", ".png", and ".gif" extensions, using regex under "s3/photos".
{{.Prompt}} {{.HelpName}} s3/photos --regex "(?i)\.(jpg|png|gif)$"
06. Find all images with ".jpg" extension under "s3/bucket" and copy to "play/bucket" *continuously*.
{{.Prompt}} {{.HelpName}} s3/bucket --name "*.jpg" --watch --exec "mc cp {} play/bucket"
07. Find and generate public URLs valid for 7 days, for all objects between 64 MB, and 1 GB in size under "s3" account.
{{.Prompt}} {{.HelpName}} s3 --larger 64MB --smaller 1GB --print {url}
08. Find all objects created in the last week under "s3/bucket".
{{.Prompt}} {{.HelpName}} s3/bucket --newer-than 7d
09. Find all objects which were created are older than 2 days, 5 hours and 10 minutes and exclude the ones with ".jpg"
extension under "s3".
{{.Prompt}} {{.HelpName}} s3 --older-than 2d5h10m --ignore "*.jpg"
10. List all objects up to 3 levels sub-directory deep under "s3/bucket".
{{.Prompt}} {{.HelpName}} s3/bucket --maxdepth 3
`,
}
// checkFindSyntax - validate the passed arguments
func checkFindSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string][]prefixSSEPair) {
args := cliCtx.Args()
if !args.Present() {
args = []string{"./"} // No args just default to present directory.
} else if args.Get(0) == "." {
args[0] = "./" // If the arg is '.' treat it as './'.
}
for _, arg := range args {
if strings.TrimSpace(arg) == "" {
fatalIf(errInvalidArgument().Trace(args...), "Unable to validate empty argument.")
}
}
// Extract input URLs and validate.
for _, url := range args {
_, _, err := url2Stat(ctx, url, false, encKeyDB)
if err != nil && !isURLPrefixExists(url, false) {
// Bucket name empty is a valid error for 'find myminio' unless we are using watch, treat it as such.
if _, ok := err.ToGoError().(BucketNameEmpty); ok && !cliCtx.Bool("watch") {
continue
}
fatalIf(err.Trace(url), "Unable to stat `"+url+"`.")
}
}
}
// Find context is container to hold all parsed input arguments,
// each parsed input is stored in its native typed form for
// ease of repurposing.
type findContext struct {
*cli.Context
execCmd string
ignorePattern string
namePattern string
pathPattern string
regexPattern string
maxDepth uint
printFmt string
olderThan string
newerThan string
largerSize uint64
smallerSize uint64
watch bool
// Internal values
targetAlias string
targetURL string
targetFullURL string
clnt Client
}
// mainFind - handler for mc find commands
func mainFind(cliCtx *cli.Context) error {
ctx, cancelFind := context.WithCancel(globalContext)
defer cancelFind()
// Additional command specific theme customization.
console.SetColor("Find", color.New(color.FgGreen, color.Bold))
console.SetColor("FindExecErr", color.New(color.FgRed, color.Italic, color.Bold))
// Parse encryption keys per command.
encKeyDB, err := getEncKeys(cliCtx)
fatalIf(err, "Unable to parse encryption keys.")
checkFindSyntax(ctx, cliCtx, encKeyDB)
args := cliCtx.Args()
if !args.Present() {
args = []string{"./"} // Not args present default to present directory.
} else if args.Get(0) == "." {
args[0] = "./" // If the arg is '.' treat it as './'.
}
clnt, err := newClient(args[0])
fatalIf(err.Trace(args...), "Unable to initialize `"+args[0]+"`.")
var olderThan, newerThan string
if cliCtx.String("older-than") != "" {
olderThan = cliCtx.String("older-than")
}
if cliCtx.String("newer-than") != "" {
newerThan = cliCtx.String("newer-than")
}
// Use 'e' to indicate Go error, this is a convention followed in `mc`. For probe.Error we call it
// 'err' and regular Go error is called as 'e'.
var e error
var largerSize, smallerSize uint64
if cliCtx.String("larger") != "" {
largerSize, e = humanize.ParseBytes(cliCtx.String("larger"))
fatalIf(probe.NewError(e).Trace(cliCtx.String("larger")), "Unable to parse input bytes.")
}
if cliCtx.String("smaller") != "" {
smallerSize, e = humanize.ParseBytes(cliCtx.String("smaller"))
fatalIf(probe.NewError(e).Trace(cliCtx.String("smaller")), "Unable to parse input bytes.")
}
targetAlias, _, hostCfg, err := expandAlias(args[0])
fatalIf(err.Trace(args[0]), "Unable to expand alias.")
var targetFullURL string
if hostCfg != nil {
targetFullURL = hostCfg.URL
}
return doFind(ctx, &findContext{
Context: cliCtx,
maxDepth: cliCtx.Uint("maxdepth"),
execCmd: cliCtx.String("exec"),
printFmt: cliCtx.String("print"),
namePattern: cliCtx.String("name"),
pathPattern: cliCtx.String("path"),
regexPattern: cliCtx.String("regex"),
ignorePattern: cliCtx.String("ignore"),
olderThan: olderThan,
newerThan: newerThan,
largerSize: largerSize,
smallerSize: smallerSize,
watch: cliCtx.Bool("watch"),
targetAlias: targetAlias,
targetURL: args[0],
targetFullURL: targetFullURL,
clnt: clnt,
})
}