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
Changes from 1 commit
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
22 changes: 16 additions & 6 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,12 @@ func NewApp() *cli.App {
app.Version = dbmate.Version

defaultDB := dbmate.New(nil)

app.Before = func(c *cli.Context) error {
loadDotEnv(c.String("env-file"))
return nil
}

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

// load environment variables from .env file
func loadDotEnv() {
if _, err := os.Stat(".env"); err != nil {
// load environment variables
func loadDotEnv(filename string) {
if _, err := os.Stat(filename); err != nil {
return
}

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