Remove tags for object versions (#4563)

Signed-off-by: Shubhendu Ram Tripathi <shubhendu@minio.io>
This commit is contained in:
Shubhendu
2023-05-01 08:04:31 -07:00
committed by GitHub
parent a7e729fd38
commit b005b9a94d
3 changed files with 152 additions and 33 deletions
+54 -11
View File
@@ -46,6 +46,10 @@ var tagListFlags = []cli.Flag{
Name: "versions",
Usage: "list tags on all versions for an object",
},
cli.BoolFlag{
Name: "recursive, r",
Usage: "recursivley show tags for all objects",
},
}
var tagListCmd = cli.Command{
@@ -85,6 +89,12 @@ EXAMPLES:
6. List the tags assigned to a bucket in JSON format.
{{.Prompt}} {{.HelpName}} --json s3/testbucket
7. List the tags recursively for all the objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} --recursive myminio/testbucket
8. Show the tags recursively for all versions of all objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} --recursive --versions myminio/testbucket
`,
}
@@ -114,8 +124,12 @@ func (t tagListMessage) String() string {
sort.Strings(keys)
maxKeyLen += 2 // add len(" :")
strName := t.URL
if strings.TrimSpace(t.VersionID) != "" {
strName += " (" + t.VersionID + ")"
}
strs := []string{
fmt.Sprintf("%v%*v %v", console.Colorize("Name", "Name"), maxKeyLen-4, ":", console.Colorize("Name", t.URL+" ("+t.VersionID+")")),
fmt.Sprintf("%v%*v %v", console.Colorize("Name", "Name"), maxKeyLen-4, ":", console.Colorize("Name", strName)),
}
for _, key := range keys {
@@ -133,7 +147,7 @@ func (t tagListMessage) String() string {
}
// parseTagListSyntax performs command-line input validation for tag list command.
func parseTagListSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withOlderVersions bool) {
func parseTagListSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withOlderVersions bool, recursive bool) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, globalErrorExitStatus)
}
@@ -142,6 +156,7 @@ func parseTagListSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef
versionID = ctx.String("version-id")
withOlderVersions = ctx.Bool("versions")
rewind := ctx.String("rewind")
recursive = ctx.Bool("recursive")
if versionID != "" && rewind != "" {
fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind flags at the same time")
@@ -175,6 +190,16 @@ func showTags(ctx context.Context, clnt Client, versionID string) {
})
}
func showTagsSingle(ctx context.Context, alias, url, versionID string) *probe.Error {
newClnt, err := newClientFromAlias(alias, url)
if err != nil {
return err
}
showTags(ctx, newClnt, versionID)
return nil
}
func mainListTag(cliCtx *cli.Context) error {
ctx, cancelListTag := context.WithCancel(globalContext)
defer cancelListTag()
@@ -184,7 +209,7 @@ func mainListTag(cliCtx *cli.Context) error {
console.SetColor("Value", color.New(color.FgYellow))
console.SetColor("NoTags", color.New(color.FgRed))
targetURL, versionID, timeRef, withVersions := parseTagListSyntax(cliCtx)
targetURL, versionID, timeRef, withVersions, recursive := parseTagListSyntax(cliCtx)
if timeRef.IsZero() && withVersions {
timeRef = time.Now().UTC()
}
@@ -192,14 +217,32 @@ func mainListTag(cliCtx *cli.Context) error {
clnt, err := newClient(targetURL)
fatalIf(err, "Unable to initialize target "+targetURL)
if timeRef.IsZero() && !withVersions {
showTags(ctx, clnt, versionID)
} else {
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
}
showTags(ctx, clnt, content.VersionID)
alias, urlStr, _ := mustExpandAlias(targetURL)
if timeRef.IsZero() && !withVersions && !recursive {
err := showTagsSingle(ctx, alias, urlStr, versionID)
fatalIf(err.Trace(), "Unable to show tags on `%s`", targetURL)
return nil
}
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions, Recursive: recursive}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
continue
}
// Skip if its delete marker
if content.IsDeleteMarker {
continue
}
if !recursive && alias+getKey(content) != getStandardizedURL(targetURL) {
break
}
err := showTagsSingle(ctx, alias, content.URL.String(), content.VersionID)
if err != nil {
errorIf(err.Trace(clnt.GetURL().String()), "Invalid URL")
continue
}
}
+50 -12
View File
@@ -19,6 +19,7 @@ package cmd
import (
"context"
"strings"
"time"
"github.com/fatih/color"
@@ -39,7 +40,11 @@ var tagRemoveFlags = []cli.Flag{
},
cli.BoolFlag{
Name: "versions",
Usage: "remote tags on multiple versions of an object",
Usage: "remove tags on multiple versions of an object",
},
cli.BoolFlag{
Name: "recursive, r",
Usage: "recursivley remove tags for all objects",
},
}
@@ -74,6 +79,12 @@ EXAMPLES:
4. Remove the tags assigned to a bucket.
{{.Prompt}} {{.HelpName}} play/testbucket
5. Remove the tags recursively for all the objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} --recursive myminio/testbucket
6. Remove the tags recursively for all versions of all objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} --recursive --versions myminio/testbucket
`,
}
@@ -88,7 +99,7 @@ type tagRemoveMessage struct {
func (t tagRemoveMessage) String() string {
var msg string
msg += "Tags removed for " + t.Name
if t.VersionID != "" {
if strings.TrimSpace(t.VersionID) != "" {
msg += " (" + t.VersionID + ")"
}
msg += "."
@@ -102,7 +113,7 @@ func (t tagRemoveMessage) JSON() string {
return string(msgBytes)
}
func parseRemoveTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool) {
func parseRemoveTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool, recursive bool) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, globalErrorExitStatus)
}
@@ -111,6 +122,7 @@ func parseRemoveTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRe
versionID = ctx.String("version-id")
withVersions = ctx.Bool("versions")
rewind := ctx.String("rewind")
recursive = ctx.Bool("recursive")
if versionID != "" && (rewind != "" || withVersions) {
fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind or --versions flags at the same time")
@@ -140,13 +152,23 @@ func deleteTags(ctx context.Context, clnt Client, versionID string) {
})
}
func deleteTagsSingle(ctx context.Context, alias, url, versionID string) *probe.Error {
newClnt, err := newClientFromAlias(alias, url)
if err != nil {
return err
}
deleteTags(ctx, newClnt, versionID)
return nil
}
func mainRemoveTag(cliCtx *cli.Context) error {
ctx, cancelList := context.WithCancel(globalContext)
defer cancelList()
console.SetColor("Remove", color.New(color.FgGreen))
targetURL, versionID, timeRef, withVersions := parseRemoveTagSyntax(cliCtx)
targetURL, versionID, timeRef, withVersions, recursive := parseRemoveTagSyntax(cliCtx)
if timeRef.IsZero() && withVersions {
timeRef = time.Now().UTC()
}
@@ -154,14 +176,30 @@ func mainRemoveTag(cliCtx *cli.Context) error {
clnt, pErr := newClient(targetURL)
fatalIf(pErr, "Unable to initialize target "+targetURL)
if timeRef.IsZero() && !withVersions {
deleteTags(ctx, clnt, versionID)
} else {
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
}
deleteTags(ctx, clnt, content.VersionID)
alias, urlStr, _ := mustExpandAlias(targetURL)
if timeRef.IsZero() && !withVersions && !recursive {
err := deleteTagsSingle(ctx, alias, urlStr, versionID)
fatalIf(err.Trace(), "Unable to remove tags on `%s`", targetURL)
return nil
}
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions, Recursive: recursive}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
}
// Skip if its delete marker
if content.IsDeleteMarker {
continue
}
if !recursive && alias+getKey(content) != getStandardizedURL(targetURL) {
break
}
err := deleteTagsSingle(ctx, alias, content.URL.String(), content.VersionID)
if err != nil {
errorIf(err.Trace(clnt.GetURL().String()), "Invalid URL")
continue
}
}
return nil
+48 -10
View File
@@ -41,6 +41,10 @@ var tagSetFlags = []cli.Flag{
Name: "versions",
Usage: "set tags on multiple versions for an object",
},
cli.BoolFlag{
Name: "recursive, r",
Usage: "recursivley set tags for all objects of subdirs",
},
}
var tagSetCmd = cli.Command{
@@ -73,6 +77,12 @@ EXAMPLES:
4. Assign tags to a bucket.
{{.Prompt}} {{.HelpName}} myminio/testbucket "key1=value1&key2=value2&key3=value3"
5. Assign tags recursively to all the objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} myminio/testbucket --recursive "key1=value1&key2=value2&key3=value3"
6. Assign tags recursively to all versions of all objects of subdirs of bucket.
{{.Prompt}} {{.HelpName}} myminio/testbucket --recursive --versions "key1=value1&key2=value2&key3=value3"
`,
}
@@ -101,7 +111,7 @@ func (t tagSetMessage) JSON() string {
return string(msgBytes)
}
func parseSetTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool, tags string) {
func parseSetTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool, tags string, recursive bool) {
if len(ctx.Args()) != 2 || ctx.Args().Get(1) == "" {
showCommandHelpAndExit(ctx, globalErrorExitStatus)
}
@@ -111,6 +121,7 @@ func parseSetTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef t
versionID = ctx.String("version-id")
withVersions = ctx.Bool("versions")
rewind := ctx.String("rewind")
recursive = ctx.Bool("recursive")
if versionID != "" && (rewind != "" || withVersions) {
fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind or --versions flags at the same time")
@@ -139,13 +150,23 @@ func setTags(ctx context.Context, clnt Client, versionID, tags string) {
})
}
func setTagsSingle(ctx context.Context, alias, url, versionID, tags string) *probe.Error {
newClnt, err := newClientFromAlias(alias, url)
if err != nil {
return err
}
setTags(ctx, newClnt, versionID, tags)
return nil
}
func mainSetTag(cliCtx *cli.Context) error {
ctx, cancelSetTag := context.WithCancel(globalContext)
defer cancelSetTag()
console.SetColor("List", color.New(color.FgGreen))
targetURL, versionID, timeRef, withVersions, tags := parseSetTagSyntax(cliCtx)
targetURL, versionID, timeRef, withVersions, tags, recursive := parseSetTagSyntax(cliCtx)
if timeRef.IsZero() && withVersions {
timeRef = time.Now().UTC()
}
@@ -153,14 +174,31 @@ func mainSetTag(cliCtx *cli.Context) error {
clnt, err := newClient(targetURL)
fatalIf(err.Trace(cliCtx.Args()...), "Unable to initialize target "+targetURL)
if timeRef.IsZero() && !withVersions {
setTags(ctx, clnt, versionID, tags)
} else {
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
}
setTags(ctx, clnt, content.VersionID, tags)
alias, urlStr, _ := mustExpandAlias(targetURL)
if timeRef.IsZero() && !withVersions && !recursive {
err := setTagsSingle(ctx, alias, urlStr, versionID, tags)
fatalIf(err.Trace(), "Unable to set tags on `%s`", targetURL)
return nil
}
for content := range clnt.List(ctx, ListOptions{TimeRef: timeRef, WithOlderVersions: withVersions, Recursive: recursive}) {
if content.Err != nil {
fatalIf(content.Err.Trace(), "Unable to list target "+targetURL)
continue
}
// Dont set tag for the delete marker
if content.IsDeleteMarker {
continue
}
if !recursive && alias+getKey(content) != getStandardizedURL(targetURL) {
break
}
err := setTagsSingle(ctx, alias, content.URL.String(), content.VersionID, tags)
if err != nil {
errorIf(err.Trace(clnt.GetURL().String()), "Invalid URL")
continue
}
}