Skip to content

Commit 1198fe5

Browse files
committed
Consolidate duplicate output helpers into output.go
Extract validateOutputFormat and isJSONOutput into output.go to eliminate code duplication between summary.go and metrics.go. This follows the existing pattern of shared output logic and ensures consistent validation across CI verbs.
1 parent d6be8fa commit 1198fe5

3 files changed

Lines changed: 22 additions & 29 deletions

File tree

pkg/cmd/ci/metrics.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewCmdMetrics() *cobra.Command {
3838
depot ci metrics --job job_123 --output json
3939
depot ci metrics --run run_123 --output json`,
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
if err := validateMetricsOutput(output); err != nil {
41+
if err := validateOutputFormat(output); err != nil {
4242
return err
4343
}
4444
if len(args) > 1 {
@@ -84,7 +84,7 @@ func NewCmdMetrics() *cobra.Command {
8484
}
8585
return fmt.Errorf("failed to get job metrics: %w", err)
8686
}
87-
if metricsOutputJSON(output) {
87+
if isJSONOutput(output) {
8888
return writeJSON(buildJobMetricsJSON(resp))
8989
}
9090
printJobMetrics(resp, orgFlag)
@@ -96,7 +96,7 @@ func NewCmdMetrics() *cobra.Command {
9696
}
9797
return fmt.Errorf("failed to get run metrics: %w", err)
9898
}
99-
if metricsOutputJSON(output) {
99+
if isJSONOutput(output) {
100100
return writeJSON(buildRunMetricsJSON(resp))
101101
}
102102
printRunMetrics(resp, orgFlag)
@@ -108,7 +108,7 @@ func NewCmdMetrics() *cobra.Command {
108108
}
109109
return fmt.Errorf("failed to get attempt metrics: %w", err)
110110
}
111-
if metricsOutputJSON(output) {
111+
if isJSONOutput(output) {
112112
return writeJSON(buildAttemptMetricsJSON(resp))
113113
}
114114
printAttemptMetrics(resp, orgFlag)
@@ -451,17 +451,6 @@ func connectErrorText(err error) string {
451451
return ""
452452
}
453453

454-
func validateMetricsOutput(output string) error {
455-
if output == "" || output == "text" || output == "json" {
456-
return nil
457-
}
458-
return fmt.Errorf("unsupported output %q (valid: text, json)", output)
459-
}
460-
461-
func metricsOutputJSON(output string) bool {
462-
return output == "json"
463-
}
464-
465454
func countNonEmpty(values ...string) int {
466455
count := 0
467456
for _, value := range values {

pkg/cmd/ci/output.go

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

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
)
78

@@ -12,3 +13,17 @@ func writeJSON(v any) error {
1213
enc.SetIndent("", " ")
1314
return enc.Encode(v)
1415
}
16+
17+
// validateOutputFormat validates that the output format is one of: "", "text", or "json".
18+
// Used by CI verbs that support --output flags.
19+
func validateOutputFormat(output string) error {
20+
if output == "" || output == "text" || output == "json" {
21+
return nil
22+
}
23+
return fmt.Errorf("unsupported output %q (valid: text, json)", output)
24+
}
25+
26+
// isJSONOutput returns true if the output format is "json".
27+
func isJSONOutput(output string) bool {
28+
return output == "json"
29+
}

pkg/cmd/ci/summary.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewCmdSummary() *cobra.Command {
3333
depot ci summary <job-id>
3434
depot ci summary <attempt-id> --output json`,
3535
RunE: func(cmd *cobra.Command, args []string) error {
36-
if err := validateSummaryOutput(output); err != nil {
36+
if err := validateOutputFormat(output); err != nil {
3737
return err
3838
}
3939
if len(args) == 0 {
@@ -60,7 +60,7 @@ func NewCmdSummary() *cobra.Command {
6060

6161
resp, attemptErr := ciGetJobAttemptSummary(ctx, tokenVal, orgID, id)
6262
if attemptErr == nil {
63-
if summaryOutputJSON(output) {
63+
if isJSONOutput(output) {
6464
return writeJSON(buildSummaryJSON(resp))
6565
}
6666
return printSummaryResponse(cmd.OutOrStdout(), resp)
@@ -82,7 +82,7 @@ func NewCmdSummary() *cobra.Command {
8282
return fmt.Errorf("failed to get job summary: %w", jobErr)
8383
}
8484

85-
if summaryOutputJSON(output) {
85+
if isJSONOutput(output) {
8686
return writeJSON(buildSummaryJSON(resp))
8787
}
8888

@@ -156,14 +156,3 @@ func emptySummaryMessage(resp *civ1.GetJobSummaryResponse) string {
156156
return "No CI step summary was produced."
157157
}
158158
}
159-
160-
func validateSummaryOutput(output string) error {
161-
if output == "" || output == "text" || output == "json" {
162-
return nil
163-
}
164-
return fmt.Errorf("unsupported output %q (valid: text, json)", output)
165-
}
166-
167-
func summaryOutputJSON(output string) bool {
168-
return output == "json"
169-
}

0 commit comments

Comments
 (0)