-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
95 lines (77 loc) · 1.98 KB
/
configuration.go
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
package dbshiftcore
import (
"fmt"
"os"
"strconv"
)
const (
envPathMigrations = "DBSHIFT_ABS_FOLDER_MIGRATIONS"
envOptionIsCreateDisabled = "DBSHIFT_OPTION_IS_CREATE_DISABLED"
envOptionIsDowngradeDisabled = "DBSHIFT_OPTION_IS_DOWNGRADE_DISABLED"
envOptionIsUpgradeDisabled = "DBSHIFT_OPTION_IS_UPGRADE_DISABLED"
)
type configuration struct {
MigrationsPath string
Options configurationOptions
}
type configurationOptions struct {
IsCreateDisabled bool
IsDowngradeDisabled bool
IsUpgradeDisabled bool
}
func getConfiguration() (*configuration, error) {
folderMigrations, err := getEnvVar(envPathMigrations)
if err != nil {
return nil, err
}
// Check if migrations path exists
err = checkMigrationPath(folderMigrations)
if err != nil {
return nil, err
}
// Optional values
options, err := getOptions()
if err != nil {
return nil, err
}
return &configuration{
MigrationsPath: folderMigrations,
Options: *options,
}, nil
}
func checkMigrationPath(migrationsPath string) error {
_, err := os.Stat(migrationsPath)
if os.IsNotExist(err) {
return fmt.Errorf("migrations folder does not exist at %s", migrationsPath)
}
return nil
}
func getOptions() (*configurationOptions, error) {
var err error
options := configurationOptions{}
if options.IsCreateDisabled, err = getBooleanOption(envOptionIsCreateDisabled); err != nil {
return nil, err
}
if options.IsDowngradeDisabled, err = getBooleanOption(envOptionIsDowngradeDisabled); err != nil {
return nil, err
}
if options.IsUpgradeDisabled, err = getBooleanOption(envOptionIsUpgradeDisabled); err != nil {
return nil, err
}
return &options, nil
}
func getBooleanOption(envKey string) (bool, error) {
optionEnv, err := getEnvVar(envKey)
if err == nil {
return strconv.ParseBool(optionEnv)
}
return false, nil
}
func getEnvVar(key string) (string, error) {
var err error
value := os.Getenv(key)
if value == "" {
err = fmt.Errorf("%s is not set", key)
}
return value, err
}