From 809fc4639e58ca9ca082ce68b5c62b463b2f1e1d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 30 Dec 2023 16:00:20 -0500 Subject: Add initial code --- internal/config/config.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 internal/config/config.go (limited to 'internal/config') diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..89acd23 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,82 @@ +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) + } +} -- cgit v1.2.3