Skip to content

Commit dd91dc0

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

File tree

39 files changed

+688
-179
lines changed

39 files changed

+688
-179
lines changed

cmd/list/accessrequests/cmd.go

Lines changed: 44 additions & 25 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,6 +39,7 @@ func NewListAccessRequestsCommand() *cobra.Command {
3839
}
3940

4041
output.AddFlag(cmd)
42+
output.AddHideEmptyColumnsFlag(cmd)
4143
ocm.AddOptionalClusterFlag(cmd)
4244
return cmd
4345
}
@@ -67,40 +69,57 @@ func ListAccessRequestsRunner() rosa.CommandRunner {
6769
}
6870
return nil
6971
}
70-
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
71-
output, hasPending, pendingId := getAccessRequestsOutput(clusterId, accessRequests)
72-
fmt.Fprint(writer, output)
73-
if err := writer.Flush(); err != nil {
74-
return err
72+
73+
hasPending := false
74+
pendingId := "<ID>"
75+
for _, accessRequest := range accessRequests {
76+
if accessRequest.Status().State() == v1.AccessRequestStatePending ||
77+
accessRequest.Status().State() == v1.AccessRequestStateApproved {
78+
hasPending = true
79+
if clusterId != "" {
80+
pendingId = accessRequest.ID()
81+
}
82+
}
7583
}
84+
7685
if hasPending {
7786
r.Reporter.Infof("Run the following command to approve or deny the Access Request:\n\n"+
7887
" rosa create decision --access-request %s --decision Approved\n"+
7988
" rosa create decision --access-request %s --decision Denied --justification \"justification\"\n",
8089
pendingId, pendingId)
8190
}
82-
}
83-
return nil
84-
}
85-
}
8691

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()
92+
headers := []string{"STATE", "ID", "CLUSTER ID", "UPDATED AT"}
93+
var tableData [][]string
94+
for _, accessRequest := range accessRequests {
95+
row := []string{
96+
string(accessRequest.Status().State()),
97+
accessRequest.ID(),
98+
accessRequest.ClusterId(),
99+
accessRequest.UpdatedAt().UTC().Format(time.UnixDate),
100+
}
101+
tableData = append(tableData, row)
96102
}
103+
104+
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
105+
106+
if output.ShouldHideEmptyColumns() {
107+
newHeaders, newData := output.RemoveEmptyColumns(headers, tableData)
108+
config := output.TableConfig{
109+
Separator: "\t",
110+
HasTrailingSeparator: false,
111+
UseFprintln: false,
112+
}
113+
output.PrintTable(writer, newHeaders, newData, config)
114+
} else {
115+
fmt.Fprint(writer, "STATE\tID\tCLUSTER ID\tUPDATED AT\n")
116+
for _, row := range tableData {
117+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
118+
}
119+
}
120+
121+
writer.Flush()
97122
}
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))
123+
return nil
103124
}
104-
105-
return output, hasPending, id
106125
}

cmd/list/accessrequests/cmd_test.go

Lines changed: 8 additions & 10 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

@@ -18,23 +17,23 @@ import (
1817
)
1918

2019
var (
21-
accessRequestsOutput = `STATE ID CLUSTER ID UPDATED AT
22-
Approved mock-id-1 mock-cluster-id-1 Sat Sep 28 20:35:00 UTC 2024
23-
Pending mock-id-2 mock-cluster-id-2 Sat Sep 28 20:35:00 UTC 2024
24-
INFO: Run the following command to approve or deny the Access Request:
20+
accessRequestsOutput = `INFO: Run the following command to approve or deny the Access Request:
2521
2622
rosa create decision --access-request <ID> --decision Approved
2723
rosa create decision --access-request <ID> --decision Denied --justification "justification"
2824
25+
STATE ID CLUSTER ID UPDATED AT
26+
Approved mock-id-1 mock-cluster-id-1 Sat Sep 28 20:35:00 UTC 2024
27+
Pending mock-id-2 mock-cluster-id-2 Sat Sep 28 20:35:00 UTC 2024
2928
`
30-
clusterAccessRequestsOutput = `STATE ID CLUSTER ID UPDATED AT
31-
Pending mock-id-1 mock-cluster-id Sat Sep 28 21:35:00 UTC 2024
32-
Denied mock-id-2 mock-cluster-id Sat Sep 28 20:35:00 UTC 2024
33-
INFO: Run the following command to approve or deny the Access Request:
29+
clusterAccessRequestsOutput = `INFO: Run the following command to approve or deny the Access Request:
3430
3531
rosa create decision --access-request mock-id-1 --decision Approved
3632
rosa create decision --access-request mock-id-1 --decision Denied --justification "justification"
3733
34+
STATE ID CLUSTER ID UPDATED AT
35+
Pending mock-id-1 mock-cluster-id Sat Sep 28 21:35:00 UTC 2024
36+
Denied mock-id-2 mock-cluster-id Sat Sep 28 20:35:00 UTC 2024
3837
`
3938
)
4039

@@ -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: 25 additions & 7 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,39 @@ 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+
var tableData [][]string
114115
for _, accountRole := range accountRoles {
115116
awsManaged := "No"
116117
if accountRole.ManagedPolicy {
117118
awsManaged = "Yes"
118119
}
119-
fmt.Fprintf(
120-
writer,
121-
"%s\t%s\t%s\t%s\t%s\n",
120+
row := []string{
122121
accountRole.RoleName,
123122
accountRole.RoleType,
124123
accountRole.RoleARN,
125124
accountRole.Version,
126125
awsManaged,
127-
)
126+
}
127+
tableData = append(tableData, row)
128+
}
129+
130+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
131+
132+
if output.ShouldHideEmptyColumns() {
133+
newHeaders, newData := output.RemoveEmptyColumns(headers, tableData)
134+
config := output.TableConfig{
135+
Separator: "\t",
136+
HasTrailingSeparator: false,
137+
UseFprintln: false,
138+
}
139+
output.PrintTable(writer, newHeaders, newData, config)
140+
} else {
141+
fmt.Fprintf(writer, "ROLE NAME\tROLE TYPE\tROLE ARN\tOPENSHIFT VERSION\tAWS Managed\n")
142+
for _, row := range tableData {
143+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
144+
}
128145
}
129146
writer.Flush()
147+
130148
}

cmd/list/addon/cmd.go

Lines changed: 52 additions & 9 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,38 @@ 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)
9399
}
94-
writer.Flush()
95100

101+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
102+
103+
if output.ShouldHideEmptyColumns() {
104+
newHeaders, newData := output.RemoveEmptyColumns(headers, tableData)
105+
config := output.TableConfig{
106+
Separator: "\t\t",
107+
HasTrailingSeparator: false,
108+
UseFprintln: false,
109+
}
110+
output.PrintTable(writer, newHeaders, newData, config)
111+
} else {
112+
fmt.Fprintf(writer, "ID\t\tNAME\t\tAVAILABILITY\n")
113+
for _, row := range tableData {
114+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
115+
}
116+
}
117+
writer.Flush()
96118
os.Exit(0)
97119
}
98120

@@ -142,11 +164,32 @@ func listClusterAddOns(clusterKey string, r *rosa.Runtime) {
142164
os.Exit(0)
143165
}
144166

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")
167+
headers := []string{"ID", "NAME", "STATE"}
168+
var tableData [][]string
148169
for _, clusterAddOn := range clusterAddOns {
149-
fmt.Fprintf(writer, "%s\t\t%s\t\t%s\n", clusterAddOn.ID, clusterAddOn.Name, clusterAddOn.State)
170+
row := []string{
171+
clusterAddOn.ID,
172+
clusterAddOn.Name,
173+
clusterAddOn.State,
174+
}
175+
tableData = append(tableData, row)
176+
}
177+
178+
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
179+
180+
if output.ShouldHideEmptyColumns() {
181+
newHeaders, newData := output.RemoveEmptyColumns(headers, tableData)
182+
config := output.TableConfig{
183+
Separator: "\t\t",
184+
HasTrailingSeparator: false,
185+
UseFprintln: false,
186+
}
187+
output.PrintTable(writer, newHeaders, newData, config)
188+
} else {
189+
fmt.Fprintf(writer, "ID\t\tNAME\t\tSTATE\n")
190+
for _, row := range tableData {
191+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t\t"))
192+
}
150193
}
151194
writer.Flush()
152195
}

cmd/list/breakglasscredential/cmd.go

Lines changed: 26 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,34 @@ 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)
8283
}
83-
writer.Flush()
8484

85+
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
86+
87+
if output.ShouldHideEmptyColumns() {
88+
newHeaders, newData := output.RemoveEmptyColumns(headers, tableData)
89+
90+
config := output.TableConfig{
91+
Separator: "\t",
92+
HasTrailingSeparator: false,
93+
UseFprintln: false,
94+
}
95+
output.PrintTable(writer, newHeaders, newData, config)
96+
} else {
97+
fmt.Fprintf(writer, "ID\tUSERNAME\tSTATUS\n")
98+
for _, row := range tableData {
99+
fmt.Fprintf(writer, "%s\n", strings.Join(row, "\t"))
100+
}
101+
}
102+
writer.Flush()
85103
return nil
86104
}

0 commit comments

Comments
 (0)