-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Command and config improvements (#3)
* refactor commands and config to use cobra and viper * use git version number * show version in intro * updated readme * fix broken link in readme
- Loading branch information
Showing
16 changed files
with
384 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
showAllConstraints: true | ||
outputFileName: "my-db.mmd" | ||
|
||
# These connection strings are available as suggestions in the cli (use tab to access) | ||
connectionStringSuggestions: | ||
- postgresql://user:password@localhost:5432/yourDb | ||
- mysql://root:password@tcp(127.0.0.1:3306)/yourDb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"mermerd/analyzer" | ||
"mermerd/config" | ||
"mermerd/diagram" | ||
"mermerd/util" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var runConfig string | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "mermerd", | ||
Short: "Create Mermaid ERD diagrams from existing tables", | ||
Long: "Create Mermaid ERD diagrams from existing tables", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
util.ShowIntro() | ||
result, err := analyzer.Analyze() | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
util.ShowError() | ||
os.Exit(1) | ||
} | ||
|
||
err = diagram.Create(result) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
util.ShowError() | ||
os.Exit(1) | ||
} | ||
|
||
util.ShowSuccess() | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
rootCmd.Flags().StringVar(&runConfig, "runConfig", "", "run configuration (replaces global configuration)") | ||
rootCmd.Flags().Bool(config.ShowAllConstraintsKey, false, "show all constraints, even though the table of the resulting constraint was not selected") | ||
rootCmd.Flags().Bool(config.UseAllTablesKey, false, "use all available tables") | ||
rootCmd.Flags().StringP(config.ConnectionStringKey, "c", "", "connection string that should be used") | ||
rootCmd.Flags().StringP(config.SchemaKey, "s", "", "schema that should be used") | ||
rootCmd.Flags().StringP(config.OutputFileNameKey, "o", "result.mmd", "output file name") | ||
|
||
bindFlagToViper(config.ShowAllConstraintsKey) | ||
bindFlagToViper(config.UseAllTablesKey) | ||
bindFlagToViper(config.ConnectionStringKey) | ||
bindFlagToViper(config.SchemaKey) | ||
bindFlagToViper(config.OutputFileNameKey) | ||
} | ||
|
||
func bindFlagToViper(key string) { | ||
_ = viper.BindPFlag(key, rootCmd.Flags().Lookup(key)) | ||
} | ||
|
||
func initConfig() { | ||
if runConfig != "" { | ||
color.Blue(fmt.Sprintf("Using run configuration (from %s)", runConfig)) | ||
viper.SetConfigFile(runConfig) | ||
} else { | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
viper.AddConfigPath(home) | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(".mermerd") | ||
} | ||
|
||
err := viper.ReadInConfig() | ||
cobra.CheckErr(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number of mermerd", | ||
Long: "All software has versions. This is mermerd's", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("mermerd %s %s\n", viper.Get("version"), viper.Get("commit")) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
package config | ||
|
||
var ShowAllConstraints bool | ||
var Schema string | ||
var ConnectionString string | ||
var ConnectionStringSuggestions []string | ||
import "github.com/spf13/viper" | ||
|
||
const ( | ||
ShowAllConstraintsKey = "showAllConstraints" | ||
UseAllTablesKey = "useAllTables" | ||
SelectedTablesKey = "selectedTables" | ||
SchemaKey = "schema" | ||
ConnectionStringKey = "connectionString" | ||
ConnectionStringSuggestionsKey = "connectionStringSuggestions" | ||
OutputFileNameKey = "outputFileName" | ||
) | ||
|
||
func ShowAllConstraints() bool { | ||
return viper.GetBool(ShowAllConstraintsKey) | ||
} | ||
|
||
func UseAllTables() bool { | ||
return viper.GetBool(UseAllTablesKey) | ||
} | ||
|
||
func Schema() string { | ||
return viper.GetString(SchemaKey) | ||
} | ||
|
||
func ConnectionString() string { | ||
return viper.GetString(ConnectionStringKey) | ||
} | ||
|
||
func OutputFileName() string { | ||
return viper.GetString(OutputFileNameKey) | ||
} | ||
|
||
func ConnectionStringSuggestions() []string { | ||
return viper.GetStringSlice(ConnectionStringSuggestionsKey) | ||
} | ||
|
||
func SelectedTables() []string { | ||
return viper.GetStringSlice(SelectedTablesKey) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Connection properties | ||
connectionString: "postgresql://user:password@localhost:5432/dvdrental" | ||
schema: "public" | ||
|
||
# Define what tables should be used | ||
#useAllTables: true | ||
selectedTables: | ||
- city | ||
- customer | ||
|
||
# Additional flags | ||
showAllConstraints: true | ||
outputFileName: "my-db.mmd" |
Oops, something went wrong.