Skip to content

Commit

Permalink
no longer silently fail when running unconfigured (#17)
Browse files Browse the repository at this point in the history
- when a configuration error happens the process would exit without displaying that anything went wrong
- now the config lookup returns a Result object with more information
- fixes #16
- it's a bit messy now since there are two Result objects available in main
  • Loading branch information
tlhunter authored Aug 13, 2023
1 parent 2189b67 commit d11651a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 4 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"errors"
)
import "github.com/tlhunter/mig/result"

const (
DEF_MIG_DIR = "./migrations"
Expand All @@ -15,7 +13,7 @@ type MigConfig struct {
OutputJson bool // stdout should be valid JSON
}

func GetConfig() (MigConfig, []string, error) {
func GetConfig() (MigConfig, []string, *result.Response) {
config := MigConfig{}

flagConfig, subcommands, _ := GetConfigFromProcessFlags()
Expand All @@ -25,7 +23,7 @@ func GetConfig() (MigConfig, []string, error) {
err := SetEnvFromConfigFile(flagConfig.MigRcPath) // reads .env and sets env vars but does not override

if err != nil {
return config, []string{}, err
return config, []string{}, nil
}

envConfig, _ := GetConfigFromEnvVars()
Expand All @@ -35,7 +33,7 @@ func GetConfig() (MigConfig, []string, error) {
} else if envConfig.Connection != "" {
config.Connection = envConfig.Connection
} else {
return config, subcommands, errors.New("unable to determine server connection")
return config, subcommands, result.NewError("unable to determine server connection", "bad_config")
}

if flagConfig.Migrations != "" {
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
)

func main() {
cfg, subcommands, err := config.GetConfig()
cfg, subcommands, bail := config.GetConfig()

var res result.Response

if err != nil && len(subcommands) == 0 {
if bail != nil && len(subcommands) == 0 {
res.SetError("usage: mig <command>", "command_usage")
} else if err != nil && len(subcommands) == 1 && subcommands[0] == "version" {
} else if bail != nil && len(subcommands) == 1 && subcommands[0] == "version" {
res = commands.CommandVersion(cfg)
} else if err == nil {
} else if bail != nil {
res = *bail
} else if bail == nil {
res = commands.Dispatch(cfg, subcommands)
}

Expand Down

0 comments on commit d11651a

Please sign in to comment.