59 lines
946 B
Go
59 lines
946 B
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"github.com/ParadoxPixel/ThemePark-Websocket/server"
|
||
|
"os"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var commands = make(map[string]func([]string) bool)
|
||
|
|
||
|
func ReadConsole() {
|
||
|
loadCommands()
|
||
|
reader := bufio.NewReader(os.Stdin)
|
||
|
|
||
|
for {
|
||
|
text, _ := reader.ReadString('\n')
|
||
|
text = strings.Replace(text, "\n", "", -1)
|
||
|
|
||
|
args := strings.Split(text, " ")
|
||
|
command := strings.ToLower(args[0])
|
||
|
args = args[1:]
|
||
|
|
||
|
b := handleCommand(command, args)
|
||
|
if b {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func loadCommands() {
|
||
|
commands["exit"] = func(i []string) bool {
|
||
|
server.Stop()
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
commands["ip"] = func(i []string) bool {
|
||
|
ip, err := GetPublicIp()
|
||
|
if err != nil {
|
||
|
fmt.Println("Unable to get public IP")
|
||
|
} else {
|
||
|
fmt.Println("IP:", ip)
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func handleCommand(cmd string, args []string) bool {
|
||
|
f := commands[cmd]
|
||
|
if f == nil {
|
||
|
fmt.Println("Unknown command:", cmd)
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return f(args)
|
||
|
}
|