/
repository.go
109 lines (89 loc) · 3.09 KB
/
repository.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package cfg
import (
"fmt"
"io/ioutil"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
const (
minSearchDepth = 0
maxSearchDepth = 10
// configVersion identifies the format of the configuration files,
// whenever an incompatible change is made, this number has to be
// increased
configVersion int = 4
)
// Repository contains the repository configuration.
type Repository struct {
ConfigVersion int `toml:"config_version" comment:"Version of baur configuration format"`
Database Database `toml:"Database"`
Discover Discover `comment:"Application discovery settings"`
}
// Database contains database configuration
type Database struct {
PGSQLURL string `toml:"postgresql_url" comment:"Connection string to the PostgreSQL database, see https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING"`
}
// Discover stores the [Discover] section of the repository configuration.
type Discover struct {
Dirs []string `toml:"application_dirs" comment:"List of directories containing applications, example: ['go/code', 'shop/']"`
SearchDepth int `toml:"search_depth" comment:"Descend at most SearchDepth levels to find application configs"`
}
// RepositoryFromFile reads the repository config from a file and returns it.
func RepositoryFromFile(cfgPath string) (*Repository, error) {
config := Repository{}
content, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
err = toml.Unmarshal(content, &config)
if err != nil {
return nil, err
}
return &config, err
}
// ExampleRepository returns an exemplary Repository config
func ExampleRepository() *Repository {
return &Repository{
ConfigVersion: configVersion,
Discover: Discover{
Dirs: []string{"."},
SearchDepth: 1,
},
Database: Database{
PGSQLURL: "postgres://postgres@localhost:5432/baur?sslmode=disable&connect_timeout=5",
},
}
}
// ToFile writes an Repository configuration file to filepath.
// If overwrite is true an existent file will be overwriten. If it's false the
// function returns an error if the file exist.
func (r *Repository) ToFile(filepath string, overwrite bool) error {
return toFile(r, filepath, overwrite)
}
// Validate validates a repository configuration
func (r *Repository) Validate() error {
if r.ConfigVersion == 0 {
return fmt.Errorf("config_version value is unset or 0")
}
if r.ConfigVersion != configVersion {
return fmt.Errorf("incompatible configuration files\n"+
"config_version value is %d, expecting version: %d\n"+
"Update your baur configuration files or downgrade baur.", r.ConfigVersion, configVersion)
}
err := r.Discover.Validate()
if err != nil {
return errors.Wrap(err, "[Discover] section contains errors")
}
return nil
}
// Validate validates the Discover section and sets defaults.
func (d *Discover) Validate() error {
if len(d.Dirs) == 0 {
return errors.New("application_dirs parameter is empty")
}
if d.SearchDepth < minSearchDepth || d.SearchDepth > maxSearchDepth {
return fmt.Errorf("search_depth parameter must be in range (%d, %d]",
minSearchDepth, maxSearchDepth)
}
return nil
}