Skip to content

Commit eb6702d

Browse files
committed
OCM-1520 | feat: Add --hide-empty-columns flag to rosa list commands
1 parent 8f44abd commit eb6702d

File tree

41 files changed

+787
-286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+787
-286
lines changed

cmd/list/accessrequests/cmd.go

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strings"
78
"text/tabwriter"
89
"time"
910

@@ -38,10 +39,32 @@ func NewListAccessRequestsCommand() *cobra.Command {
3839
}
3940

4041
output.AddFlag(cmd)
42+
output.AddHideEmptyColumnsFlag(cmd)
4143
ocm.AddOptionalClusterFlag(cmd)
4244
return cmd
4345
}
4446

47+
func getAccessRequestsOutput(clusterId string, accessRequests []*v1.AccessRequest) (string, bool, string) {
48+
output := "STATE\tID\tCLUSTER ID\tUPDATED AT\n"
49+
hasPending := false
50+
id := "<ID>"
51+
for _, accessRequest := range accessRequests {
52+
if accessRequest.Status().State() == v1.AccessRequestStatePending ||
53+
accessRequest.Status().State() == v1.AccessRequestStateApproved {
54+
hasPending = true
55+
if clusterId != "" {
56+
id = accessRequest.ID()
57+
}
58+
}
59+
output += fmt.Sprintf("%s\t%s\t%s\t%s\n",
60+
accessRequest.Status().State(),
61+
accessRequest.ID(),
62+
accessRequest.ClusterId(),
63+
accessRequest.UpdatedAt().UTC().Format(time.UnixDate))
64+
}
65+
return output, hasPending, id
66+
}
67+
4568
func ListAccessRequestsRunner() rosa.CommandRunner {
4669
return func(_ context.Context, r *rosa.Runtime, cmd *cobra.Command, _ []string) error {
4770
clusterId := ""
@@ -67,12 +90,36 @@ func ListAccessRequestsRunner() rosa.CommandRunner {
6790
}
6891
return nil
6992
}
93+
94+
headers := []string{"STATE", "ID", "CLUSTER ID", "UPDATED AT"}
95+
96+
var tableData [][]string
97+
for _, accessRequest := range accessRequests {
98+
row := []string{
99+
string(accessRequest.Status().State()),
100+
accessRequest.ID(),
101+
accessRequest.ClusterId(),
102+
accessRequest.UpdatedAt().UTC().Format(time.UnixDate),
103+
}
104+
tableData = append(tableData, row)
105+
}
106+
107+
if output.ShouldHideEmptyColumns() {
108+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
109+
}
110+
70111
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
71-
output, hasPending, pendingId := getAccessRequestsOutput(clusterId, accessRequests)
72-
fmt.Fprint(writer, output)
112+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t"))
113+
for _, row := range tableData {
114+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
115+
}
116+
73117
if err := writer.Flush(); err != nil {
74118
return err
75119
}
120+
121+
_, hasPending, pendingId := getAccessRequestsOutput(clusterId, accessRequests)
122+
76123
if hasPending {
77124
r.Reporter.Infof("Run the following command to approve or deny the Access Request:\n\n"+
78125
" rosa create decision --access-request %s --decision Approved\n"+
@@ -83,24 +130,3 @@ func ListAccessRequestsRunner() rosa.CommandRunner {
83130
return nil
84131
}
85132
}
86-
87-
func getAccessRequestsOutput(clusterId string, accessRequests []*v1.AccessRequest) (string, bool, string) {
88-
output := "STATE\tID\tCLUSTER ID\tUPDATED AT\n"
89-
hasPending := false
90-
id := "<ID>"
91-
for _, accessRequest := range accessRequests {
92-
if accessRequest.Status().State() == v1.AccessRequestStatePending {
93-
hasPending = true
94-
if clusterId != "" {
95-
id = accessRequest.ID()
96-
}
97-
}
98-
output += fmt.Sprintf("%s\t%s\t%s\t%s\n",
99-
accessRequest.Status().State(),
100-
accessRequest.ID(),
101-
accessRequest.ClusterId(),
102-
accessRequest.UpdatedAt().Format(time.UnixDate))
103-
}
104-
105-
return output, hasPending, id
106-
}

cmd/list/accessrequests/cmd_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package accessrequests
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
76
"time"
87

@@ -126,7 +125,6 @@ var _ = Describe("rosa attach policy", func() {
126125
Expect(err).NotTo(HaveOccurred())
127126

128127
stdOut, _ := t.StdOutReader.Read()
129-
fmt.Println(stdOut)
130128
Expect(stdOut).To(Equal(accessRequestsOutput))
131129
})
132130

cmd/list/accountroles/cmd.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package accountroles
1919
import (
2020
"fmt"
2121
"os"
22+
"strings"
2223
"text/tabwriter"
2324
"time"
2425

@@ -55,6 +56,7 @@ func init() {
5556
"List only account-roles that are associated with the given version.",
5657
)
5758
output.AddFlag(Cmd)
59+
output.AddHideEmptyColumnsFlag(Cmd)
5860
}
5961

6062
func run(_ *cobra.Command, _ []string) {
@@ -108,23 +110,37 @@ func run(_ *cobra.Command, _ []string) {
108110
os.Exit(0)
109111
}
110112

111-
// Create the writer that will be used to print the tabulated results:
112-
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
113-
fmt.Fprintf(writer, "ROLE NAME\tROLE TYPE\tROLE ARN\tOPENSHIFT VERSION\tAWS Managed\n")
113+
headers := []string{"ROLE NAME", "ROLE TYPE", "ROLE ARN", "OPENSHIFT VERSION", "AWS Managed"}
114+
115+
var tableData [][]string
114116
for _, accountRole := range accountRoles {
115117
awsManaged := "No"
116118
if accountRole.ManagedPolicy {
117119
awsManaged = "Yes"
118120
}
119-
fmt.Fprintf(
120-
writer,
121-
"%s\t%s\t%s\t%s\t%s\n",
121+
row := []string{
122122
accountRole.RoleName,
123123
accountRole.RoleType,
124124
accountRole.RoleARN,
125125
accountRole.Version,
126126
awsManaged,
127-
)
127+
}
128+
tableData = append(tableData, row)
129+
}
130+
131+
if output.ShouldHideEmptyColumns() {
132+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
133+
}
134+
135+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
136+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t"))
137+
for _, row := range tableData {
138+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
139+
}
140+
141+
if err := writer.Flush(); err != nil {
142+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
143+
os.Exit(1)
128144
}
129-
writer.Flush()
145+
130146
}

cmd/list/addon/cmd.go

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package addon
1919
import (
2020
"fmt"
2121
"os"
22+
"strings"
2223
"text/tabwriter"
2324

2425
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
@@ -56,6 +57,7 @@ func init() {
5657
)
5758

5859
output.AddFlag(Cmd)
60+
output.AddHideEmptyColumnsFlag(Cmd)
5961
}
6062

6163
// When no specific cluster id is provided by the user, this function lists all available AddOns
@@ -81,18 +83,35 @@ func listAllAddOns(r *rosa.Runtime) {
8183
os.Exit(0)
8284
}
8385

84-
// Create the writer that will be used to print the tabulated results:
85-
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
86-
fmt.Fprintf(writer, "ID\t\tNAME\t\tAVAILABILITY\n")
86+
headers := []string{"ID", "NAME", "AVAILABILITY"}
87+
var tableData [][]string
8788
for _, addOnResource := range addOnResources {
8889
availability := "unavailable"
8990
if addOnResource.Available {
9091
availability = "available"
9192
}
92-
fmt.Fprintf(writer, "%s\t\t%s\t\t%s\n", addOnResource.AddOn.ID(), addOnResource.AddOn.Name(), availability)
93+
row := []string{
94+
addOnResource.AddOn.ID(),
95+
addOnResource.AddOn.Name(),
96+
availability,
97+
}
98+
tableData = append(tableData, row)
99+
}
100+
101+
if output.ShouldHideEmptyColumns() {
102+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
103+
}
104+
105+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
106+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t\t"))
107+
for _, row := range tableData {
108+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t\t"))
93109
}
94-
writer.Flush()
95110

111+
if err := writer.Flush(); err != nil {
112+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
113+
os.Exit(1)
114+
}
96115
os.Exit(0)
97116
}
98117

@@ -142,13 +161,32 @@ func listClusterAddOns(clusterKey string, r *rosa.Runtime) {
142161
os.Exit(0)
143162
}
144163

145-
// Create the writer that will be used to print the tabulated results:
146-
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
147-
fmt.Fprintf(writer, "ID\t\tNAME\t\tSTATE\n")
164+
headers := []string{"ID", "NAME", "STATE"}
165+
166+
var tableData [][]string
148167
for _, clusterAddOn := range clusterAddOns {
149-
fmt.Fprintf(writer, "%s\t\t%s\t\t%s\n", clusterAddOn.ID, clusterAddOn.Name, clusterAddOn.State)
168+
row := []string{
169+
clusterAddOn.ID,
170+
clusterAddOn.Name,
171+
clusterAddOn.State,
172+
}
173+
tableData = append(tableData, row)
174+
}
175+
176+
if output.ShouldHideEmptyColumns() {
177+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
178+
}
179+
180+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
181+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t\t"))
182+
for _, row := range tableData {
183+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t\t"))
184+
}
185+
186+
if err := writer.Flush(); err != nil {
187+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
188+
os.Exit(1)
150189
}
151-
writer.Flush()
152190
}
153191

154192
func run(_ *cobra.Command, _ []string) {

cmd/list/breakglasscredential/cmd.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package breakglasscredential
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"text/tabwriter"
78

89
"github.com/spf13/cobra"
@@ -27,6 +28,7 @@ var Cmd = &cobra.Command{
2728
func init() {
2829
ocm.AddClusterFlag(Cmd)
2930
output.AddFlag(Cmd)
31+
output.AddHideEmptyColumnsFlag(Cmd)
3032
}
3133

3234
func run(cmd *cobra.Command, _ []string) {
@@ -69,18 +71,29 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command) error {
6971
return nil
7072
}
7173

72-
// Create the writer that will be used to print the tabulated results:
73-
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
74-
75-
fmt.Fprintf(writer, "ID\tUSERNAME\tSTATUS\n")
74+
headers := []string{"ID", "USERNAME", "STATUS"}
75+
var tableData [][]string
7676
for _, credential := range breakGlassCredentials {
77-
fmt.Fprintf(writer, "%s\t%s\t%s\n",
77+
row := []string{
7878
credential.ID(),
7979
credential.Username(),
80-
credential.Status(),
81-
)
80+
string(credential.Status()),
81+
}
82+
tableData = append(tableData, row)
83+
}
84+
85+
if output.ShouldHideEmptyColumns() {
86+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
8287
}
83-
writer.Flush()
8488

89+
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
90+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t"))
91+
for _, row := range tableData {
92+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
93+
}
94+
95+
if err := writer.Flush(); err != nil {
96+
return err
97+
}
8598
return nil
8699
}

cmd/list/cluster/cmd.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cluster
1919
import (
2020
"fmt"
2121
"os"
22+
"strings"
2223
"text/tabwriter"
2324

2425
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
@@ -52,6 +53,7 @@ func init() {
5253
flags.SortFlags = false
5354

5455
output.AddFlag(Cmd)
56+
output.AddHideEmptyColumnsFlag(Cmd)
5557
flags.BoolVarP(&args.listAll, "all", "a", false, "List all clusters across different AWS "+
5658
"accounts under the same Red Hat organization")
5759
flags.StringVar(&args.accountRoleArn, "account-role-arn", "", "List all clusters "+
@@ -107,25 +109,38 @@ func run(_ *cobra.Command, _ []string) {
107109
os.Exit(0)
108110
}
109111

110-
// Create the writer that will be used to print the tabulated results:
111-
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
112-
fmt.Fprintf(writer, "ID\tNAME\tSTATE\tTOPOLOGY\n")
112+
headers := []string{"ID", "NAME", "STATE", "TOPOLOGY"}
113+
var tableData [][]string
113114
for _, cluster := range clusters {
114-
typeOutput := "Classic"
115+
typeOutput := ""
115116
if cluster.AWS() != nil && cluster.AWS().STS() != nil && cluster.AWS().STS().Enabled() {
116117
typeOutput = "Classic (STS)"
117118
}
118119
if cluster.Hypershift().Enabled() {
119120
typeOutput = "Hosted CP"
120121
}
121-
fmt.Fprintf(
122-
writer,
123-
"%s\t%s\t%s\t%s\n",
122+
123+
row := []string{
124124
cluster.ID(),
125125
cluster.Name(),
126-
cluster.State(),
126+
string(cluster.State()),
127127
typeOutput,
128-
)
128+
}
129+
tableData = append(tableData, row)
130+
}
131+
132+
if output.ShouldHideEmptyColumns() {
133+
headers, tableData = output.RemoveEmptyColumns(headers, tableData)
134+
}
135+
136+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
137+
fmt.Fprintf(writer, "%s\n", strings.Join(headers, "\t"))
138+
for _, row := range tableData {
139+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
140+
}
141+
142+
if err := writer.Flush(); err != nil {
143+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
144+
os.Exit(1)
129145
}
130-
writer.Flush()
131146
}

0 commit comments

Comments
 (0)