From fce989c8d14c35e550f58639b2e56f013f7b253e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 6 Jan 2024 09:26:41 -0500 Subject: Add support for environment variables --- internal/commands/create.go | 6 +- internal/commands/list.go | 6 +- internal/commands/migrate.go | 6 +- internal/config/config.go | 82 -------------------------- internal/configloader/configloader.go | 107 ++++++++++++++++++++++++++++++++++ internal/db/db.go | 4 +- internal/repositories/migration.go | 26 ++++----- 7 files changed, 131 insertions(+), 106 deletions(-) delete mode 100644 internal/config/config.go create mode 100644 internal/configloader/configloader.go (limited to 'internal') diff --git a/internal/commands/create.go b/internal/commands/create.go index 5176648..169bc4d 100644 --- a/internal/commands/create.go +++ b/internal/commands/create.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/db" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" @@ -48,13 +48,13 @@ func (cmd *CreateCmd) Execute( return subcommands.ExitFailure } - c, err := config.NewConfig() + cfg, err := configloader.NewConfig() if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure } - migrationRepo, err := repositories.NewMigrationRepo(conn, c) + migrationRepo, err := repositories.NewMigrationRepo(conn, cfg) if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure diff --git a/internal/commands/list.go b/internal/commands/list.go index ba7958d..36600c9 100644 --- a/internal/commands/list.go +++ b/internal/commands/list.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/db" "github.com/eugenetriguba/bolt/internal/repositories" "github.com/google/subcommands" @@ -32,13 +32,13 @@ func (m *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) } defer db.Close() - c, err := config.NewConfig() + cfg, err := configloader.NewConfig() if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure } - migrationRepo, err := repositories.NewMigrationRepo(db, c) + migrationRepo, err := repositories.NewMigrationRepo(db, cfg) if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure diff --git a/internal/commands/migrate.go b/internal/commands/migrate.go index edcfc81..f3e4cbd 100644 --- a/internal/commands/migrate.go +++ b/internal/commands/migrate.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/db" "github.com/eugenetriguba/bolt/internal/repositories" "github.com/google/subcommands" @@ -32,13 +32,13 @@ func (m *MigrateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{ } defer db.Close() - c, err := config.NewConfig() + cfg, err := configloader.NewConfig() if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure } - migrationRepo, err := repositories.NewMigrationRepo(db, c) + migrationRepo, err := repositories.NewMigrationRepo(db, cfg) if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitFailure diff --git a/internal/config/config.go b/internal/config/config.go deleted file mode 100644 index 89acd23..0000000 --- a/internal/config/config.go +++ /dev/null @@ -1,82 +0,0 @@ -package config - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/BurntSushi/toml" -) - -const configFileName = "bolt.toml" - -var config *Config - -type Config struct { - // Relative file path from the bolt configuration file. - // This is optional and will default to "migrations" if - // not specified. - MigrationsDir string `toml:"migrations_dir"` - - // Information related to how to connect to the database - // that is desired to run migrations against. - Connection connectionConfig -} - -type connectionConfig struct { - Host string - Port int - User string - Password string - DBName string - Driver string -} - -func NewConfig() (*Config, error) { - if config == nil { - filePath, err := findConfigFilePath() - if err != nil { - return nil, err - } - - config = &Config{} - _, err = toml.DecodeFile(filePath, &config) - if err != nil { - return nil, err - } - } - - return config, nil -} - -func findConfigFilePath() (filePath string, err error) { - dir, err := os.Getwd() - if err != nil { - return "", err - } - - for { - entries, err := os.ReadDir(dir) - if err != nil { - return "", err - } - - for _, e := range entries { - if e.Name() == configFileName { - return filepath.Join(dir, e.Name()), nil - } - } - - // TODO: This won't support Windows. - if dir == "/" { - errMsg := fmt.Sprintf( - "%s not found in current directory or any parent directories.", - configFileName, - ) - return "", errors.New(errMsg) - } - - dir = filepath.Dir(dir) - } -} diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go new file mode 100644 index 0000000..4ef542d --- /dev/null +++ b/internal/configloader/configloader.go @@ -0,0 +1,107 @@ +// Package configloader implements a Config type for loading in the +// configuration to be used by the app. +// +// It supports reading in configuration from the "bolt.toml" file +// if it can be found in the current directory or any parent directory +// and it supports reading in environment variables. Furthermore, if +// both a configuration file is found and environment variables are set, +// the environment variables will take precedence. +package configloader + +import ( + "errors" + "os" + "path/filepath" + "runtime" + + "github.com/BurntSushi/toml" + "github.com/kelseyhightower/envconfig" +) + +var errConfigFileNotFound = errors.New( + "bolt configuration file not found in current directory or any parent directories", +) + +// Config represents the application configuration settings. +// +// This can come from the TOML file or environment variables, +// with environment variables taking precedence. +type Config struct { + // Relative file path from the bolt configuration file. + // This is optional and will default to "migrations" if + // not specified. + MigrationsDir string `toml:"migrations_dir" envconfig:"BOLT_MIGRATIONS_DIR"` + + // Information related to how to connect to the database + // that is desired to run migrations against. + Connection ConnectionConfig `toml:"connection"` +} + +type ConnectionConfig struct { + Host string `toml:"host" envconfig:"BOLT_CONNECTION_HOST"` + Port int `toml:"port" envconfig:"BOLT_CONNECTION_PORT"` + User string `toml:"user" envconfig:"BOLT_CONNECTION_USER"` + Password string `toml:"password" envconfig:"BOLT_CONNECTION_PASSWORD"` + DBName string `toml:"dbname" envconfig:"BOLT_CONNECTION_DBNAME"` + Driver string `toml:"driver" envconfig:"BOLT_CONNECTION_DRIVER"` +} + +func NewConfig() (*Config, error) { + filePath, err := findConfigFilePath() + if err != nil && !errors.Is(err, errConfigFileNotFound) { + return nil, err + } + + cfg := &Config{} + _, err = toml.DecodeFile(filePath, &cfg) + if err != nil { + return nil, err + } + + err = envconfig.Process("", &cfg) + if err != nil { + return nil, err + } + + if cfg.MigrationsDir == "" { + cfg.MigrationsDir = "migrations" + } + + return cfg, nil +} + +func findConfigFilePath() (filePath string, err error) { + const configFileName = "bolt.toml" + var rootDir = fsRootDir() + + dir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + entries, err := os.ReadDir(dir) + if err != nil { + return "", err + } + + for _, e := range entries { + if e.Name() == configFileName { + return filepath.Join(dir, e.Name()), nil + } + } + + if dir == rootDir { + return "", errConfigFileNotFound + } + + dir = filepath.Dir(dir) + } +} + +func fsRootDir() string { + if runtime.GOOS == "windows" { + return os.Getenv("SystemDrive") + } + return "/" +} diff --git a/internal/db/db.go b/internal/db/db.go index df5934c..3927681 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -4,7 +4,7 @@ import ( "database/sql" "fmt" - "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/configloader" _ "github.com/lib/pq" ) @@ -17,7 +17,7 @@ var db *sql.DB // handle. func Connect() (*sql.DB, error) { if db == nil { - config, err := config.NewConfig() + config, err := configloader.NewConfig() if err != nil { return nil, err } diff --git a/internal/repositories/migration.go b/internal/repositories/migration.go index dccf8b6..e7d1f73 100644 --- a/internal/repositories/migration.go +++ b/internal/repositories/migration.go @@ -9,15 +9,15 @@ import ( "sort" "strings" - "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" ) -type IsNotDirError struct { +type ErrIsNotDir struct { path string } -func (e *IsNotDirError) Error() string { +func (e *ErrIsNotDir) Error() string { return fmt.Sprintf( "The specified migrations directory path '%s' is not a directory.", e.path, @@ -28,15 +28,15 @@ type MigrationRepo struct { // The database connection that will be used // to look up migrations that have already been // applied. - db *sql.DB - config *config.Config + db *sql.DB + cfg *configloader.Config } // Create a new Migration Repo. // // This handles the interactions with applying // and reverting migrations. -func NewMigrationRepo(db *sql.DB, c *config.Config) (*MigrationRepo, error) { +func NewMigrationRepo(db *sql.DB, cfg *configloader.Config) (*MigrationRepo, error) { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS bolt_migrations( version CHARACTER(14) PRIMARY KEY NOT NULL @@ -46,23 +46,23 @@ func NewMigrationRepo(db *sql.DB, c *config.Config) (*MigrationRepo, error) { return nil, err } - fileInfo, err := os.Stat(c.MigrationsDir) + fileInfo, err := os.Stat(cfg.MigrationsDir) if errors.Is(err, os.ErrNotExist) { - err = os.MkdirAll(c.MigrationsDir, 0755) + err = os.MkdirAll(cfg.MigrationsDir, 0755) if err != nil { return nil, err } } else if err != nil { return nil, err } else if err == nil && !fileInfo.IsDir() { - return nil, &IsNotDirError{path: c.MigrationsDir} + return nil, &ErrIsNotDir{path: cfg.MigrationsDir} } - return &MigrationRepo{db: db, config: c}, nil + return &MigrationRepo{db: db, cfg: cfg}, nil } func (mr *MigrationRepo) Create(m *models.Migration) error { - path := filepath.Join(mr.config.MigrationsDir, m.Dirname()) + path := filepath.Join(mr.cfg.MigrationsDir, m.Dirname()) err := os.Mkdir(path, 0755) if err != nil { return err @@ -98,7 +98,7 @@ func (mr *MigrationRepo) List() ([]*models.Migration, error) { } } - entries, err := os.ReadDir(mr.config.MigrationsDir) + entries, err := os.ReadDir(mr.cfg.MigrationsDir) if err != nil { return nil, err } @@ -147,7 +147,7 @@ func (mr *MigrationRepo) Apply(migration *models.Migration) error { defer tx.Rollback() upgradeScriptPath := filepath.Join( - mr.config.MigrationsDir, migration.Dirname(), "upgrade.sql", + mr.cfg.MigrationsDir, migration.Dirname(), "upgrade.sql", ) contents, err := os.ReadFile(upgradeScriptPath) if err != nil { -- cgit v1.2.3