/
wizard.go
214 lines (205 loc) 路 5.59 KB
/
wizard.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package minectl
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/dirien/minectl-sdk/common"
"github.com/dirien/minectl-sdk/model"
"github.com/dirien/minectl-sdk/template"
"github.com/spf13/cobra"
)
var wizardQuestions = []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Enter the name of your Minecraft server:"},
Validate: func(val interface{}) error {
str, err := val.(string)
if !err {
return errors.New("please enter a the name of your Minecraft server")
}
match, _ := regexp.MatchString(common.NameRegex, str)
if !match {
return errors.New("the name of your Minecraft server must consist of lower case alphanumeric characters or '-'")
}
return nil
},
},
{
Name: "provider",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Choose a cloud provider:",
Options: []string{
"DigitalOcean",
"Civo",
"Scaleway",
"Hetzner",
"Akamai Connected Cloud",
"OVHcloud",
"Equinix Metal",
"Google Compute Engine",
"vultr",
"Azure",
"Oracle Cloud Infrastructure",
"IONOS Cloud",
"Amazon Web Services",
"VEXXHOST",
"Ubuntu Multipass",
"Exoscale",
"Fuga Cloud",
},
PageSize: 15,
},
},
{
Name: "plan",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the plan/size for the server:",
Help: "Plans could have dedicated names, like s-4vcpu-8gb for DO or e2-standard-2 for GCE. Check your provider for details.",
},
},
{
Name: "region",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the region/datacenter:",
Help: "Regions could have dedicated names, like fra1 for DO or europe-west6-a for GCE. Check your provider for details",
},
},
{
Name: "ssh",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter a full path to ssh private public key (like /f/a/key.pub):",
Help: "Please enter the full path to ssh public key like this -> /Users/dirien/Tools/repos/stackit-minecraft/minecraft/ssh/minecraft-be.pub",
},
},
{
Name: "ssh_port",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the ssh port (default 22)",
Default: "22",
},
},
{
Name: "fail2ban_bantime",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the fail2ban bantime (default 600):",
Default: "600",
},
},
{
Name: "fail2ban_maxretry",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the fail2ban maxretry (default 6):",
Default: "6",
},
},
{
Name: "features",
Prompt: &survey.MultiSelect{
Message: "Which additional features do you want for your server?:",
Options: []string{"Monitoring", "RCON"},
},
},
{
Name: "java",
Prompt: &survey.Select{
Message: "Choose the Java version:",
Help: "If you are not running bedrock, you have to select the java version.",
Options: []string{"8", "16", "17"},
Default: "16",
},
},
{
Name: "heap",
Validate: func(val interface{}) error {
if str, ok := val.(string); !ok || !strings.Contains(str, "G") {
return errors.New("enter the Java heap size in following notation <size>G, eg 2G")
}
return nil
},
Prompt: &survey.Input{
Message: "Enter the Java heap size in following notation <size>G:",
Help: "Rule of thumb is to set the heap size to around half of your available system RAM",
Default: "2G",
},
},
{
Name: "rconpw",
Prompt: &survey.Input{
Message: "Enter a RCON password (if you selected that feature):",
Help: "The password for the RCON function",
},
},
{
Name: "edition",
Prompt: &survey.Select{
Message: "Select a Minecraft edition:",
Options: []string{"bedrock", "nukkit", "powernukkit", "craftbukkit", "fabric", "forge", "java", "papermc", "spigot", "purpur"},
Default: "java",
PageSize: 10,
},
},
{
Name: "version",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the Minecraft version number:",
Help: "The version of the Minecraft edition, you want to install.",
},
},
{
Name: "properties",
Prompt: &survey.Multiline{
Message: "Add additional Minecraft server properties:",
Help: "Properties like level-seed,max-players etc. Enter as key=value",
},
},
}
var wizardCmd = &cobra.Command{
Use: "wizard",
Short: "Calls the minectl wizard to create interactively a minectl 馃椇 config",
Example: `mincetl wizard`,
RunE: RunFunc(runWizard),
SilenceUsage: true,
SilenceErrors: true,
}
func init() {
wizardCmd.Flags().StringP("output", "o", "", "output folder for the configuration file for minectl 馃椇 (default: ~/.minectl)")
wizardCmd.Flags().SetAnnotation("output", cobra.BashCompSubdirsInDir, []string{}) //nolint:errcheck
}
func runWizard(cmd *cobra.Command, _ []string) error {
minectlLog.RawMessage("馃 minectl configuration file wizard\n")
wizard := model.Wizard{}
err := survey.Ask(wizardQuestions, &wizard)
if err != nil {
return err
}
config, err := template.NewTemplateConfig(wizard)
if err != nil {
return err
}
outputFolder := GetHomeFolder()
output, err := cmd.Flags().GetString("output")
if err == nil {
if len(output) > 0 {
outputFolder = output
}
}
filename := fmt.Sprintf("%s/config-%s.yaml", outputFolder, wizard.Name)
minectlLog.PrintMixedGreen("\n馃搫 Writing configuration file to %s", filename)
err = os.WriteFile(filename, []byte(config), 0o600)
if err != nil {
return err
}
minectlLog.PrintMixedGreen("\n馃彈 To create the server type:\n\n %s", fmt.Sprintf("minectl create -f %s \n", filename))
return nil
}