-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
merge.go
41 lines (31 loc) · 863 Bytes
/
merge.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
package file
import (
"github.com/guumaster/hostctl/pkg/types"
)
// MergeFile joins new content with existing content.
func (f *File) MergeFile(from *File) {
ps := make([]*types.Profile, len(from.data.Profiles))
for i, name := range from.data.ProfileNames {
ps[i] = from.data.Profiles[name]
}
f.MergeProfiles(ps)
}
// MergeProfiles joins new profiles with existing content.
func (f *File) MergeProfiles(profiles []*types.Profile) {
for _, newP := range profiles {
newName := newP.Name
_, ok := f.data.Profiles[newName]
if !ok {
f.data.ProfileNames = append(f.data.ProfileNames, newName)
f.data.Profiles[newName] = newP
continue
}
baseP := f.data.Profiles[newName]
var routes []*types.Route
for _, r := range newP.Routes {
routes = append(routes, r)
}
baseP.AddRoutes(routes)
f.data.Profiles[newName] = baseP
}
}