@@ -3,6 +3,8 @@ package main
33import (
44 "context"
55 "database/sql"
6+ "encoding/json"
7+ "errors"
68 "fmt"
79 "io"
810 "regexp"
@@ -55,12 +57,9 @@ func buildPlanCmd() *cobra.Command {
5557 plan , err := generatePlan (context .Background (), logger , connConfig , planConfig )
5658 if err != nil {
5759 return err
58- } else if len (plan .Statements ) == 0 {
59- fmt .Println ("Schema matches expected. No plan generated" )
60- return nil
6160 }
62- fmt . Printf ( " \n %s \n " , header ( "Generated plan" ))
63- fmt .Println (planToPrettyS (plan ))
61+
62+ fmt .Println (planFlags . outputFormat . convertToOutputString (plan ))
6463 return nil
6564 }
6665
@@ -78,6 +77,11 @@ type (
7877 targetDatabaseDSN string
7978 }
8079
80+ outputFormat struct {
81+ identifier string
82+ convertToOutputString func (diff.Plan ) string
83+ }
84+
8185 planFlags struct {
8286 dbSchemaSourceFlags schemaSourceFlags
8387
8993 statementTimeoutModifiers []string
9094 lockTimeoutModifiers []string
9195 insertStatements []string
96+ outputFormat outputFormat
9297 }
9398
9499 timeoutModifier struct {
@@ -115,8 +120,35 @@ type (
115120 }
116121)
117122
123+ var (
124+ outputFormatPretty outputFormat = outputFormat {identifier : "pretty" , convertToOutputString : planToPrettyS }
125+ outputFormatJson outputFormat = outputFormat {identifier : "json" , convertToOutputString : planToJsonS }
126+ )
127+
128+ func (e * outputFormat ) String () string {
129+ return string (e .identifier )
130+ }
131+
132+ func (e * outputFormat ) Set (v string ) error {
133+ switch v {
134+ case "pretty" :
135+ * e = outputFormatPretty
136+ return nil
137+ case "json" :
138+ * e = outputFormatJson
139+ return nil
140+ default :
141+ return errors .New (`must be one of "pretty" or "json"` )
142+ }
143+ }
144+
145+ func (e * outputFormat ) Type () string {
146+ return "outputFormat"
147+ }
148+
118149func createPlanFlags (cmd * cobra.Command ) * planFlags {
119150 flags := & planFlags {}
151+ flags .outputFormat = outputFormatPretty
120152
121153 schemaSourceFlagsVar (cmd , & flags .dbSchemaSourceFlags )
122154
@@ -139,6 +171,8 @@ func createPlanFlags(cmd *cobra.Command) *planFlags {
139171 ),
140172 )
141173
174+ cmd .Flags ().Var (& flags .outputFormat , "output-format" , "Change the output format for what is printed. Defaults to pretty-printed human-readable output. (options: pretty, json)" )
175+
142176 return flags
143177}
144178
@@ -428,6 +462,13 @@ func applyPlanModifiers(
428462func planToPrettyS (plan diff.Plan ) string {
429463 sb := strings.Builder {}
430464
465+ if len (plan .Statements ) == 0 {
466+ sb .WriteString ("Schema matches expected. No plan generated" )
467+ return sb .String ()
468+ }
469+
470+ sb .WriteString (fmt .Sprintf ("%s\n " , header ("Generated plan" )))
471+
431472 // We are going to put a statement index before each statement. To do that,
432473 // we need to find how many characters are in the largest index, so we can provide the appropriate amount
433474 // of padding before the statements to align all of them
@@ -471,3 +512,11 @@ func hazardToPrettyS(hazard diff.MigrationHazard) string {
471512 return hazard .Type
472513 }
473514}
515+
516+ func planToJsonS (plan diff.Plan ) string {
517+ jsonData , err := json .MarshalIndent (plan , "" , " " )
518+ if err != nil {
519+ panic (err )
520+ }
521+ return string (jsonData )
522+ }
0 commit comments