Skip to content

Commit 5dbbc4c

Browse files
committed
allow intercepting command output for testing
1 parent 5293b2f commit 5dbbc4c

6 files changed

Lines changed: 17 additions & 17 deletions

File tree

cmd/api_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ var apiKeyCmd = &cobra.Command{
2424
}
2525

2626
if isJSONOutput() {
27-
return printJSON(result)
27+
return printJSON(cmd.OutOrStdout(), result)
2828
}
29-
fmt.Printf("Valid API key for team: %s\n", result.TeamName)
29+
fmt.Fprintf(cmd.OutOrStdout(), "Valid API key for team: %s\n", result.TeamName)
3030
return nil
3131
},
3232
}

cmd/auth_login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ var loginCmd = &cobra.Command{
3838
}
3939

4040
if isJSONOutput() {
41-
return printJSON(Result{Success: true, Message: fmt.Sprintf("Authenticated as team: %s", result.TeamName)})
41+
return printJSON(cmd.OutOrStdout(), Result{Success: true, Message: fmt.Sprintf("Authenticated as team: %s", result.TeamName)})
4242
}
43-
fmt.Printf("API key saved. Authenticated as team: %s\n", result.TeamName)
43+
fmt.Fprintf(cmd.OutOrStdout(), "API key saved. Authenticated as team: %s\n", result.TeamName)
4444
return nil
4545
},
4646
}

cmd/auth_logout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ var logoutCmd = &cobra.Command{
1515
return err
1616
}
1717
if isJSONOutput() {
18-
return printJSON(Result{Success: true})
18+
return printJSON(cmd.OutOrStdout(), Result{Success: true})
1919
}
20-
fmt.Println("Logged out.")
20+
fmt.Fprintln(cmd.OutOrStdout(), "Logged out.")
2121
return nil
2222
},
2323
}

cmd/auth_status.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ var statusCmd = &cobra.Command{
1717
}
1818

1919
if isJSONOutput() {
20-
return printJSON(cfg)
20+
return printJSON(cmd.OutOrStdout(), cfg)
2121
}
2222

23-
fmt.Printf("API Key: %s\n", cfg.APIKey)
24-
fmt.Printf("Endpoint: %s\n", cfg.EndpointURL)
23+
fmt.Fprintf(cmd.OutOrStdout(), "API Key: %s\n", cfg.APIKey)
24+
fmt.Fprintf(cmd.OutOrStdout(), "Endpoint: %s\n", cfg.EndpointURL)
2525
return nil
2626
},
2727
}

cmd/output.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6-
"os"
6+
"io"
77
)
88

99
type outputFlag string
@@ -30,8 +30,8 @@ func isJSONOutput() bool {
3030
return outputFormat == "json"
3131
}
3232

33-
func printJSON(v any) error {
34-
enc := json.NewEncoder(os.Stdout)
33+
func printJSON(w io.Writer, v any) error {
34+
enc := json.NewEncoder(w)
3535
enc.SetIndent("", " ")
3636
return enc.Encode(v)
3737
}

cmd/transactional.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ var transactionalListCmd = &cobra.Command{
7878
if emails == nil {
7979
emails = []api.TransactionalEmail{}
8080
}
81-
return printJSON(emails)
81+
return printJSON(cmd.OutOrStdout(), emails)
8282
}
8383

8484
if len(emails) == 0 {
85-
fmt.Println("No transactional emails found.")
85+
fmt.Fprintln(cmd.OutOrStdout(), "No transactional emails found.")
8686
return nil
8787
}
8888

89-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
89+
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
9090
fmt.Fprintln(w, "ID\tNAME\tLAST UPDATED\tVARIABLES")
9191
for _, e := range emails {
9292
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", e.ID, e.Name, e.LastUpdated, strings.Join(e.DataVariables, ", "))
@@ -141,9 +141,9 @@ var transactionalSendCmd = &cobra.Command{
141141
}
142142

143143
if isJSONOutput() {
144-
return printJSON(Result{Success: true})
144+
return printJSON(cmd.OutOrStdout(), Result{Success: true})
145145
}
146-
fmt.Println("Sent.")
146+
fmt.Fprintln(cmd.OutOrStdout(), "Sent.")
147147
return nil
148148
},
149149
}

0 commit comments

Comments
 (0)