-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_manager.go
83 lines (68 loc) · 2.35 KB
/
diff_manager.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
package diff_manager
import (
"slices"
"github.com/jozefcipa/novus/internal/config"
"github.com/jozefcipa/novus/internal/dns_manager"
"github.com/jozefcipa/novus/internal/novus"
"github.com/jozefcipa/novus/internal/shared"
)
func routeExists(domain string, routes []shared.Route) bool {
for _, route := range routes {
if route.Domain == domain {
return true
}
}
return false
}
func DetectConfigDiff(conf config.NovusConfig, state novus.AppState) (added []shared.Route, deleted []shared.Route) {
// Detect routes that are stored in state but have been removed from the configuration file
for _, route := range state.Routes {
if !routeExists(route.Domain, conf.Routes) {
deleted = append(deleted, route)
}
}
// Detect routes that are found in configuration file but are not stored in the state
for _, route := range conf.Routes {
if !routeExists(route.Domain, state.Routes) {
added = append(added, route)
}
}
return added, deleted
}
func DetectUnusedTLDs(deletedRoutes []shared.Route, stateRoutes []shared.Route) []string {
deletedRoutesTLDs := dns_manager.GetTLDs(deletedRoutes)
stateTLDs := dns_manager.GetTLDs(stateRoutes)
unusedTLDs := []string{}
// Iterate through all routes that have been deleted
// and check if their TLD domain is used in the remaining routes in state
// if not, that means the TLD is not used anymore and can be removed
for _, deletedRouteTLD := range deletedRoutesTLDs {
if !slices.Contains(stateTLDs, deletedRouteTLD) {
unusedTLDs = append(unusedTLDs, deletedRouteTLD)
}
}
return unusedTLDs
}
type appDomain struct {
App string
Domain string
}
func DetectDuplicateDomains(existingApps map[string]novus.AppState, addedRoutes []shared.Route) error {
allDomains := []appDomain{}
// Collect all existing domains across apps
for appName, appConfig := range existingApps {
for _, route := range appConfig.Routes {
allDomains = append(allDomains, appDomain{App: appName, Domain: route.Domain})
}
}
// Iterate through the newly added routes to see if some of them already exists in the slice
for _, route := range addedRoutes {
if idx := slices.IndexFunc(allDomains, func(appDomain appDomain) bool { return appDomain.Domain == route.Domain }); idx != -1 {
return &DuplicateDomainError{
DuplicateDomain: route.Domain,
OriginalAppWithDomain: allDomains[idx].App,
}
}
}
return nil
}