Skip to content

Commit ae55066

Browse files
Ensure command prints to stdout (#245)
1 parent 7742250 commit ae55066

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

cmd/pg-schema-diff/apply_cmd.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func buildApplyCmd() *cobra.Command {
5959
if err != nil {
6060
return err
6161
} else if len(plan.Statements) == 0 {
62-
cmd.Println("Schema matches expected. No plan generated")
62+
cmdPrintln(cmd, "Schema matches expected. No plan generated")
6363
return nil
6464
}
6565

66-
cmd.Println(header("Review plan"))
67-
cmd.Print(planToPrettyS(plan), "\n\n")
66+
cmdPrintln(cmd, header("Review plan"))
67+
cmdPrint(cmd, planToPrettyS(plan), "\n\n")
6868

6969
if err := failIfHazardsNotAllowed(plan, *allowedHazardsTypesStrs); err != nil {
7070
return err
@@ -84,7 +84,7 @@ func buildApplyCmd() *cobra.Command {
8484
if err := runPlan(cmd.Context(), cmd, connConfig, plan); err != nil {
8585
return err
8686
}
87-
cmd.Println("Schema applied successfully")
87+
cmdPrintln(cmd, "Schema applied successfully")
8888
return nil
8989
}
9090

@@ -143,8 +143,8 @@ func runPlan(ctx context.Context, cmd *cobra.Command, connConfig *pgx.ConnConfig
143143
// must be executed within its own transaction block. Postgres will error if you try to set a TRANSACTION-level
144144
// timeout for it. SESSION-level statement_timeouts are respected by `ADD INDEX CONCURRENTLY`
145145
for i, stmt := range plan.Statements {
146-
cmd.Println(header(fmt.Sprintf("Executing statement %d", getDisplayableStmtIdx(i))))
147-
cmd.Printf("%s\n\n", statementToPrettyS(stmt))
146+
cmdPrintln(cmd, header(fmt.Sprintf("Executing statement %d", getDisplayableStmtIdx(i))))
147+
cmdPrintf(cmd, "%s\n\n", statementToPrettyS(stmt))
148148
start := time.Now()
149149
if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION statement_timeout = %d", stmt.Timeout.Milliseconds())); err != nil {
150150
return fmt.Errorf("setting statement timeout: %w", err)
@@ -155,9 +155,9 @@ func runPlan(ctx context.Context, cmd *cobra.Command, connConfig *pgx.ConnConfig
155155
if _, err := conn.ExecContext(ctx, stmt.ToSQL()); err != nil {
156156
return fmt.Errorf("executing migration statement. the database maybe be in a dirty state: %s: %w", stmt, err)
157157
}
158-
cmd.Printf("Finished executing statement. Duration: %s\n", time.Since(start))
158+
cmdPrintf(cmd, "Finished executing statement. Duration: %s\n", time.Since(start))
159159
}
160-
cmd.Println(header("Complete"))
160+
cmdPrintln(cmd, header("Complete"))
161161

162162
return nil
163163
}

cmd/pg-schema-diff/cli.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/manifoldco/promptui"
9+
"github.com/spf13/cobra"
910
)
1011

1112
func header(header string) string {
@@ -44,3 +45,18 @@ func mustContinuePrompt(continueLabel string) error {
4445
}
4546
return nil
4647
}
48+
49+
// cmdPrint writes the arguments to stdout using cmd.OutOrStdout() to ensure consistent output behavior
50+
func cmdPrint(cmd *cobra.Command, a ...interface{}) {
51+
fmt.Fprint(cmd.OutOrStdout(), a...)
52+
}
53+
54+
// cmdPrintf writes the formatted string to stdout using cmd.OutOrStdout() to ensure consistent output behavior
55+
func cmdPrintf(cmd *cobra.Command, format string, a ...interface{}) {
56+
fmt.Fprintf(cmd.OutOrStdout(), format, a...)
57+
}
58+
59+
// cmdPrintln writes the arguments to stdout with a newline using cmd.OutOrStdout() to ensure consistent output behavior
60+
func cmdPrintln(cmd *cobra.Command, a ...interface{}) {
61+
fmt.Fprintln(cmd.OutOrStdout(), a...)
62+
}

cmd/pg-schema-diff/plan_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func buildPlanCmd() *cobra.Command {
9999
return err
100100
}
101101

102-
cmd.Println(outputFmt.convertToOutputString(plan))
102+
cmdPrintln(cmd, outputFmt.convertToOutputString(plan))
103103
return nil
104104
}
105105

cmd/pg-schema-diff/version_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func buildVersionCmd() *cobra.Command {
1717
if !ok {
1818
return fmt.Errorf("build information not available")
1919
}
20-
cmd.Printf("version=%s\n", buildInfo.Main.Version)
20+
cmdPrintf(cmd, "version=%s\n", buildInfo.Main.Version)
2121

2222
return nil
2323
}

0 commit comments

Comments
 (0)