Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accepting env vars for config #128

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions sql-migrate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"

"github.com/kelseyhightower/envconfig"
"github.com/rubenv/sql-migrate"
"gopkg.in/gorp.v1"
"gopkg.in/yaml.v2"
Expand All @@ -32,30 +33,34 @@ func ConfigFlags(f *flag.FlagSet) {
}

type Environment struct {
Dialect string `yaml:"dialect"`
DataSource string `yaml:"datasource"`
Dir string `yaml:"dir"`
TableName string `yaml:"table"`
SchemaName string `yaml:"schema"`
Dialect string `yaml:"dialect" envconfig:"DIALECT"`
DataSource string `yaml:"datasource" envconfig:"DATASOURCE"`
Dir string `yaml:"dir" envconfig:"DIR"`
TableName string `yaml:"table" envconfig:"TABLE"`
SchemaName string `yaml:"schema" envconfig:"SCHEMA"`
}

func ReadConfig() (map[string]*Environment, error) {
file, err := ioutil.ReadFile(ConfigFile)
if err != nil {
func ConfigPresent() bool {
_, err := os.Stat(ConfigFile)
return !os.IsNotExist(err)
}

func ReadEnv() (*Environment, error) {
env := &Environment{}
if err := envconfig.Process("", env); err != nil {
return nil, err
}
return env, nil
}

config := make(map[string]*Environment)
err = yaml.Unmarshal(file, config)
func ReadEnvFromFile() (*Environment, error) {
file, err := ioutil.ReadFile(ConfigFile)
if err != nil {
return nil, err
}

return config, nil
}

func GetEnvironment() (*Environment, error) {
config, err := ReadConfig()
config := make(map[string]*Environment)
err = yaml.Unmarshal(file, config)
if err != nil {
return nil, err
}
Expand All @@ -65,6 +70,25 @@ func GetEnvironment() (*Environment, error) {
return nil, errors.New("No environment: " + ConfigEnvironment)
}

return env, nil
}

func GetEnvironment() (*Environment, error) {
var env *Environment
if ConfigPresent() {
var err error
env, err = ReadEnvFromFile()
if err != nil {
return nil, err
}
} else {
var err error
env, err = ReadEnv()
if err != nil {
return nil, err
}
}

if env.Dialect == "" {
return nil, errors.New("No dialect specified")
}
Expand Down