Skip to content

Commit

Permalink
allow connection string to be set using command line flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Dec 27, 2023
1 parent 42e5b77 commit 9ac5d7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**:
Expand Down
15 changes: 12 additions & 3 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,6 +68,7 @@ func GetCommand() Command {
return args.command
}

// in future migrate whole configuration to viper
func GetConfig() (*Config, error) {
args := parseCLIArgs()

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ac5d7f

Please sign in to comment.