-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (145 loc) · 4.01 KB
/
main.go
File metadata and controls
154 lines (145 loc) · 4.01 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/goccy/go-yaml"
"github.com/nobl9/nobl9-go/internal/pathutils"
"github.com/nobl9/nobl9-go/manifest"
v1alphaExamples "github.com/nobl9/nobl9-go/manifest/v1alpha/examples"
"github.com/nobl9/nobl9-go/sdk"
)
type examplesGeneratorConfig struct {
Examples []v1alphaExamples.Example
Path string
Comments yaml.CommentMap
}
const manifestPath = "manifest"
func main() {
rootPath := pathutils.FindModuleRoot()
configs := getV1alphaExamplesConfigs()
for _, config := range configs {
examples := make([]any, 0, len(config.Examples))
for _, variant := range config.Examples {
examples = append(examples, variant.GetObject())
}
config.Path = filepath.Join(rootPath, config.Path)
var input any
if len(examples) == 1 {
input = examples[0]
} else {
input = examples
}
if err := writeExamples(input, config.Path, config.Comments); err != nil {
panic(err.Error())
}
}
}
func getV1alphaExamplesConfigs() []examplesGeneratorConfig {
basePath := filepath.Join(manifestPath, "v1alpha")
// Non-standard examples.
configs := []examplesGeneratorConfig{
{
Examples: v1alphaExamples.Labels(),
Path: filepath.Join(basePath, "labels_examples.yaml"),
},
{
Examples: v1alphaExamples.MetadataAnnotations(),
Path: filepath.Join(basePath, "metadata_annotations_examples.yaml"),
},
}
// Standard examples.
allExamples := [][]v1alphaExamples.Example{
v1alphaExamples.Project(),
v1alphaExamples.Service(),
v1alphaExamples.AlertMethod(),
v1alphaExamples.SLO(),
v1alphaExamples.Agent(),
v1alphaExamples.Direct(),
v1alphaExamples.AlertPolicy(),
v1alphaExamples.AlertSilence(),
v1alphaExamples.Alert(),
v1alphaExamples.Annotation(),
v1alphaExamples.BudgetAdjustment(),
v1alphaExamples.DataExport(),
v1alphaExamples.RoleBinding(),
v1alphaExamples.Report(),
}
for _, examples := range allExamples {
object := examples[0].GetObject().(manifest.Object)
basePath := filepath.Join(
manifestPath,
object.GetVersion().VersionString(),
object.GetKind().ToLower(),
)
grouped := groupBy(examples, func(e v1alphaExamples.Example) string { return e.GetVariant() })
for variant, examples := range grouped {
var path string
if len(grouped) == 1 {
// If we don't have any variants, we can write all examples into examples.yaml file.
path = filepath.Join(basePath, "examples.yaml")
} else {
path = filepath.Join(basePath, "examples", strings.ReplaceAll(strings.ToLower(variant), " ", "-")+".yaml")
}
config := examplesGeneratorConfig{
Examples: examples,
Path: path,
Comments: make(yaml.CommentMap),
}
if len(examples) == 1 {
configs = append(configs, config)
continue
}
if examples[0].GetSubVariant() != "" {
sort.Slice(examples, func(i, j int) bool {
return examples[i].GetSubVariant() < examples[j].GetSubVariant()
})
}
for i, example := range examples {
comments := example.GetYAMLComments()
if len(comments) == 0 {
continue
}
for i := range comments {
comments[i] = " " + comments[i]
}
config.Comments[fmt.Sprintf("$[%d]", i)] = []*yaml.Comment{yaml.HeadComment(comments...)}
}
configs = append(configs, config)
}
}
return configs
}
func writeExamples(v any, path string, comments yaml.CommentMap) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
// #nosec G304
file, err := os.Create(path)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
if object, ok := v.(manifest.Object); ok {
return sdk.EncodeObject(object, file, manifest.ObjectFormatYAML)
}
opts := []yaml.EncodeOption{
yaml.Indent(2),
yaml.UseLiteralStyleIfMultiline(true),
}
if len(comments) > 0 {
opts = append(opts, yaml.WithComment(comments))
}
enc := yaml.NewEncoder(file, opts...)
return enc.Encode(v)
}
func groupBy[K comparable, V any](s []V, key func(V) K) map[K][]V {
m := make(map[K][]V)
for _, v := range s {
k := key(v)
m[k] = append(m[k], v)
}
return m
}