From f5290e5ca52da417904e9af6dedda586c9e66c1e Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sun, 22 Oct 2023 14:29:32 +0200 Subject: [PATCH 01/16] support --env-file CLI option (#274) --- main.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 83b85bf8..867c806e 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,6 @@ import ( ) func main() { - loadDotEnv() - app := NewApp() err := app.Run(os.Args) @@ -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", @@ -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"}, + Value: ".env", + Usage: "specify an env file containing DATABASE_URL", + }, &cli.StringSliceFlag{ Name: "migrations-dir", Aliases: []string{"d"}, @@ -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()) } } From e61bf86d7e54bbfe8b90c5ea6c4dfa0499795b06 Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sat, 18 Nov 2023 19:24:50 +0100 Subject: [PATCH 02/16] Passing errors when loading env from file, remove --env-file alias --- main.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 867c806e..e390e510 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,11 @@ func NewApp() *cli.App { defaultDB := dbmate.New(nil) app.Before = func(c *cli.Context) error { - loadDotEnv(c.String("env-file")) + if err := loadDotEnv(c.String("env-file")); err != nil { + log.Fatalf(err.Error()) + return err + } + return nil } @@ -54,10 +58,9 @@ func NewApp() *cli.App { Usage: "specify an environment variable containing the database URL", }, &cli.StringFlag{ - Name: "env-file", - Aliases: []string{"f"}, - Value: ".env", - Usage: "specify an env file containing DATABASE_URL", + Name: "env-file", + Value: ".env", + Usage: "specify an env file containing DATABASE_URL", }, &cli.StringSliceFlag{ Name: "migrations-dir", @@ -222,14 +225,18 @@ func NewApp() *cli.App { } // load environment variables -func loadDotEnv(filename string) { - if _, err := os.Stat(filename); err != nil { - return +func loadDotEnv(filenames ...string) error { + for _, filename := range filenames { + if _, err := os.Stat(filename); err != nil { + return err + } } - if err := godotenv.Load(filename); err != nil { - log.Fatalf("Error loading .env file: %s", err.Error()) + if err := godotenv.Load(filenames...); err != nil { + return err } + + return nil } // action wraps a cli.ActionFunc with dbmate initialization logic From 2694e370bacd72e376663988b7ffe5d2bae45b11 Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sat, 18 Nov 2023 19:29:56 +0100 Subject: [PATCH 03/16] Add --env-file flag documentation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 36fd2871..8d14106f 100644 --- a/README.md +++ b/README.md @@ -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. - `--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`)_ From 62d617e358d4f542b7c8a3d673efda7fb6b2e28c Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sat, 18 Nov 2023 19:50:06 +0100 Subject: [PATCH 04/16] format errors messages in loadDotEnv function --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index e390e510..d3aa1c89 100644 --- a/main.go +++ b/main.go @@ -228,12 +228,12 @@ func NewApp() *cli.App { func loadDotEnv(filenames ...string) error { for _, filename := range filenames { if _, err := os.Stat(filename); err != nil { - return err + return fmt.Errorf("Error loading %s\n%s", filename, err.Error()) } } if err := godotenv.Load(filenames...); err != nil { - return err + return fmt.Errorf("Error loading %s\n%s", filenames, err.Error()) } return nil From 437f23c997ee074392ac4ff2315d781c92a95bd0 Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sat, 18 Nov 2023 20:40:47 +0100 Subject: [PATCH 05/16] Fix returning error when not using .env, change error message --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d3aa1c89..a01ef0ef 100644 --- a/main.go +++ b/main.go @@ -228,12 +228,12 @@ func NewApp() *cli.App { func loadDotEnv(filenames ...string) error { for _, filename := range filenames { if _, err := os.Stat(filename); err != nil { - return fmt.Errorf("Error loading %s\n%s", filename, err.Error()) + return nil } } if err := godotenv.Load(filenames...); err != nil { - return fmt.Errorf("Error loading %s\n%s", filenames, err.Error()) + return fmt.Errorf("Error loading env file: %s", err.Error()) } return nil From 85814fc058677f646563f11ab185a373cbde2399 Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sun, 19 Nov 2023 12:47:45 +0100 Subject: [PATCH 06/16] renaming loadDotEnv to loadEnvFiles, change the logic of loadEnvFiles function, change cli to more accurate doc fix typo in readme --- README.md | 2 +- main.go | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8d14106f..fbd4e682 100644 --- a/README.md +++ b/README.md @@ -138,7 +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. +- `--env-file ".env"` - specify an alternate environment variables file to load. - `--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`)_ diff --git a/main.go b/main.go index a01ef0ef..274d9726 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( + "errors" "fmt" - "log" "net/url" "os" "regexp" @@ -37,12 +37,7 @@ func NewApp() *cli.App { defaultDB := dbmate.New(nil) app.Before = func(c *cli.Context) error { - if err := loadDotEnv(c.String("env-file")); err != nil { - log.Fatalf(err.Error()) - return err - } - - return nil + return loadEnvFiles(c.String("env-file")) } app.Flags = []cli.Flag{ @@ -60,7 +55,7 @@ func NewApp() *cli.App { &cli.StringFlag{ Name: "env-file", Value: ".env", - Usage: "specify an env file containing DATABASE_URL", + Usage: "specify a file to load environment variables from", }, &cli.StringSliceFlag{ Name: "migrations-dir", @@ -224,19 +219,20 @@ func NewApp() *cli.App { return app } -// load environment variables -func loadDotEnv(filenames ...string) error { - for _, filename := range filenames { - if _, err := os.Stat(filename); err != nil { - return nil - } +// load environment variables from file(s) +func loadEnvFiles(filenames ...string) error { + err := godotenv.Load(filenames...) + if err == nil { + return nil } - if err := godotenv.Load(filenames...); err != nil { - return fmt.Errorf("Error loading env file: %s", err.Error()) + var perr *os.PathError + if errors.As(err, &perr) && errors.Is(perr, os.ErrNotExist) { + // Ignoring file not found error + return nil } - return nil + return fmt.Errorf("loading env file(s) %v: %v", filenames, err) } // action wraps a cli.ActionFunc with dbmate initialization logic From 505a4ca2e52d94cca46e22d6b49037853eacdf55 Mon Sep 17 00:00:00 2001 From: Hamza Khuswan Date: Sun, 19 Nov 2023 14:21:36 +0100 Subject: [PATCH 07/16] support env-file flag to accept multiple files --- README.md | 2 +- main.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fbd4e682..540ccc22 100644 --- a/README.md +++ b/README.md @@ -138,7 +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. +- `--env-file ".env"` - specify an alternate environment variables file(s) to load. - `--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`)_ diff --git a/main.go b/main.go index 274d9726..51870a5a 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func NewApp() *cli.App { defaultDB := dbmate.New(nil) app.Before = func(c *cli.Context) error { - return loadEnvFiles(c.String("env-file")) + return loadEnvFiles(c.StringSlice("env-file")...) } app.Flags = []cli.Flag{ @@ -52,9 +52,9 @@ func NewApp() *cli.App { Value: "DATABASE_URL", Usage: "specify an environment variable containing the database URL", }, - &cli.StringFlag{ + &cli.StringSliceFlag{ Name: "env-file", - Value: ".env", + Value: cli.NewStringSlice(".env"), Usage: "specify a file to load environment variables from", }, &cli.StringSliceFlag{ From e2618f8cf9514ae19d5f8e7061d8b177a6872d1c Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 12:14:47 -0500 Subject: [PATCH 08/16] Call loadEnvFiles earlier so that cli.App can see the values. --- main.go | 56 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 51870a5a..160123b3 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,14 @@ import ( ) func main() { + err := loadEnvFiles(os.Args[1:]) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(3) + } + app := NewApp() - err := app.Run(os.Args) + err = app.Run(os.Args) if err != nil { errText := redactLogString(fmt.Sprintf("Error: %s\n", err)) @@ -36,10 +42,6 @@ func NewApp() *cli.App { defaultDB := dbmate.New(nil) - app.Before = func(c *cli.Context) error { - return loadEnvFiles(c.StringSlice("env-file")...) - } - app.Flags = []cli.Flag{ &cli.StringFlag{ Name: "url", @@ -220,19 +222,45 @@ func NewApp() *cli.App { } // load environment variables from file(s) -func loadEnvFiles(filenames ...string) error { - err := godotenv.Load(filenames...) - if err == nil { - return nil +func loadEnvFiles(args []string) error { + var envFiles []string + + for i := 0; i < len(args); i++ { + if args[i] == "--env-file" { + if i+1 >= len(args) { + // returning nil here, even though it's an error + // because we want the caller to proceed anyway, + // and produce the actual arg parsing error response + return nil + } + + envFiles = append(envFiles, args[i+1]) + i++ + } + } + + if len(envFiles) == 0 { + envFiles = []string{".env"} } - var perr *os.PathError - if errors.As(err, &perr) && errors.Is(perr, os.ErrNotExist) { - // Ignoring file not found error - return nil + // try to load all files in sequential order, + // ignoring any that do not exist + for _, file := range envFiles { + err := godotenv.Load([]string{file}...) + if err == nil { + continue + } + + var perr *os.PathError + if errors.As(err, &perr) && errors.Is(perr, os.ErrNotExist) { + // Ignoring file not found error + continue + } + + return fmt.Errorf("loading env file(s) %v: %v", envFiles, err) } - return fmt.Errorf("loading env file(s) %v: %v", filenames, err) + return nil } // action wraps a cli.ActionFunc with dbmate initialization logic From 3e846d3fd9026f0ef5699a4ff9531823aa6c75aa Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 12:58:27 -0500 Subject: [PATCH 09/16] Make appending to FLAGS possible when invoking make. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6d5a3268..84a6e310 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ export CGO_ENABLED = 1 # strip binaries -FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' +override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) # default output file OUTPUT ?= dbmate @@ -11,7 +11,7 @@ OUTPUT ?= dbmate GOOS := $(shell go env GOOS) ifeq ($(GOOS),linux) # statically link binaries to support alpine linux - FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' + override FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' $(FLAGS) endif ifeq ($(GOOS),darwin) export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path) From 29e3bcb0e2061f8fc2f52076bc06e706bd10f5c6 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:09:25 -0500 Subject: [PATCH 10/16] Add tests to document loadEnvFiles implementation. --- main_test.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/main_test.go b/main_test.go index df1cab52..ddaa8b99 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "flag" "os" + "strings" "testing" "github.com/stretchr/testify/require" @@ -58,3 +59,114 @@ func TestRedactLogString(t *testing.T) { require.Equal(t, ex.expected, redactLogString(ex.in)) } } + +func TestLoadEnvFiles(t *testing.T) { + setup := func(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + + env := os.Environ() + + os.Clearenv() + os.Chdir("fixtures/loadEnvFiles") + + t.Cleanup(func() { + os.Chdir(cwd) + os.Clearenv() + for _, e := range env { + pair := strings.SplitN(e, "=", 2) + os.Setenv(pair[0], pair[1]) + } + }) + } + + t.Run("default file is .env", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{}) + require.NoError(t, err) + + require.Equal(t, 1, len(os.Environ())) + require.Equal(t, "default", os.Getenv("TEST_DOTENV")) + }) + + t.Run("valid file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "first.txt"}) + require.NoError(t, err) + require.Equal(t, 1, len(os.Environ())) + require.Equal(t, "one", os.Getenv("FIRST")) + }) + + t.Run("two valid files", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "second.txt"}) + require.NoError(t, err) + require.Equal(t, 2, len(os.Environ())) + require.Equal(t, "one", os.Getenv("FIRST")) + require.Equal(t, "two", os.Getenv("SECOND")) + }) + + t.Run("nonexistent file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "nonexistent.txt"}) + require.NoError(t, err) + require.Equal(t, 0, len(os.Environ())) + }) + + t.Run("no overload", func(t *testing.T) { + setup(t) + + // we do not load values over existing values + os.Setenv("FIRST", "not one") + + err := loadEnvFiles([]string{"--env-file", "first.txt"}) + require.NoError(t, err) + require.Equal(t, 1, len(os.Environ())) + require.Equal(t, "not one", os.Getenv("FIRST")) + }) + + t.Run("invalid file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "invalid.txt"}) + require.Error(t, err) + require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"") + require.Equal(t, 0, len(os.Environ())) + }) + + t.Run("invalid file followed by a valid file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "invalid.txt", "--env-file", "first.txt"}) + require.Error(t, err) + require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"") + require.Equal(t, 0, len(os.Environ())) + }) + + t.Run("valid file followed by an invalid file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "invalid.txt"}) + require.Error(t, err) + require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"") + require.Equal(t, 1, len(os.Environ())) + require.Equal(t, "one", os.Getenv("FIRST")) + }) + + t.Run("valid file followed by an invalid file followed by a valid file", func(t *testing.T) { + setup(t) + + err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "invalid.txt", "--env-file", "second.txt"}) + require.Error(t, err) + require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"") + // files after an invalid file should not get loaded + require.Equal(t, 1, len(os.Environ())) + require.Equal(t, "one", os.Getenv("FIRST")) + }) +} From 29246883e568ca0ae678d91148924d293cdcb6a6 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:12:08 -0500 Subject: [PATCH 11/16] Add test fixture files. --- fixtures/loadEnvFiles/first.txt | 1 + fixtures/loadEnvFiles/invalid.txt | 1 + fixtures/loadEnvFiles/second.txt | 1 + 3 files changed, 3 insertions(+) create mode 100644 fixtures/loadEnvFiles/first.txt create mode 100644 fixtures/loadEnvFiles/invalid.txt create mode 100644 fixtures/loadEnvFiles/second.txt diff --git a/fixtures/loadEnvFiles/first.txt b/fixtures/loadEnvFiles/first.txt new file mode 100644 index 00000000..71c5e94e --- /dev/null +++ b/fixtures/loadEnvFiles/first.txt @@ -0,0 +1 @@ +FIRST=one diff --git a/fixtures/loadEnvFiles/invalid.txt b/fixtures/loadEnvFiles/invalid.txt new file mode 100644 index 00000000..7524c931 --- /dev/null +++ b/fixtures/loadEnvFiles/invalid.txt @@ -0,0 +1 @@ +INVALID ENV FILE diff --git a/fixtures/loadEnvFiles/second.txt b/fixtures/loadEnvFiles/second.txt new file mode 100644 index 00000000..0e1c837b --- /dev/null +++ b/fixtures/loadEnvFiles/second.txt @@ -0,0 +1 @@ +SECOND=two From 0d429f03d7d58cd4e6c3116ffeb0a6f35d8f4837 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:25:58 -0500 Subject: [PATCH 12/16] Fix setting Makefile FLAGS change error. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 84a6e310..a27e78ed 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,6 @@ # enable cgo to build sqlite export CGO_ENABLED = 1 -# strip binaries -override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) - # default output file OUTPUT ?= dbmate @@ -12,6 +9,9 @@ GOOS := $(shell go env GOOS) ifeq ($(GOOS),linux) # statically link binaries to support alpine linux override FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' $(FLAGS) +else + # strip binaries + override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) endif ifeq ($(GOOS),darwin) export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path) From 1aa066590fedd2727489924f48f0ae41644e7358 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:30:46 -0500 Subject: [PATCH 13/16] Revert "Fix setting Makefile FLAGS change error." This reverts commit 0d429f03d7d58cd4e6c3116ffeb0a6f35d8f4837. Moving this to its own separate PR. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a27e78ed..84a6e310 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ # enable cgo to build sqlite export CGO_ENABLED = 1 +# strip binaries +override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) + # default output file OUTPUT ?= dbmate @@ -9,9 +12,6 @@ GOOS := $(shell go env GOOS) ifeq ($(GOOS),linux) # statically link binaries to support alpine linux override FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' $(FLAGS) -else - # strip binaries - override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) endif ifeq ($(GOOS),darwin) export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path) From 9527c881f8a261932d9b81708463202c7db69c50 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:31:13 -0500 Subject: [PATCH 14/16] Revert "Make appending to FLAGS possible when invoking make." This reverts commit 3e846d3fd9026f0ef5699a4ff9531823aa6c75aa. Moving this to its own separate PR. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 84a6e310..6d5a3268 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ export CGO_ENABLED = 1 # strip binaries -override FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' $(FLAGS) +FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s' # default output file OUTPUT ?= dbmate @@ -11,7 +11,7 @@ OUTPUT ?= dbmate GOOS := $(shell go env GOOS) ifeq ($(GOOS),linux) # statically link binaries to support alpine linux - override FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' $(FLAGS) + FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"' endif ifeq ($(GOOS),darwin) export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path) From ff3c8f3d298b6bff3a03946806d9fe7f5b4c037f Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 13:34:39 -0500 Subject: [PATCH 15/16] Add error checking to satisfy linter. --- main_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index ddaa8b99..5d184978 100644 --- a/main_test.go +++ b/main_test.go @@ -68,13 +68,21 @@ func TestLoadEnvFiles(t *testing.T) { } env := os.Environ() - os.Clearenv() - os.Chdir("fixtures/loadEnvFiles") + + err = os.Chdir("fixtures/loadEnvFiles") + if err != nil { + t.Fatal(err) + } t.Cleanup(func() { - os.Chdir(cwd) + err := os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } + os.Clearenv() + for _, e := range env { pair := strings.SplitN(e, "=", 2) os.Setenv(pair[0], pair[1]) From 857ffed1f3332b596dacfcb21e38c352c45434c5 Mon Sep 17 00:00:00 2001 From: Dossy Shiobara Date: Sun, 19 Nov 2023 14:34:03 -0500 Subject: [PATCH 16/16] Override top-level gitignore, so we can check in .env test fixture file. --- fixtures/loadEnvFiles/.env | 1 + fixtures/loadEnvFiles/.gitignore | 1 + 2 files changed, 2 insertions(+) create mode 100644 fixtures/loadEnvFiles/.env create mode 100644 fixtures/loadEnvFiles/.gitignore diff --git a/fixtures/loadEnvFiles/.env b/fixtures/loadEnvFiles/.env new file mode 100644 index 00000000..11b4e5ca --- /dev/null +++ b/fixtures/loadEnvFiles/.env @@ -0,0 +1 @@ +TEST_DOTENV=default diff --git a/fixtures/loadEnvFiles/.gitignore b/fixtures/loadEnvFiles/.gitignore new file mode 100644 index 00000000..8e0776e8 --- /dev/null +++ b/fixtures/loadEnvFiles/.gitignore @@ -0,0 +1 @@ +!.env