/
file.go
186 lines (145 loc) · 4.2 KB
/
file.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package bosun
import (
"fmt"
"github.com/imdario/mergo"
"github.com/naveego/bosun/pkg/core"
"github.com/naveego/bosun/pkg/environment"
"github.com/naveego/bosun/pkg/mirror"
"github.com/naveego/bosun/pkg/script"
"github.com/naveego/bosun/pkg/values"
"github.com/naveego/bosun/pkg/yaml"
"github.com/pkg/errors"
"io/ioutil"
"regexp"
)
// File represents a loaded bosun.yaml file.
type File struct {
Imports []string `yaml:"imports,omitempty" json:"imports"`
Environments []*environment.Config `yaml:"environments,omitempty" json:"environments"`
AppRefs map[string]*Dependency `yaml:"appRefs,omitempty" json:"appRefs"`
Apps []*AppConfig `yaml:"apps,omitempty" json:"apps"`
Repos []*RepoConfig `yaml:"repos,omitempty" json:"repos"`
FromPath string `yaml:"fromPath" json:"fromPath"`
Config *Workspace `yaml:"-" json:"-"`
Tools []*ToolDef `yaml:"tools,omitempty" json:"tools"`
TestSuites []*E2ESuiteConfig `yaml:"testSuites,omitempty" json:"testSuites,omitempty"`
Scripts []*script.Script `yaml:"scripts,omitempty" json:"scripts,omitempty"`
ValueSets []values.ValueSet `yaml:"valueSets,omitempty" json:"valueSets,omitempty"`
Platforms []*Platform `yaml:"platforms,omitempty" json:"platforms,omitempty"`
// merged indicates that this File has had File instances merged into it and cannot be saved.
merged bool `yaml:"-" json:"-"`
}
func (f *File) MarshalYAML() (interface{}, error) {
if f == nil {
return nil, nil
}
type proxy File
p := proxy(*f)
return &p, nil
}
func (f *File) UnmarshalYAML(unmarshal func(interface{}) error) error {
type proxy File
var p proxy
if f != nil {
p = proxy(*f)
}
err := unmarshal(&p)
if err == nil {
*f = File(p)
}
return err
}
func LoadFile(path string) (*File, error) {
f := &File{
}
err := yaml.LoadYaml(path, &f)
if err != nil {
return nil, errors.Errorf("yaml error loading %q: %s", path, err)
}
f.SetFromPath(path)
return f, nil
}
func (f *File) SetFromPath(path string) {
f.FromPath = path
mirror.ApplyFuncRecursively(f, func(x core.FileSaverSetter) {
x.SetFileSaver(f)
})
mirror.ApplyFuncRecursively(f, func(x core.FromPathSetter) {
x.SetFromPath(f.FromPath)
})
}
func (f *File) Merge(other *File) error {
f.merged = true
for _, otherEnv := range other.Environments {
err := f.mergeEnvironment(otherEnv)
if err != nil {
return errors.Wrap(err, "merge environment")
}
}
for _, otherApp := range other.Apps {
if err := f.mergeApp(otherApp); err != nil {
return errors.Wrapf(err, "merge app %q", otherApp.Name)
}
}
err := mergo.Merge(f, other, mergo.WithAppendSlice)
return err
//
// f.Scripts = append(f.Scripts, other.Scripts...)
// f.Repos = append(f.Repos, other.Repos...)
// f.TestSuites = append(f.TestSuites, other.TestSuites...)
// f.Tools = append(f.Tools, other.Tools...)
//
// return nil
}
func (f *File) Save() error {
if f.merged {
panic("a merged File cannot be saved")
}
b, err := yaml.Marshal(f)
if err != nil {
return errors.WithStack(err)
}
b = stripFromPath.ReplaceAll(b, []byte{})
err = ioutil.WriteFile(f.FromPath, b, 0600)
if err != nil {
return errors.WithStack(err)
}
return nil
}
var stripFromPath = regexp.MustCompile(`\s*fromPath:.*`)
func (f *File) mergeApp(incoming *AppConfig) error {
var appConfigs []*AppConfig
for _, app := range f.Apps {
if app.Name == incoming.Name {
core.Log.Warnf("app %q imported from %q will replace version imported from %q", incoming.Name, incoming.FromPath, app.FromPath)
} else {
appConfigs = append(appConfigs, app)
}
}
f.Apps = append(f.Apps, incoming)
return nil
}
func (f *File) mergeEnvironment(env *environment.Config) error {
if env.Name == "all" {
for _, e := range f.Environments {
e.Merge(env)
}
return nil
}
for _, e := range f.Environments {
if e.Name == env.Name {
e.Merge(env)
return nil
}
}
f.Environments = append(f.Environments, env)
return nil
}
func (f *File) GetEnvironmentConfig(name string) *environment.Config {
for _, e := range f.Environments {
if e.Name == name {
return e
}
}
panic(fmt.Sprintf("no environment named %q", name))
}