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

support --env-file CLI option (#274) #491

Merged
merged 16 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The following options are available with all commands. You must use command line

- `--url, -u "protocol://host:port/dbname"` - specify the database url directly. _(env: `DATABASE_URL`)_
- `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from.
- `--env-file "./env"` - specify an alternate environment variables file to load.
dossy marked this conversation as resolved.
Show resolved Hide resolved
- `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `DBMATE_MIGRATIONS_DIR`)_
- `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_
- `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_
Expand Down
33 changes: 25 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
)

func main() {
loadDotEnv()

app := NewApp()
err := app.Run(os.Args)

Expand All @@ -37,6 +35,16 @@ func NewApp() *cli.App {
app.Version = dbmate.Version

defaultDB := dbmate.New(nil)

app.Before = func(c *cli.Context) error {
if err := loadDotEnv(c.String("env-file")); err != nil {
log.Fatalf(err.Error())
dossy marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return nil
}
dossy marked this conversation as resolved.
Show resolved Hide resolved

app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "url",
Expand All @@ -49,6 +57,11 @@ func NewApp() *cli.App {
Value: "DATABASE_URL",
Usage: "specify an environment variable containing the database URL",
},
&cli.StringFlag{
Name: "env-file",
Value: ".env",
Usage: "specify an env file containing DATABASE_URL",
},
dossy marked this conversation as resolved.
Show resolved Hide resolved
&cli.StringSliceFlag{
Name: "migrations-dir",
Aliases: []string{"d"},
Expand Down Expand Up @@ -211,15 +224,19 @@ func NewApp() *cli.App {
return app
}

// load environment variables from .env file
func loadDotEnv() {
if _, err := os.Stat(".env"); err != nil {
return
// load environment variables
func loadDotEnv(filenames ...string) error {
for _, filename := range filenames {
if _, err := os.Stat(filename); err != nil {
return nil
}
}
dossy marked this conversation as resolved.
Show resolved Hide resolved

if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %s", err.Error())
dossy marked this conversation as resolved.
Show resolved Hide resolved
if err := godotenv.Load(filenames...); err != nil {
return fmt.Errorf("Error loading env file: %s", err.Error())
}

return nil
}
dossy marked this conversation as resolved.
Show resolved Hide resolved

// action wraps a cli.ActionFunc with dbmate initialization logic
Expand Down