-
Notifications
You must be signed in to change notification settings - Fork 35
/
configuration.go
48 lines (38 loc) · 996 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"github.com/client9/xson/hjson"
"github.com/imdario/mergo"
)
// Configuration is the configuration type
type Configuration struct {
Environment
Environments map[string]Environment `json:"environments"`
}
// Load open, read and parse the given configuration file
func Load(filename string) (Configuration, error) {
var c Configuration
c.Table = "migrations"
raw, err := ioutil.ReadFile(filename)
if err != nil {
return Configuration{}, err
}
err = hjson.Unmarshal(raw, &c)
if err != nil {
return Configuration{}, err
}
return c, nil
}
// Env return the requested environment from the configuration
func (c Configuration) Env(name string) (Environment, error) {
if name == "default" {
return c.Environment, nil
}
env, found := c.Environments[name]
if !found {
return Environment{}, fmt.Errorf("unknown environment %s", name)
}
_ = mergo.Merge(&env, c.Environment) // No error can possibly occur here
return env, nil
}