This repository was archived by the owner on Sep 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
205 lines (180 loc) · 5.61 KB
/
main.go
File metadata and controls
205 lines (180 loc) · 5.61 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
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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/google/subcommands"
"github.com/valyala/fasttemplate"
)
type unpackCmd struct {
data string
apk string
}
func (*unpackCmd) Name() string {
return "unpack"
}
func (*unpackCmd) Synopsis() string {
return "unpack Minecraft's apk"
}
func (*unpackCmd) Usage() string {
return "unpack [-target] [-apk]\n\tUnpack Minecraft Apk\n"
}
func (c *unpackCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.data, "target", "data", "Unpack Target")
f.StringVar(&c.apk, "apk", "minecraft.apk", "Unpack Source")
}
func (c *unpackCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
defer func() {
if r := recover(); r != nil {
fmt.Println("\033[5;91mError: ", r)
ret = subcommands.ExitFailure
}
}()
unpack(c.data, c.apk)
return subcommands.ExitSuccess
}
type attachCmd struct {
profile string
prompt string
}
func (*attachCmd) Name() string { return "attach" }
func (*attachCmd) Synopsis() string { return "attach daemon" }
func (*attachCmd) Usage() string { return "attach [-profile] [-prompt]\n" }
func (a *attachCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&a.profile, "profile", "default", "Game Profile")
f.StringVar(&a.prompt, "prompt", "{{esc}}[0;36;1msocket:{{esc}}[22m//{{username}}@{{hostname}}$ {{esc}}[33;4m", "Prompt String Template")
}
func (a *attachCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
defer func() {
if r := recover(); r != nil {
fmt.Println("\033[5;91mError: \n", r)
ret = subcommands.ExitFailure
}
}()
attach(a.profile, fasttemplate.New(a.prompt, "{{", "}}"))
printInfo("Done.")
return subcommands.ExitSuccess
}
type runCmd struct {
profile string
prompt string
}
func (*runCmd) Name() string {
return "run"
}
func (*runCmd) Synopsis() string {
return "run minecraft server"
}
func (*runCmd) Usage() string {
return "run [-profile] [-prompt] \n\tRun Minecraft Server\n"
}
func (c *runCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.profile, "profile", "default", "Game Proile")
f.StringVar(&c.prompt, "prompt", "{{esc}}[0;36;1mmcpe:{{esc}}[22m//{{username}}@{{hostname}}$ {{esc}}[33;4m", "Prompt String Template")
}
func checkBin() {
if _, err := os.Stat("./bin"); err != nil {
printWarn("/bin not found, checking /opt/mcpeserver-core...")
if _, err = os.Stat("/opt/mcpeserver-core"); err != nil {
printWarn("/opt/mcpeserver-core not found, exiting...")
os.Exit(1)
} else {
os.Symlink("/opt/mcpeserver-core", "bin")
}
}
}
func (c *runCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
defer func() {
if r := recover(); r != nil {
fmt.Println("\033[5;91mError: \n", r)
ret = subcommands.ExitFailure
}
}()
checkBin()
if run(c.profile, fasttemplate.New(c.prompt, "{{", "}}")) {
printInfo("Done.")
return subcommands.ExitSuccess
}
printInfo("Failed.")
return subcommands.ExitFailure
}
type daemonCmd struct {
profile string
systemd bool
}
func (*daemonCmd) Name() string { return "daemon" }
func (*daemonCmd) Synopsis() string { return "Daemon" }
func (*daemonCmd) Usage() string {
return "daemon [-profile] [-systemd]\n\tRun server as daemon"
}
func (d *daemonCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&d.profile, "profile", "default", "Game Profile")
f.BoolVar(&d.systemd, "systemd", false, "Systemd mode")
}
func (d *daemonCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
defer func() {
if r := recover(); r != nil {
fmt.Println("\033[5;91mError: \n", r)
ret = subcommands.ExitFailure
}
}()
checkBin()
runDaemon(d.profile, d.systemd)
return subcommands.ExitSuccess
}
type versionCmd struct{}
func (*versionCmd) Name() string { return "version" }
func (*versionCmd) Synopsis() string { return "Show version" }
func (*versionCmd) Usage() string { return "version" }
func (*versionCmd) SetFlags(f *flag.FlagSet) {}
func (*versionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
printPair("Version", VERSION)
return subcommands.ExitSuccess
}
type execCmd struct {
profile string
timeout int
}
func (*execCmd) Name() string { return "exec" }
func (*execCmd) Synopsis() string { return "Exec command and retrieve the output" }
func (*execCmd) Usage() string { return "exec [-profile] [-timeout] [command]\n" }
func (cmd *execCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.profile, "profile", "default", "Game Profile")
f.IntVar(&cmd.timeout, "timeout", 1000, "Timout")
}
func (cmd *execCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (ret subcommands.ExitStatus) {
defer func() {
if r := recover(); r != nil {
fmt.Println("\033[5;91mError: \n", r)
ret = subcommands.ExitFailure
}
}()
args := f.Args()
if len(args) == 0 {
printWarn("Empty command")
return subcommands.ExitUsageError
}
result, err := runExec(cmd.profile, strings.Join(args, " "), cmd.timeout)
if err != nil {
printWarn(err.Error())
return subcommands.ExitFailure
}
fmt.Println(result)
return subcommands.ExitSuccess
}
func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&unpackCmd{}, "")
subcommands.Register(&attachCmd{}, "")
subcommands.Register(&runCmd{}, "")
subcommands.Register(&daemonCmd{}, "")
subcommands.Register(&execCmd{}, "")
subcommands.Register(&versionCmd{}, "")
flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
}