/
config.go
69 lines (59 loc) · 1.64 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
64
65
66
67
68
69
package cake
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
type ImageConfig struct {
Id string
Parent string
Repository string
Name string
TagPrefix string `yaml:"tag_prefix"`
TagSuffix string `yaml:"tag_suffix"`
Template string
ExtraFiles []string `yaml:"extra_files"`
ExcludedFiles []string `yaml:"exclude_files"`
Properties map[string]string
}
func (image ImageConfig) String() string {
out, err := json.Marshal(image)
if err != nil {
log.Fatalf("Failed to marshall image config to JSON: %v", err)
}
return fmt.Sprintf("ImageConfig%s", out)
}
type AuthConfig struct {
DockerRegistryUrl string
Username string
Password string
}
type BuildConfig struct {
AuthConfig AuthConfig
BaseDir string
ReleaseTag string
OutputFile string
Images []ImageConfig `yaml:"images"`
GlobalProperties map[string]string `yaml:"global_properties"`
}
func (config BuildConfig) validate() error {
for _, image := range config.Images {
if len(image.Id) == 0 || len(image.Repository) == 0 || len(image.Name) == 0 || len(image.Template) == 0 {
return fmt.Errorf("Image ID, repository, name, or template is not specified for the following definition: " + image.String())
}
}
return nil
}
func (config *BuildConfig) LoadConfigFromFile(fileName string) error {
configFile, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("error reading config file: %v", err)
}
err = yaml.Unmarshal([]byte(configFile), &config)
if err != nil {
return fmt.Errorf("cannot unmarshal data: %v", err)
}
return nil
}