Allow storageclass to be specified on mc ls (#3915)

This commit is contained in:
Villena Guillaume
2022-01-28 14:42:21 -08:00
committed by GitHub
parent 43524f7978
commit 9914c8187e
3 changed files with 23 additions and 7 deletions
+14 -4
View File
@@ -53,6 +53,10 @@ var (
Name: "summarize",
Usage: "display summary information (number of objects, total size)",
},
cli.StringFlag{
Name: "storage-class, sc",
Usage: "filter to specified storage class",
},
}
)
@@ -102,6 +106,9 @@ EXAMPLES:
9. List all objects on mybucket, summarize the number of objects and total size.
{{.Prompt}} {{.HelpName}} --summarize s3/mybucket/
10. List all objects on mybucket, for the GLACIER storage class
{{.Prompt}} {{.HelpName}} --storage-class 'GLACIER' s3/mybucket
`,
}
@@ -147,7 +154,7 @@ func parseRewindFlag(rewind string) (timeRef time.Time) {
}
// checkListSyntax - validate all the passed arguments
func checkListSyntax(ctx context.Context, cliCtx *cli.Context) ([]string, bool, bool, bool, time.Time, bool) {
func checkListSyntax(ctx context.Context, cliCtx *cli.Context) ([]string, bool, bool, bool, time.Time, bool, string) {
args := cliCtx.Args()
if !cliCtx.Args().Present() {
args = []string{"."}
@@ -168,7 +175,9 @@ func checkListSyntax(ctx context.Context, cliCtx *cli.Context) ([]string, bool,
timeRef = time.Now().UTC()
}
return args, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions
storageClasss := cliCtx.String("storage-class")
return args, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions, storageClasss
}
// mainList - is a handler for mc ls command
@@ -186,9 +195,10 @@ func mainList(cliCtx *cli.Context) error {
console.SetColor("Size", color.New(color.FgYellow))
console.SetColor("Time", color.New(color.FgGreen))
console.SetColor("Summarize", color.New(color.Bold))
console.SetColor("SC", color.New(color.FgBlue))
// check 'ls' cliCtx arguments.
args, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions := checkListSyntax(ctx, cliCtx)
args, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions, storageClassFilter := checkListSyntax(ctx, cliCtx)
var cErr error
for _, targetURL := range args {
@@ -203,7 +213,7 @@ func mainList(cliCtx *cli.Context) error {
fatalIf(err.Trace(targetURL), "Unable to initialize target `"+targetURL+"`.")
}
}
if e := doList(ctx, clnt, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions); e != nil {
if e := doList(ctx, clnt, isRecursive, isIncomplete, isSummary, timeRef, withOlderVersions, storageClassFilter); e != nil {
cErr = e
}
}
+8 -2
View File
@@ -50,6 +50,7 @@ type contentMessage struct {
VersionOrd int `json:"versionOrdinal,omitempty"`
VersionIndex int `json:"versionIndex,omitempty"`
IsDeleteMarker bool `json:"isDeleteMarker,omitempty"`
StorageClass string `json:"storageClass,omitempty"`
}
// String colorized string message.
@@ -58,6 +59,10 @@ func (c contentMessage) String() string {
message += console.Colorize("Size", fmt.Sprintf("%7s", strings.Join(strings.Fields(humanize.IBytes(uint64(c.Size))), "")))
fileDesc := ""
if c.StorageClass != "" {
message += " " + console.Colorize("SC", c.StorageClass)
}
if c.VersionID != "" {
fileDesc += console.Colorize("VersionID", " "+c.VersionID) + console.Colorize("VersionOrd", fmt.Sprintf(" v%d", c.VersionOrd))
if c.IsDeleteMarker {
@@ -131,6 +136,7 @@ func generateContentMessages(clntURL ClientURL, ctnts []*ClientContent, printAll
}()
contentMsg.Size = c.Size
contentMsg.StorageClass = c.StorageClass
md5sum := strings.TrimPrefix(c.ETag, "\"")
md5sum = strings.TrimSuffix(md5sum, "\"")
contentMsg.ETag = md5sum
@@ -195,7 +201,7 @@ func printObjectVersions(clntURL ClientURL, ctntVersions []*ClientContent, print
}
// doList - list all entities inside a folder.
func doList(ctx context.Context, clnt Client, isRecursive, isIncomplete, isSummary bool, timeRef time.Time, withOlderVersions bool) error {
func doList(ctx context.Context, clnt Client, isRecursive, isIncomplete, isSummary bool, timeRef time.Time, withOlderVersions bool, filter string) error {
var (
lastPath string
perObjectVersions []*ClientContent
@@ -233,7 +239,7 @@ func doList(ctx context.Context, clnt Client, isRecursive, isIncomplete, isSumma
continue
}
if content.StorageClass == s3StorageClassGlacier {
if content.StorageClass != "" && filter != "" && filter != "*" && content.StorageClass != filter {
continue
}
+1 -1
View File
@@ -284,7 +284,7 @@ func mainTree(cliCtx *cli.Context) error {
}
clnt, err := newClientFromAlias(targetAlias, targetURL)
fatalIf(err.Trace(targetURL), "Unable to initialize target `"+targetURL+"`.")
if e := doList(ctx, clnt, true, false, false, timeRef, false); e != nil {
if e := doList(ctx, clnt, true, false, false, timeRef, false, "*"); e != nil {
cErr = e
}
}