Skip to content

Commit 2f270f0

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

File tree

41 files changed

+785
-274
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

+785
-274
lines changed

cmd/list/accessrequests/cmd.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewListAccessRequestsCommand() *cobra.Command {
3838
}
3939

4040
output.AddFlag(cmd)
41+
output.AddHideEmptyColumnsFlag(cmd)
4142
ocm.AddOptionalClusterFlag(cmd)
4243
return cmd
4344
}
@@ -67,12 +68,34 @@ func ListAccessRequestsRunner() rosa.CommandRunner {
6768
}
6869
return nil
6970
}
71+
72+
headers := []string{"STATE", "ID", "CLUSTER ID", "UPDATED AT"}
73+
74+
var tableData [][]string
75+
for _, accessRequest := range accessRequests {
76+
row := []string{
77+
string(accessRequest.Status().State()),
78+
accessRequest.ID(),
79+
accessRequest.ClusterId(),
80+
accessRequest.UpdatedAt().UTC().Format(time.UnixDate),
81+
}
82+
tableData = append(tableData, row)
83+
}
84+
85+
if output.ShouldHideEmptyColumns() {
86+
tableData = output.RemoveEmptyColumns(headers, tableData)
87+
} else {
88+
tableData = append([][]string{headers}, tableData...)
89+
}
90+
7091
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
71-
output, hasPending, pendingId := getAccessRequestsOutput(clusterId, accessRequests)
72-
fmt.Fprint(writer, output)
92+
output.BuildTable(writer, "\t", tableData)
7393
if err := writer.Flush(); err != nil {
7494
return err
7595
}
96+
97+
_, hasPending, pendingId := getAccessRequestsOutput(clusterId, accessRequests)
98+
7699
if hasPending {
77100
r.Reporter.Infof("Run the following command to approve or deny the Access Request:\n\n"+
78101
" rosa create decision --access-request %s --decision Approved\n"+
@@ -89,7 +112,8 @@ func getAccessRequestsOutput(clusterId string, accessRequests []*v1.AccessReques
89112
hasPending := false
90113
id := "<ID>"
91114
for _, accessRequest := range accessRequests {
92-
if accessRequest.Status().State() == v1.AccessRequestStatePending {
115+
if accessRequest.Status().State() == v1.AccessRequestStatePending ||
116+
accessRequest.Status().State() == v1.AccessRequestStateApproved {
93117
hasPending = true
94118
if clusterId != "" {
95119
id = accessRequest.ID()
@@ -99,8 +123,7 @@ func getAccessRequestsOutput(clusterId string, accessRequests []*v1.AccessReques
99123
accessRequest.Status().State(),
100124
accessRequest.ID(),
101125
accessRequest.ClusterId(),
102-
accessRequest.UpdatedAt().Format(time.UnixDate))
126+
accessRequest.UpdatedAt().UTC().Format(time.UnixDate))
103127
}
104-
105128
return output, hasPending, id
106129
}

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: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package accountroles
1818

1919
import (
20-
"fmt"
2120
"os"
2221
"text/tabwriter"
2322
"time"
@@ -55,6 +54,7 @@ func init() {
5554
"List only account-roles that are associated with the given version.",
5655
)
5756
output.AddFlag(Cmd)
57+
output.AddHideEmptyColumnsFlag(Cmd)
5858
}
5959

6060
func run(_ *cobra.Command, _ []string) {
@@ -108,23 +108,35 @@ func run(_ *cobra.Command, _ []string) {
108108
os.Exit(0)
109109
}
110110

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")
111+
headers := []string{"ROLE NAME", "ROLE TYPE", "ROLE ARN", "OPENSHIFT VERSION", "AWS Managed"}
112+
113+
var tableData [][]string
114114
for _, accountRole := range accountRoles {
115115
awsManaged := "No"
116116
if accountRole.ManagedPolicy {
117117
awsManaged = "Yes"
118118
}
119-
fmt.Fprintf(
120-
writer,
121-
"%s\t%s\t%s\t%s\t%s\n",
119+
row := []string{
122120
accountRole.RoleName,
123121
accountRole.RoleType,
124122
accountRole.RoleARN,
125123
accountRole.Version,
126124
awsManaged,
127-
)
125+
}
126+
tableData = append(tableData, row)
127+
}
128+
129+
if output.ShouldHideEmptyColumns() {
130+
tableData = output.RemoveEmptyColumns(headers, tableData)
131+
} else {
132+
tableData = append([][]string{headers}, tableData...)
128133
}
129-
writer.Flush()
134+
135+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
136+
output.BuildTable(writer, "\t", tableData)
137+
if err := writer.Flush(); err != nil {
138+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
139+
os.Exit(1)
140+
}
141+
130142
}

cmd/list/addon/cmd.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package addon
1818

1919
import (
20-
"fmt"
2120
"os"
2221
"text/tabwriter"
2322

@@ -56,6 +55,7 @@ func init() {
5655
)
5756

5857
output.AddFlag(Cmd)
58+
output.AddHideEmptyColumnsFlag(Cmd)
5959
}
6060

6161
// When no specific cluster id is provided by the user, this function lists all available AddOns
@@ -81,18 +81,34 @@ func listAllAddOns(r *rosa.Runtime) {
8181
os.Exit(0)
8282
}
8383

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")
84+
headers := []string{"ID", "NAME", "AVAILABILITY"}
85+
var tableData [][]string
8786
for _, addOnResource := range addOnResources {
8887
availability := "unavailable"
8988
if addOnResource.Available {
9089
availability = "available"
9190
}
92-
fmt.Fprintf(writer, "%s\t\t%s\t\t%s\n", addOnResource.AddOn.ID(), addOnResource.AddOn.Name(), availability)
91+
row := []string{
92+
addOnResource.AddOn.ID(),
93+
addOnResource.AddOn.Name(),
94+
availability,
95+
}
96+
tableData = append(tableData, row)
97+
}
98+
99+
if output.ShouldHideEmptyColumns() {
100+
tableData = output.RemoveEmptyColumns(headers, tableData)
101+
} else {
102+
tableData = append([][]string{headers}, tableData...)
93103
}
94-
writer.Flush()
95104

105+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
106+
output.BuildTable(writer, "\t\t", tableData)
107+
108+
if err := writer.Flush(); err != nil {
109+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
110+
os.Exit(1)
111+
}
96112
os.Exit(0)
97113
}
98114

@@ -142,13 +158,31 @@ func listClusterAddOns(clusterKey string, r *rosa.Runtime) {
142158
os.Exit(0)
143159
}
144160

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")
161+
headers := []string{"ID", "NAME", "STATE"}
162+
163+
var tableData [][]string
148164
for _, clusterAddOn := range clusterAddOns {
149-
fmt.Fprintf(writer, "%s\t\t%s\t\t%s\n", clusterAddOn.ID, clusterAddOn.Name, clusterAddOn.State)
165+
row := []string{
166+
clusterAddOn.ID,
167+
clusterAddOn.Name,
168+
clusterAddOn.State,
169+
}
170+
tableData = append(tableData, row)
171+
}
172+
173+
if output.ShouldHideEmptyColumns() {
174+
tableData = output.RemoveEmptyColumns(headers, tableData)
175+
} else {
176+
tableData = append([][]string{headers}, tableData...)
177+
}
178+
179+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
180+
output.BuildTable(writer, "\t\t", tableData)
181+
182+
if err := writer.Flush(); err != nil {
183+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
184+
os.Exit(1)
150185
}
151-
writer.Flush()
152186
}
153187

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

cmd/list/breakglasscredential/cmd.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var Cmd = &cobra.Command{
2727
func init() {
2828
ocm.AddClusterFlag(Cmd)
2929
output.AddFlag(Cmd)
30+
output.AddHideEmptyColumnsFlag(Cmd)
3031
}
3132

3233
func run(cmd *cobra.Command, _ []string) {
@@ -69,18 +70,28 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command) error {
6970
return nil
7071
}
7172

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")
73+
headers := []string{"ID", "USERNAME", "STATUS"}
74+
var tableData [][]string
7675
for _, credential := range breakGlassCredentials {
77-
fmt.Fprintf(writer, "%s\t%s\t%s\n",
76+
row := []string{
7877
credential.ID(),
7978
credential.Username(),
80-
credential.Status(),
81-
)
79+
string(credential.Status()),
80+
}
81+
tableData = append(tableData, row)
82+
}
83+
84+
if output.ShouldHideEmptyColumns() {
85+
tableData = output.RemoveEmptyColumns(headers, tableData)
86+
} else {
87+
tableData = append([][]string{headers}, tableData...)
8288
}
83-
writer.Flush()
8489

90+
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
91+
output.BuildTable(writer, "\t", tableData)
92+
93+
if err := writer.Flush(); err != nil {
94+
return err
95+
}
8596
return nil
8697
}

cmd/list/cluster/cmd.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package cluster
1818

1919
import (
20-
"fmt"
2120
"os"
2221
"text/tabwriter"
2322

@@ -52,6 +51,7 @@ func init() {
5251
flags.SortFlags = false
5352

5453
output.AddFlag(Cmd)
54+
output.AddHideEmptyColumnsFlag(Cmd)
5555
flags.BoolVarP(&args.listAll, "all", "a", false, "List all clusters across different AWS "+
5656
"accounts under the same Red Hat organization")
5757
flags.StringVar(&args.accountRoleArn, "account-role-arn", "", "List all clusters "+
@@ -107,25 +107,37 @@ func run(_ *cobra.Command, _ []string) {
107107
os.Exit(0)
108108
}
109109

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")
110+
headers := []string{"ID", "NAME", "STATE", "TOPOLOGY"}
111+
var tableData [][]string
113112
for _, cluster := range clusters {
114-
typeOutput := "Classic"
113+
typeOutput := ""
115114
if cluster.AWS() != nil && cluster.AWS().STS() != nil && cluster.AWS().STS().Enabled() {
116115
typeOutput = "Classic (STS)"
117116
}
118117
if cluster.Hypershift().Enabled() {
119118
typeOutput = "Hosted CP"
120119
}
121-
fmt.Fprintf(
122-
writer,
123-
"%s\t%s\t%s\t%s\n",
120+
121+
row := []string{
124122
cluster.ID(),
125123
cluster.Name(),
126-
cluster.State(),
124+
string(cluster.State()),
127125
typeOutput,
128-
)
126+
}
127+
tableData = append(tableData, row)
128+
}
129+
130+
if output.ShouldHideEmptyColumns() {
131+
tableData = output.RemoveEmptyColumns(headers, tableData)
132+
} else {
133+
tableData = append([][]string{headers}, tableData...)
134+
}
135+
136+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
137+
output.BuildTable(writer, "\t", tableData)
138+
139+
if err := writer.Flush(); err != nil {
140+
_ = r.Reporter.Errorf("Failed to flush output: %v", err)
141+
os.Exit(1)
129142
}
130-
writer.Flush()
131143
}

0 commit comments

Comments
 (0)