-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.go
96 lines (80 loc) · 2.12 KB
/
traverse.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
package tyrgin
import "fmt"
// Traverse is a core function to traverse a list of dependencies in a StatusEndpoint.
func Traverse(s []StatusEndpoint, dependencies []string, action, protocol, aboutFilePath, versionFilePath string, customData map[string]interface{}) string {
if action == "" {
action = "about"
}
// base case
if len(dependencies) == 0 {
switch action {
case "about":
return About(s, protocol, aboutFilePath, versionFilePath, customData)
default:
sl := StatusList{
StatusList: []Status{
{
Description: "Unsupported action",
Result: CRITICAL,
Details: fmt.Sprintf("Unsupported traversal action '%s'", action),
},
},
}
return SerializeStatusList(sl)
}
}
headDependency := dependencies[0]
headStatusEndpoint := FindStatusEndpoint(s, headDependency)
if headStatusEndpoint == nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("Status path '%s' is not registered", headDependency),
},
},
}
return SerializeStatusList(sl)
}
if !headStatusEndpoint.IsTraversable {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("%s is not traversable", headStatusEndpoint.Name),
},
},
}
return SerializeStatusList(sl)
}
if headStatusEndpoint.TraverseCheck == nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("%s does not have a TraverseCheck() function defined", headStatusEndpoint.Name),
},
},
}
return SerializeStatusList(sl)
}
// found dependency, continue to traverse with the tail of the dependencies
tailDependencies := dependencies[1:]
resp, err := headStatusEndpoint.TraverseCheck.Traverse(tailDependencies, action)
if err != nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Traverse",
Result: CRITICAL,
Details: err.Error(),
},
},
}
return SerializeStatusList(sl)
}
return resp
}