-
Notifications
You must be signed in to change notification settings - Fork 10
/
include.go
59 lines (46 loc) · 1.28 KB
/
include.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
package cfg
import (
"io/ioutil"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
// Include represents an include configuration file.
type Include struct {
BuildInput BuildInput
BuildOutput BuildOutput
}
// ExampleInclude returns an Include struct with exemplary values.
func ExampleInclude() *Include {
return &Include{
BuildInput: exampleBuildInput(),
BuildOutput: exampleBuildOutput(),
}
}
// IncludeToFile serializes the Include struct to TOML and writes it to filepath.
func (in *Include) IncludeToFile(filepath string) error {
return toFile(in, filepath, false)
}
// IncludeFromFile deserializes an Include struct from a file.
func IncludeFromFile(path string) (*Include, error) {
config := Include{}
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = toml.Unmarshal(content, &config)
if err != nil {
return nil, err
}
removeEmptySections(&config.BuildOutput)
return &config, err
}
// Validate validates an Include configuration struct.
func (in *Include) Validate() error {
if err := in.BuildInput.Validate(); err != nil {
return errors.Wrap(err, "[BuildInput] section contains errors")
}
if err := in.BuildOutput.Validate(); err != nil {
return errors.Wrap(err, "[BuildOutput] section contains errors")
}
return nil
}