-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (78 loc) · 2.19 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
package main
import (
"flag"
"fmt"
"io"
"os"
"turutupa/gsnake/events"
gsnake "turutupa/gsnake/game"
"turutupa/gsnake/log"
"turutupa/gsnake/ssh"
)
// flags
const SSH_MODE = "ssh"
const PORT_FLAG_SHORT = "p"
const PORT_FLAG_LONG = "port"
const MODE_FLAG_SHORT = "m"
const MODE_FLAG_LONG = "mode"
const LOG_FLAG_SHORT = "l"
const LOG_FLAG_LONG = "log"
const HELP_FLAG_SHORT = "h"
const HELP_FLAG_LONG = "help"
const DEFAULT_PORT = 5555
func main() {
var port int
var mode string
var logging bool
var help bool
flag.IntVar(&port, PORT_FLAG_SHORT, DEFAULT_PORT, "")
flag.IntVar(&port, PORT_FLAG_LONG, DEFAULT_PORT, "")
flag.StringVar(&mode, MODE_FLAG_SHORT, "local", "")
flag.StringVar(&mode, MODE_FLAG_LONG, "local", "")
flag.BoolVar(&logging, LOG_FLAG_SHORT, false, "")
flag.BoolVar(&logging, LOG_FLAG_LONG, false, "")
flag.BoolVar(&help, HELP_FLAG_SHORT, false, "")
flag.BoolVar(&help, HELP_FLAG_LONG, false, "")
flag.Usage = displayHelp
flag.Parse()
if logging {
log.EnableStorage()
}
if help {
displayHelp()
}
if mode == SSH_MODE {
sshServer := ssh.NewSshServer(port)
sshServer.Run(snakeApp)
} else {
screen := gsnake.NewScreen(os.Stdout)
game := gsnake.NewOfflineGsnake(screen)
if rows, cols, err := gsnake.GetTerminalSize(); err == nil {
game.OnWindowChange(struct {
Width uint32
Height uint32
PixelWidth uint32
PixelHeight uint32
}{
Width: uint32(cols),
Height: uint32(rows),
PixelWidth: 0,
PixelHeight: 0,
})
}
game.Run()
}
}
func snakeApp(writer io.Writer, eventsPoller events.EventPoller) ssh.SshApp {
screen := gsnake.NewScreen(writer)
return gsnake.NewOnlineGsnake(eventsPoller, screen)
}
func displayHelp() {
fmt.Println("Usage: gsnake [-m mode] [-p port]")
fmt.Println("\nOptions:")
fmt.Println(" -m, --mode\n\t(Optional) Expected values are 'local' or 'ssh'. If -m flag is set to 'ssh' then it will host a gsnake ssh server. Defaults to 'local'.")
fmt.Println(" -p, --port\n\t(Optional) Port number. Only used in 'ssh' mode. Defaults to 5555.")
fmt.Println(" -l, --log\n\t(Optional) Enables logging persistence. Defaults to false.")
fmt.Println(" -h, --help\n\tDisplay this help text.")
os.Exit(0)
}