3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Socket/command/commands.go

59 lines
946 B
Go
Raw Normal View History

2022-08-12 19:08:47 +00:00
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)
}