aboutsummaryrefslogtreecommitdiffstats
path: root/internal/configloader/configloader.go
blob: 6ac4d82d0aa0966c35bfc33f31838cc9f3355de0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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"

	"git.eugenetriguba.com/envelope"
	"github.com/BurntSushi/toml"
)

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 {
	Migrations MigrationsConfig `toml:"migrations" env:"BOLT_MIGRATIONS"`

	// Information related to how to connect to the database
	// that is desired to run migrations against.
	Connection ConnectionConfig `toml:"database" env:"BOLT_DB"`
}

type MigrationsConfig struct {
	DirectoryPath string `toml:"directory_path" env:"DIR_PATH"`
}

type ConnectionConfig struct {
	DSN             string `toml:"dsn" env:"DSN"`
	Driver          string `toml:"driver" env:"DRIVER"`
	MigrationsTable string `toml:"migrations_table" env:"MIGRATIONS_TABLE"`
}

func NewConfig() (*Config, error) {
	filePath, err := findConfigFilePath()
	if err != nil && !errors.Is(err, ErrConfigFileNotFound) {
		return nil, err
	}

	cfg := Config{
		Migrations: MigrationsConfig{
			DirectoryPath: "migrations",
		},
		Connection: ConnectionConfig{
			MigrationsTable: "bolt_migrations",
		},
	}
	if !errors.Is(err, ErrConfigFileNotFound) {
		_, err = toml.DecodeFile(filePath, &cfg)
		if err != nil {
			return nil, err
		}
	}

	err = envelope.LoadFromEnv(&cfg)
	if err != nil {
		return nil, err
	}

	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)
	}
}

// fsRootDir retrieves the root directory
// of the filesystem on Windows or any unix-like
// operating system.
func fsRootDir() string {
	if runtime.GOOS == "windows" {
		return os.Getenv("SystemDrive")
	}
	return "/"
}