55 "io"
66 "os"
77
8- "github.com/fatih/color"
98 "github.com/sirupsen/logrus"
109 "github.com/spf13/cobra"
1110 "github.com/spf13/viper"
@@ -24,14 +23,18 @@ var rootCmd = &cobra.Command{
2423 Short : "Create Mermaid ERD diagrams from existing tables" ,
2524 Long : "Create Mermaid ERD diagrams from existing tables" ,
2625 Run : func (cmd * cobra.Command , args []string ) {
27- presentation .ShowIntro ()
28- config := config .NewConfig ()
26+ conf := config .NewConfig ()
27+ if runConfig != "" {
28+ presentation .ShowInfo (conf , fmt .Sprintf ("Using run configuration (from %s)" , runConfig ))
29+ }
30+
31+ presentation .ShowIntro (conf )
2932 connectorFactory := database .NewConnectorFactory ()
3033 questioner := analyzer .NewQuestioner ()
31- analyzer := analyzer .NewAnalyzer (config , connectorFactory , questioner )
32- diagram := diagram .NewDiagram (config )
34+ analyzer := analyzer .NewAnalyzer (conf , connectorFactory , questioner )
35+ diagram := diagram .NewDiagram (conf )
3336
34- if ! config .Debug () {
37+ if ! conf .Debug () {
3538 logrus .SetOutput (io .Discard )
3639 }
3740
@@ -42,14 +45,33 @@ var rootCmd = &cobra.Command{
4245 os .Exit (1 )
4346 }
4447
45- err = diagram .Create (result )
48+ var wr io.Writer
49+ if conf .OutputMode () == config .File {
50+ f , err := os .Create (conf .OutputFileName ())
51+ defer f .Close ()
52+ if err != nil {
53+ logrus .Error (err )
54+ presentation .ShowError ()
55+ os .Exit (1 )
56+ }
57+
58+ wr = f
59+ } else if conf .OutputMode () == config .Stdout {
60+ wr = os .Stdout
61+ } else {
62+ logrus .Errorf ("Output mode %s not suppported" , conf .OutputMode ())
63+ presentation .ShowError ()
64+ os .Exit (1 )
65+ }
66+
67+ err = diagram .Create (wr , result )
4668 if err != nil {
4769 logrus .Error (err )
4870 presentation .ShowError ()
4971 os .Exit (1 )
5072 }
5173
52- presentation .ShowSuccess (config .OutputFileName ())
74+ presentation .ShowSuccess (conf , conf .OutputFileName ())
5375 },
5476}
5577
@@ -76,24 +98,27 @@ func init() {
7698 rootCmd .Flags ().StringP (config .SchemaKey , "s" , "" , "schema that should be used" )
7799 rootCmd .Flags ().StringP (config .OutputFileNameKey , "o" , "result.mmd" , "output file name" )
78100 rootCmd .Flags ().String (config .SchemaPrefixSeparator , "." , "the separator that should be used between schema and table name" )
101+ var outputMode = config .File
102+ rootCmd .Flags ().Var (& outputMode , config .OutputMode , `output mode (file, stdout)` )
79103 rootCmd .Flags ().StringSlice (config .ShowDescriptionsKey , []string {"" }, "show 'notNull', 'enumValues' and/or 'columnComments' in the description column" )
80104 rootCmd .Flags ().StringSlice (config .SelectedTablesKey , []string {"" }, "tables to include" )
81105
82- bindFlagToViper (config .ShowAllConstraintsKey )
83- bindFlagToViper (config .UseAllTablesKey )
84- bindFlagToViper (config .IgnoreTables )
85- bindFlagToViper (config .UseAllSchemasKey )
106+ bindFlagToViper (config .ConnectionStringKey )
86107 bindFlagToViper (config .DebugKey )
87- bindFlagToViper (config .OmitConstraintLabelsKey )
88- bindFlagToViper (config .OmitAttributeKeysKey )
89108 bindFlagToViper (config .EncloseWithMermaidBackticksKey )
90- bindFlagToViper (config .ConnectionStringKey )
91- bindFlagToViper (config .SchemaKey )
109+ bindFlagToViper (config .IgnoreTables )
110+ bindFlagToViper (config .OmitAttributeKeysKey )
111+ bindFlagToViper (config .OmitConstraintLabelsKey )
92112 bindFlagToViper (config .OutputFileNameKey )
113+ bindFlagToViper (config .OutputMode )
114+ bindFlagToViper (config .SchemaKey )
115+ bindFlagToViper (config .SchemaPrefixSeparator )
93116 bindFlagToViper (config .SelectedTablesKey )
117+ bindFlagToViper (config .ShowAllConstraintsKey )
94118 bindFlagToViper (config .ShowDescriptionsKey )
95119 bindFlagToViper (config .ShowSchemaPrefix )
96- bindFlagToViper (config .SchemaPrefixSeparator )
120+ bindFlagToViper (config .UseAllSchemasKey )
121+ bindFlagToViper (config .UseAllTablesKey )
97122}
98123
99124func bindFlagToViper (key string ) {
@@ -102,7 +127,6 @@ func bindFlagToViper(key string) {
102127
103128func initConfig () {
104129 if runConfig != "" {
105- color .Blue (fmt .Sprintf ("Using run configuration (from %s)" , runConfig ))
106130 viper .SetConfigFile (runConfig )
107131 } else {
108132 home , err := os .UserHomeDir ()
0 commit comments