-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (53 loc) · 1.41 KB
/
config.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
package config
import (
"bytes"
"fmt"
"os"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/jtarchie/deployer/providers"
"gopkg.in/yaml.v3"
)
type Config struct {
RunDirectory string `yaml:"run_directory"`
Service string `yaml:"service"`
Image string `yaml:"image"`
DigitalOcean providers.DigitalOcean `yaml:"digital_ocean"`
Builder struct {
Dockerfile string `yaml:"dockerfile"`
Context string `yaml:"context"`
} `yaml:"builder"`
SSH struct {
PrivateKey string `yaml:"private_key"`
} `yaml:"ssh"`
Servers []string `yaml:"servers"`
Bootstrap struct {
Servers int `yaml:"servers"`
} `yaml:"bootstrap"`
}
func FromFile(filename string) (*Config, error) {
contents, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("could not read contents of file: %w", err)
}
config := Config{
RunDirectory: "/var/run/deployer",
}
file, err := template.
New("").
Funcs(sprig.FuncMap()).
Parse(string(contents))
if err != nil {
return nil, fmt.Errorf("could not parse config file: %w", err)
}
builder := &bytes.Buffer{}
err = file.Execute(builder, nil)
if err != nil {
return nil, fmt.Errorf("could not evaluate config file: %w", err)
}
err = yaml.Unmarshal(builder.Bytes(), &config)
if err != nil {
return nil, fmt.Errorf("could not unmarshal config file: %w", err)
}
return &config, nil
}