Skip to content

Commit ea2a9c4

Browse files
committed
add option to use stdout
1 parent 04e8cdf commit ea2a9c4

File tree

13 files changed

+162
-121
lines changed

13 files changed

+162
-121
lines changed

analyzer/analyzer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (a analyzer) GetTables(db database.Connector, selectedSchemas []string) ([]
123123
return util.Map2(selectedTables, func(value string) database.TableDetail {
124124
res, err := database.ParseTableName(value, selectedSchemas)
125125
if err != nil {
126-
logrus.Error("Could not parse table name", value)
126+
logrus.Error("Could not parse table name ", value)
127127
}
128128

129129
return res
@@ -165,7 +165,7 @@ func (a analyzer) GetTables(db database.Connector, selectedSchemas []string) ([]
165165
return util.Map2(surveyResult, func(value string) database.TableDetail {
166166
res, err := database.ParseTableName(value, selectedSchemas)
167167
if err != nil {
168-
logrus.Error("Could not parse table name", value)
168+
logrus.Error("Could not parse table name ", value)
169169
}
170170

171171
return res

cmd/root.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
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

99124
func bindFlagToViper(key string) {
@@ -102,7 +127,6 @@ func bindFlagToViper(key string) {
102127

103128
func 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()

config/config.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,47 @@ package config
33
import "github.com/spf13/viper"
44

55
const (
6-
ShowAllConstraintsKey = "showAllConstraints"
7-
UseAllTablesKey = "useAllTables"
8-
IgnoreTables = "ignoreTables"
9-
SelectedTablesKey = "selectedTables"
10-
SchemaKey = "schema"
116
ConnectionStringKey = "connectionString"
127
ConnectionStringSuggestionsKey = "connectionStringSuggestions"
13-
OutputFileNameKey = "outputFileName"
14-
EncloseWithMermaidBackticksKey = "encloseWithMermaidBackticks"
158
DebugKey = "debug"
16-
OmitConstraintLabelsKey = "omitConstraintLabels"
9+
EncloseWithMermaidBackticksKey = "encloseWithMermaidBackticks"
10+
IgnoreTables = "ignoreTables"
1711
OmitAttributeKeysKey = "omitAttributeKeys"
12+
OmitConstraintLabelsKey = "omitConstraintLabels"
13+
OutputFileNameKey = "outputFileName"
14+
OutputMode = "outputMode"
15+
RelationshipLabelsKey = "relationshipLabels"
16+
SchemaKey = "schema"
17+
SchemaPrefixSeparator = "schemaPrefixSeparator"
18+
SelectedTablesKey = "selectedTables"
19+
ShowAllConstraintsKey = "showAllConstraints"
1820
ShowDescriptionsKey = "showDescriptions"
19-
UseAllSchemasKey = "useAllSchemas"
2021
ShowSchemaPrefix = "showSchemaPrefix"
21-
SchemaPrefixSeparator = "schemaPrefixSeparator"
22-
RelationshipLabelsKey = "relationshipLabels"
22+
UseAllSchemasKey = "useAllSchemas"
23+
UseAllTablesKey = "useAllTables"
2324
)
2425

2526
type config struct{}
2627

2728
type MermerdConfig interface {
28-
ShowAllConstraints() bool
29-
UseAllTables() bool
30-
IgnoreTables() []string
31-
Schemas() []string
3229
ConnectionString() string
33-
OutputFileName() string
3430
ConnectionStringSuggestions() []string
35-
SelectedTables() []string
36-
EncloseWithMermaidBackticks() bool
3731
Debug() bool
38-
OmitConstraintLabels() bool
32+
EncloseWithMermaidBackticks() bool
33+
IgnoreTables() []string
3934
OmitAttributeKeys() bool
35+
OmitConstraintLabels() bool
36+
OutputFileName() string
37+
OutputMode() OutputModeType
38+
RelationshipLabels() []RelationshipLabel
39+
SchemaPrefixSeparator() string
40+
Schemas() []string
41+
SelectedTables() []string
42+
ShowAllConstraints() bool
4043
ShowDescriptions() []string
41-
UseAllSchemas() bool
4244
ShowSchemaPrefix() bool
43-
SchemaPrefixSeparator() string
44-
RelationshipLabels() []RelationshipLabel
45+
UseAllSchemas() bool
46+
UseAllTables() bool
4547
}
4648

4749
func NewConfig() MermerdConfig {
@@ -116,3 +118,7 @@ func (c config) ShowSchemaPrefix() bool {
116118
func (c config) SchemaPrefixSeparator() string {
117119
return viper.GetString(SchemaPrefixSeparator)
118120
}
121+
122+
func (c config) OutputMode() OutputModeType {
123+
return OutputModeType(viper.GetString(OutputMode))
124+
}

config/output_mode.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import "errors"
4+
5+
type OutputModeType string
6+
7+
const (
8+
File OutputModeType = "file"
9+
Stdout OutputModeType = "stdout"
10+
)
11+
12+
func (o *OutputModeType) String() string {
13+
return string(*o)
14+
}
15+
16+
func (o *OutputModeType) Set(v string) error {
17+
switch v {
18+
case "file", "stdout":
19+
*o = OutputModeType(v)
20+
return nil
21+
}
22+
23+
return errors.New(`must be one of "file" or "stdout"`)
24+
}
25+
26+
func (o *OutputModeType) Type() string {
27+
return "OutputModeType"
28+
}

diagram/diagram.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package diagram
22

33
import (
44
_ "embed"
5-
"os"
5+
"io"
66
"text/template"
77

88
"github.com/sirupsen/logrus"
@@ -19,22 +19,14 @@ type diagram struct {
1919
}
2020

2121
type Diagram interface {
22-
Create(result *database.Result) error
22+
Create(wr io.Writer, result *database.Result) error
2323
}
2424

2525
func NewDiagram(config config.MermerdConfig) Diagram {
2626
return diagram{config}
2727
}
2828

29-
func (d diagram) Create(result *database.Result) error {
30-
f, err := os.Create(d.config.OutputFileName())
31-
if err != nil {
32-
logrus.Error("Could not create output file", " | ", err)
33-
return err
34-
}
35-
36-
defer f.Close()
37-
29+
func (d diagram) Create(wr io.Writer, result *database.Result) error {
3830
tmpl, err := template.New("erd_template").Parse(erdTemplate)
3931
if err != nil {
4032
logrus.Error("Could not load template file", " | ", err)
@@ -74,7 +66,7 @@ func (d diagram) Create(result *database.Result) error {
7466
Constraints: constraints,
7567
}
7668

77-
if err = tmpl.Execute(f, diagramData); err != nil {
69+
if err = tmpl.Execute(wr, diagramData); err != nil {
7870
logrus.Error("Could not create diagram", " | ", err)
7971
return err
8072
}

exampleRunConfig.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ schema:
1010
# Define what tables should be used
1111
#useAllTables: true
1212
selectedTables:
13-
- article
14-
- label
13+
- public.article
14+
- public.label
1515

1616
# Additional flags
1717
showAllConstraints: true

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ module github.com/KarnerTh/mermerd
33
go 1.23
44

55
// https://github.com/microsoft/mssql-docker/issues/895#issuecomment-2327988940
6-
godebug (
7-
x509negativeserial=1
8-
)
6+
godebug x509negativeserial=1
97

108
require (
119
github.com/AlecAivazis/survey/v2 v2.3.7

0 commit comments

Comments
 (0)