find: Add support of --versions flag (#4394)

This commit is contained in:
Anis Elleuch
2022-12-09 02:34:30 -08:00
committed by GitHub
parent 84cbb788e2
commit 6b0f191643
2 changed files with 82 additions and 73 deletions
+47 -34
View File
@@ -40,6 +40,10 @@ var (
Name: "ignore",
Usage: "exclude objects matching the wildcard pattern",
},
cli.BoolFlag{
Name: "versions",
Usage: "include all objects versions",
},
cli.StringFlag{
Name: "name",
Usage: "find object names matching wildcard pattern",
@@ -113,11 +117,12 @@ 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.
{} --> 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.
{version} --> Substitutes to object version identifier.
Keywords supported if target is object storage:
@@ -154,6 +159,9 @@ EXAMPLES:
10. List all objects up to 3 levels sub-directory deep under "s3/bucket".
{{.Prompt}} {{.HelpName}} s3/bucket --maxdepth 3
11. Copy all versions of all objects in bucket in the local machine
{{.Prompt}} {{.HelpName}} s3/bucket --versions --exec "mc cp --version-id {version} {} /tmp/dir/{}.{version}"
`,
}
@@ -190,18 +198,19 @@ func checkFindSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[stri
// 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
execCmd string
ignorePattern string
namePattern string
pathPattern string
regexPattern string
maxDepth uint
printFmt string
olderThan string
newerThan string
largerSize uint64
smallerSize uint64
watch bool
withOlderVersions bool
// Internal values
targetAlias string
@@ -259,6 +268,9 @@ func mainFind(cliCtx *cli.Context) error {
fatalIf(probe.NewError(e).Trace(cliCtx.String("smaller")), "Unable to parse input bytes.")
}
// Get --versions flag
withVersions := cliCtx.Bool("versions")
targetAlias, _, hostCfg, err := expandAlias(args[0])
fatalIf(err.Trace(args[0]), "Unable to expand alias.")
@@ -268,22 +280,23 @@ func mainFind(cliCtx *cli.Context) error {
}
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,
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"),
withOlderVersions: withVersions,
olderThan: olderThan,
newerThan: newerThan,
largerSize: largerSize,
smallerSize: smallerSize,
watch: cliCtx.Bool("watch"),
targetAlias: targetAlias,
targetURL: args[0],
targetFullURL: targetFullURL,
clnt: clnt,
})
}
+35 -39
View File
@@ -45,7 +45,12 @@ type findMessage struct {
// String calls tells the console what to print and how to print it.
func (f findMessage) String() string {
return console.Colorize("Find", f.contentMessage.Key)
var msg string
msg += f.contentMessage.Key
if f.VersionID != "" {
msg += " (" + f.contentMessage.VersionID + ")"
}
return console.Colorize("Find", msg)
}
// JSON formats output to be JSON output.
@@ -270,10 +275,15 @@ func doFind(ctxCtx context.Context, ctx *findContext) error {
// following defer is a no-op.
defer watchFind(ctxCtx, ctx)
var prevKeyName string
lstOptions := ListOptions{
WithOlderVersions: ctx.withOlderVersions,
WithDeleteMarkers: false,
Recursive: true,
ShowDir: DirFirst,
}
// iterate over all content which is within the given directory
for content := range ctx.clnt.List(globalContext, ListOptions{Recursive: true, ShowDir: DirFirst}) {
for content := range ctx.clnt.List(globalContext, lstOptions) {
if content.Err != nil {
switch content.Err.ToGoError().(type) {
// handle this specifically for filesystem related errors.
@@ -299,18 +309,17 @@ func doFind(ctxCtx context.Context, ctx *findContext) error {
fileKeyName := getAliasedPath(ctx, content.URL.String())
fileContent := contentMessage{
Key: fileKeyName,
Time: content.Time.Local(),
Size: content.Size,
Key: fileKeyName,
VersionID: content.VersionID,
Time: content.Time.Local(),
Size: content.Size,
}
// Match the incoming content, didn't match return.
if !matchFind(ctx, fileContent) || prevKeyName == fileKeyName {
if !matchFind(ctx, fileContent) {
continue
} // For all matching content
prevKeyName = fileKeyName
// proceed to either exec, format the output string.
if ctx.execCmd != "" {
execFind(ctxCtx, ctx.execCmd, fileContent)
@@ -333,54 +342,35 @@ func doFind(ctxCtx context.Context, ctx *findContext) error {
func stringsReplace(ctx context.Context, args string, fileContent contentMessage) string {
// replace all instances of {}
str := args
if strings.Contains(str, "{}") {
str = strings.ReplaceAll(str, "{}", fileContent.Key)
}
str = strings.ReplaceAll(str, "{}", fileContent.Key)
// replace all instances of {""}
if strings.Contains(str, `{""}`) {
str = strings.ReplaceAll(str, `{""}`, strconv.Quote(fileContent.Key))
}
str = strings.ReplaceAll(str, `{""}`, strconv.Quote(fileContent.Key))
// replace all instances of {base}
if strings.Contains(str, "{base}") {
str = strings.ReplaceAll(str, "{base}", filepath.Base(fileContent.Key))
}
str = strings.ReplaceAll(str, "{base}", filepath.Base(fileContent.Key))
// replace all instances of {"base"}
if strings.Contains(str, `{"base"}`) {
str = strings.ReplaceAll(str, `{"base"}`, strconv.Quote(filepath.Base(fileContent.Key)))
}
str = strings.ReplaceAll(str, `{"base"}`, strconv.Quote(filepath.Base(fileContent.Key)))
// replace all instances of {dir}
if strings.Contains(str, "{dir}") {
str = strings.ReplaceAll(str, "{dir}", filepath.Dir(fileContent.Key))
}
str = strings.ReplaceAll(str, "{dir}", filepath.Dir(fileContent.Key))
// replace all instances of {"dir"}
if strings.Contains(str, `{"dir"}`) {
str = strings.ReplaceAll(str, `{"dir"}`, strconv.Quote(filepath.Dir(fileContent.Key)))
}
str = strings.ReplaceAll(str, `{"dir"}`, strconv.Quote(filepath.Dir(fileContent.Key)))
// replace all instances of {size}
if strings.Contains(str, "{size}") {
str = strings.ReplaceAll(str, "{size}", humanize.IBytes(uint64(fileContent.Size)))
}
str = strings.ReplaceAll(str, "{size}", humanize.IBytes(uint64(fileContent.Size)))
// replace all instances of {"size"}
if strings.Contains(str, `{"size"}`) {
str = strings.ReplaceAll(str, `{"size"}`, strconv.Quote(humanize.IBytes(uint64(fileContent.Size))))
}
str = strings.ReplaceAll(str, `{"size"}`, strconv.Quote(humanize.IBytes(uint64(fileContent.Size))))
// replace all instances of {time}
if strings.Contains(str, "{time}") {
str = strings.ReplaceAll(str, "{time}", fileContent.Time.Format(printDate))
}
str = strings.ReplaceAll(str, "{time}", fileContent.Time.Format(printDate))
// replace all instances of {"time"}
if strings.Contains(str, `{"time"}`) {
str = strings.ReplaceAll(str, `{"time"}`, strconv.Quote(fileContent.Time.Format(printDate)))
}
str = strings.ReplaceAll(str, `{"time"}`, strconv.Quote(fileContent.Time.Format(printDate)))
// replace all instances of {url}
if strings.Contains(str, "{url}") {
@@ -392,6 +382,12 @@ func stringsReplace(ctx context.Context, args string, fileContent contentMessage
str = strings.ReplaceAll(str, `{"url"}`, strconv.Quote(getShareURL(ctx, fileContent.Key)))
}
// replace all instances of {version}
str = strings.ReplaceAll(str, `{version}`, fileContent.VersionID)
// replace all instances of {"version"}
str = strings.ReplaceAll(str, `{"version"}`, strconv.Quote(fileContent.VersionID))
return str
}