diff --git a/cmd/ilm-rule-list.go b/cmd/ilm-rule-list.go index 91f457b4..813a7988 100644 --- a/cmd/ilm-rule-list.go +++ b/cmd/ilm-rule-list.go @@ -98,16 +98,13 @@ func (i ilmListMessage) JSON() string { return string(msgBytes) } -// validateILMListFlagSet - Only one of these flags needs to be set for display: --json, --expiry, --transition +// validateILMListFlagSet - validates ilm list flags func validateILMListFlagSet(ctx *cli.Context) bool { - flags := [...]bool{ctx.Bool("expiry"), ctx.Bool("transition"), ctx.Bool("json")} - found := false - for _, flag := range flags { - if found && flag { - return false - } else if flag { - found = true - } + expiryOnly := ctx.Bool("expiry") + transitionOnly := ctx.Bool("transition") + // Only one of expiry or transition rules can be filtered + if expiryOnly && transitionOnly { + return false } return true } @@ -153,6 +150,9 @@ func mainILMList(cliCtx *cli.Context) error { "Unable to ls lifecycle configuration") } + // applies listing filter on ILM rules + ilmCfg.Rules = filter.Apply(ilmCfg.Rules) + if globalJSON { printMsg(ilmListMessage{ Status: "success", @@ -164,7 +164,7 @@ func mainILMList(cliCtx *cli.Context) error { return nil } - for _, tbl := range ilm.ToTables(ilmCfg, filter) { + for _, tbl := range ilm.ToTables(ilmCfg) { rows := tbl.Rows() if len(rows) == 0 { continue diff --git a/cmd/ilm/table.go b/cmd/ilm/table.go index 41463fbc..1a792972 100644 --- a/cmd/ilm/table.go +++ b/cmd/ilm/table.go @@ -17,11 +17,15 @@ package ilm -import "github.com/jedib0t/go-pretty/v6/table" +import ( + "github.com/jedib0t/go-pretty/v6/table" + "github.com/minio/minio-go/v7/pkg/lifecycle" +) // Table interface provides methods when implemented allows a []T to be rendered // as a table. type Table interface { + Len() int Title() string Rows() []table.Row ColumnHeaders() table.Row @@ -39,6 +43,30 @@ const ( TransitionOnly ) +// Apply applies f on rules and filters lifecycle rules matching it +func (f LsFilter) Apply(rules []lifecycle.Rule) []lifecycle.Rule { + check := func(rule lifecycle.Rule) bool { + switch f { + case ExpiryOnly: + return !rule.Expiration.IsNull() || !rule.NoncurrentVersionExpiration.IsDaysNull() || + rule.NoncurrentVersionExpiration.NewerNoncurrentVersions > 0 + case TransitionOnly: + return !rule.Transition.IsNull() || !rule.NoncurrentVersionTransition.IsStorageClassEmpty() + } + return true + } + + var n int + for _, rule := range rules { + if check(rule) { + rules[n] = rule + n++ + } + } + rules = rules[:n] + return rules +} + type expirationCurrentRow struct { ID string Status string @@ -50,6 +78,10 @@ type expirationCurrentRow struct { type expirationCurrentTable []expirationCurrentRow +func (e expirationCurrentTable) Len() int { + return len(e) +} + func (e expirationCurrentTable) Title() string { return "Expiration for latest version (Expiration)" } @@ -82,6 +114,10 @@ type expirationNoncurrentRow struct { KeepVersions int } +func (e expirationNoncurrentTable) Len() int { + return len(e) +} + func (e expirationNoncurrentTable) Title() string { return "Expiration for older versions (NoncurrentVersionExpiration)" } @@ -114,6 +150,10 @@ type tierCurrentRow struct { Tier string } +func (t tierCurrentTable) Len() int { + return len(t) +} + func (t tierCurrentTable) Title() string { return "Transition for latest version (Transition)" } @@ -140,6 +180,10 @@ type ( tierNoncurrentRow tierCurrentRow ) +func (t tierNoncurrentTable) Len() int { + return len(t) +} + func (t tierNoncurrentTable) Title() string { return "Transition for older versions (NoncurrentVersionTransition)" } diff --git a/cmd/ilm/utils.go b/cmd/ilm/utils.go index 81adf68f..9741048c 100644 --- a/cmd/ilm/utils.go +++ b/cmd/ilm/utils.go @@ -82,7 +82,7 @@ func getTransitionDays(rule lifecycle.Rule) int { } // ToTables converts a lifecycle.Configuration into its tabular representation. -func ToTables(cfg *lifecycle.Configuration, filter LsFilter) []Table { +func ToTables(cfg *lifecycle.Configuration) []Table { var tierCur tierCurrentTable var tierNoncur tierNoncurrentTable var expCur expirationCurrentTable @@ -130,12 +130,15 @@ func ToTables(cfg *lifecycle.Configuration, filter LsFilter) []Table { } } - switch filter { - case ExpiryOnly: - return []Table{expCur, expNoncur} - case TransitionOnly: - return []Table{tierCur, tierNoncur} - default: - return []Table{tierCur, tierNoncur, expCur, expNoncur} + var table []Table + inclTbl := func(tbl Table) { + if len(tbl.Rows()) > 0 { + table = append(table, tbl) + } } + inclTbl(expCur) + inclTbl(expNoncur) + inclTbl(tierCur) + inclTbl(tierNoncur) + return table }