From 9ac5d7fe1e2aa496594ba06db98d06157167b3cd Mon Sep 17 00:00:00 2001 From: Jan Rada <31016933+ZelvaMan@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:33:37 +0100 Subject: [PATCH] allow connection string to be set using command line flag --- README.md | 2 ++ src/config.go | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d4dda56..20d0617 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ If `--config` flag is not set it will try following default locations Enable verbose logging with `--verbose` flag +ConnectionString can be also set with `--connectionString "postgresql://usernmae:password@host:port/database_name"` + ### Configuration overview - **ConnectionString (string)**: diff --git a/src/config.go b/src/config.go index 518e8f2..7985bfc 100644 --- a/src/config.go +++ b/src/config.go @@ -14,9 +14,10 @@ import ( var defaultConfigPaths = []string{"./db-gen.json", "./db-gen/db-gen.json", "./db-gen/config.json"} type cliArgs struct { - command Command - configPath string - verbose bool + command Command + configPath string + verbose bool + connectionString string } type Config struct { @@ -67,6 +68,7 @@ func GetCommand() Command { return args.command } +// in future migrate whole configuration to viper func GetConfig() (*Config, error) { args := parseCLIArgs() @@ -91,6 +93,11 @@ func GetConfig() (*Config, error) { // Cli args should override config loaded from file config.Verbose = args.verbose config.PathBase = filepath.Dir(args.configPath) + if args.connectionString != "" { + config.ConnectionString = args.connectionString + } + + VerboseLog(config.ConnectionString) //All paths are relative to config file config.ProcessorTemplate = joinIfRelative(config.PathBase, config.ProcessorTemplate) config.DbContextTemplate = joinIfRelative(config.PathBase, config.DbContextTemplate) @@ -136,12 +143,14 @@ func parseCLIArgs() *cliArgs { verboseFlag := flag.Bool("verbose", false, "If true it will print more stuff") configPathFlag := flag.String("config", "", "Path to config file, all paths are relative it") + connectionStringFlag := flag.String("connectionString", "", "Connection string used to connect to database (overrides value from config file)") flag.Parse() args = new(cliArgs) args.command = parseCommand(flag.Arg(0)) args.verbose = *verboseFlag args.configPath = *configPathFlag + args.connectionString = *connectionStringFlag //Necessary to allow verbose logging before configuration is parsed VerboseOverride = args.verbose