/
configlets.go
91 lines (76 loc) · 2.18 KB
/
configlets.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
package utils
import (
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"sort"
"strings"
)
func ConfigletSupportsPlatforms(configletdata *apstra.ConfigletData, platforms []apstra.PlatformOS) bool {
supportedPlatforms := make(map[apstra.PlatformOS]struct{})
for _, generator := range configletdata.Generators {
supportedPlatforms[generator.ConfigStyle] = struct{}{}
}
for _, platform := range platforms {
if _, ok := supportedPlatforms[platform]; !ok {
return false
}
}
return true
}
func AllConfigletSectionNames() []string {
platformToFriendlySectionName := ConfigletValidSectionsMap() // map of os -> friendly section names
friendlySectionNames := make(map[string]struct{}) // unique-ify the friendly section names here
for _, v := range platformToFriendlySectionName {
for _, friendlyName := range v {
friendlySectionNames[friendlyName] = struct{}{} // map keys are friendly names across all platforms
}
}
// turn the map into a list
var i int
result := make([]string, len(friendlySectionNames))
for k := range friendlySectionNames {
result[i] = k
i++
}
return result
}
func ConfigletSectionNamesByOS(os apstra.PlatformOS) []string {
var r []string
for _, v := range os.ValidSections() {
r = append(r, StringersToFriendlyString(v, os))
}
return r
}
func ConfigletValidSectionsMap() map[string][]string {
var m = make(map[string][]string)
for _, i := range apstra.AllPlatformOSes() {
m[i.String()] = ConfigletSectionNamesByOS(i)
}
return m
}
func ValidSectionsAsTable() string {
m := ConfigletValidSectionsMap()
// collect map keys into a sorted slice so that the table renders in consistent order
keys := make([]string, len(m))
var i int
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
var sb strings.Builder
sb.WriteString(" | **Config Style** | **Valid Sections** |\n")
sb.WriteString(" |---|---|\n")
for _, k := range keys {
sb.WriteString(fmt.Sprintf(" |%s|%s|\n", k, strings.Join(m[k], ", ")))
}
return sb.String()
}
func AllPlatformOSNames() []string {
platforms := apstra.AllPlatformOSes()
result := make([]string, len(platforms))
for i := range platforms {
result[i] = platforms[i].String()
}
return result
}