-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (130 loc) · 3.24 KB
/
main.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
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
package main
import (
_ "embed"
"io/fs"
"log"
"os"
"slices"
"text/template"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
sprig "github.com/go-task/slim-sprig/v3"
)
//go:embed templates/Chart.tmpl.yaml
var chartTemplate string
//go:embed templates/Makefile.tmpl
var makefileTemplate string
//go:embed templates/jenkins/values.tmpl.yaml
var jenkinsValuesTemplate string
//go:embed templates/prometheus/values.tmpl.yaml
var prometheusValuesTemplate string
//go:embed templates/meta-monitoring/values.tmpl.yaml
var metaMonitoringValuesTemplate string
type FormData struct {
dirFs fs.FS
Apps []string
}
func writeMainChart(formData FormData) {
t, err := template.New("").Funcs(sprig.FuncMap()).Parse(chartTemplate)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("fake-repo/Chart.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Fatal(err)
}
err = t.Execute(f, formData)
if err != nil {
log.Fatal(err)
}
}
func writeMakefile(formData FormData) {
t, err := template.New("").Funcs(sprig.FuncMap()).Parse(makefileTemplate)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("fake-repo/Makefile", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Fatal(err)
}
err = t.Execute(f, formData)
if err != nil {
log.Fatal(err)
}
}
func writeJenkinsValues(formData FormData) {
if !slices.Contains(formData.Apps, "jenkins") {
return
}
t, err := template.New("").Funcs(sprig.FuncMap()).Parse(jenkinsValuesTemplate)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("fake-repo/jenkins.values.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Fatal(err)
}
err = t.Execute(f, formData)
if err != nil {
log.Fatal(err)
}
}
func writePrometheusValues(formData FormData) {
if !slices.Contains(formData.Apps, "prometheus") {
return
}
t, err := template.New("").Funcs(sprig.FuncMap()).Parse(prometheusValuesTemplate)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("fake-repo/prometheus.values.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Fatal(err)
}
err = t.Execute(f, formData)
if err != nil {
log.Fatal(err)
}
}
func writeMetaMonitoringValues(formData FormData) {
if !slices.Contains(formData.Apps, "meta-monitoring") {
return
}
t, err := template.New("").Funcs(sprig.FuncMap()).Parse(metaMonitoringValuesTemplate)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("fake-repo/meta-monitoring.values.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
log.Fatal(err)
}
err = t.Execute(f, formData)
if err != nil {
log.Fatal(err)
}
}
func main() {
var formData FormData
form := huh.NewForm(
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("What applications do you want to install?").
Options(
huh.NewOption("Jenkins", "jenkins"),
huh.NewOption("Prometheus", "prometheus"),
huh.NewOption("Meta Monitoring", "meta-monitoring"),
).
Value(&formData.Apps),
),
).WithProgramOptions(tea.WithAltScreen())
formData.dirFs = os.DirFS("fake-repo")
err := form.Run()
if err != nil {
log.Fatal(err)
}
writeMainChart(formData)
writeMakefile(formData)
writeJenkinsValues(formData)
writePrometheusValues(formData)
writeMetaMonitoringValues(formData)
}