-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
130 lines (109 loc) · 3.57 KB
/
template.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
package template
import (
"bytes"
"embed"
_ "embed"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/dirien/minectl-sdk/cloud"
"github.com/dirien/minectl-sdk/model"
)
type Template struct {
Template *template.Template
Values *templateValues
}
type templateValues struct {
*model.MinecraftResource
Mount string
SSHPublicKey string
Properties []string
}
type TemplateName string //nolint:revive
const (
TemplateBash TemplateName = "bash"
TemplateCloudConfig TemplateName = "cloud-config"
TemplateJavaBinary TemplateName = "java-binary"
TemplateBedrockBinary TemplateName = "bedrock-binary"
TemplateSpigotBukkitBinary TemplateName = "spigotbukkit-binary"
TemplateFabricBinary TemplateName = "fabric-binary"
TemplateForgeBinary TemplateName = "forge-binary"
TemplatePaperMCBinary TemplateName = "papermc-binary"
TemplatePurpurBinary TemplateName = "purpur-binary"
TemplateProxyCloudConfig TemplateName = "proxy-cloud-config"
TemplateProxyBash TemplateName = "proxy-bash"
TemplateBungeeCordBinary TemplateName = "bungeecord-binary"
TemplateWaterfallBinary TemplateName = "waterfall-binary"
TemplateVelocityBinary TemplateName = "velocity-binary"
TemplateNukkitBinary TemplateName = "nukkit-binary"
TemplatePowerNukkitBinary TemplateName = "powernukkit-binary"
)
func GetUpdateTemplate() *Template {
bash := template.Must(template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateBash, "templates/bash/*"))
return &Template{
Template: bash,
Values: &templateValues{},
}
}
func (t *Template) DoUpdate(model *model.MinecraftResource, args *CreateUpdateTemplateArgs) (string, error) {
return t.GetTemplate(model, args)
}
type CreateUpdateTemplateArgs struct {
Mount string
SSHPublicKey string
Name TemplateName
}
func (t *Template) GetTemplate(model *model.MinecraftResource, args *CreateUpdateTemplateArgs) (string, error) {
var buff bytes.Buffer
t.Values.MinecraftResource = model
t.Values.Properties = strings.Split(model.GetProperties(), "\n")
t.Values.Mount = args.Mount
t.Values.SSHPublicKey = args.SSHPublicKey
err := t.Template.ExecuteTemplate(&buff, string(args.Name), t.Values)
if err != nil {
return "", err
}
return buff.String(), nil
}
func GetTemplateCloudConfigName(isProxy bool) TemplateName {
if isProxy {
return TemplateProxyCloudConfig
}
return TemplateCloudConfig
}
func GetTemplateBashName(isProxy bool) TemplateName {
if isProxy {
return TemplateProxyBash
}
return TemplateBash
}
//go:embed templates
var templateBash embed.FS
func NewTemplateBash() (*Template, error) {
bash := template.Must(template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateBash, "templates/bash/*"))
return &Template{
Template: bash,
Values: &templateValues{},
}, nil
}
//go:embed templates
var templateCloudConfig embed.FS
func NewTemplateCloudConfig() (*Template, error) {
cloudInit := template.Must(template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateCloudConfig, "templates/cloud-init/*"))
return &Template{
Template: cloudInit,
Values: &templateValues{},
}, nil
}
//go:embed templates
var templateConfig embed.FS
func NewTemplateConfig(value model.Wizard) (string, error) {
var buff bytes.Buffer
value.Provider = cloud.GetCloudProviderCode(value.Provider)
config := template.Must(template.New("config").Funcs(sprig.TxtFuncMap()).ParseFS(templateConfig, "templates/config/*"))
err := config.ExecuteTemplate(&buff, "config", value)
if err != nil {
return "", err
}
return buff.String(), nil
}