Skip to content

Commit

Permalink
Add an enableInteractiveConsole for running goTES3MP headless
Browse files Browse the repository at this point in the history
  • Loading branch information
HotaruBlaze committed May 7, 2023
1 parent ae043b6 commit 94a68ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func LoadConfig() (ConfigLoaded bool) {
viper.SetDefault("debug", false)
viper.SetDefault("discord.commandPrefix", "!")
viper.SetDefault("printMemoryInfo", false)
viper.SetDefault("enableInteractiveConsole", true)

viper.SetDefault("irc.enableChatChannel", false)
viper.SetDefault("irc.server", "127.0.0.1")
Expand Down
52 changes: 28 additions & 24 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var tes3mpLogMessage = "[goTES3MP]"

// MultiWrite : Prints to logfile and os.Stdout
var MultiWrite io.Writer
var reader *bufio.Reader

func init() {
if len(GitCommit) == 0 {
Expand Down Expand Up @@ -76,33 +77,36 @@ func main() {
go MemoryDebugInfo()
}

reader := bufio.NewReader(os.Stdin)

if viper.GetBool("enableInteractiveConsole") {
reader = bufio.NewReader(os.Stdin)
}
getStatus(true, false)
for {
time.Sleep(2 * 100 * time.Millisecond)
// TODO: This should be tweaked so ">" is always at the bottom.
fmt.Print("> ")
command, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
command = strings.TrimRight(command, "\r\n")
args := strings.Split(command, " ")

switch strings.ToLower(args[0]) {
case "status":
commandStatus()
case "reloadirc":
commandIrcReconnect()
case "reloaddiscord":
color.HiBlack("Attempting to reload Discord")
InitDiscord()
case "exit", "quit", "stop":
color.HiBlack("Shutting down...")
commandShutdown()
default:
color.Red("[goTES3MP]: " + "Command" + ` "` + command + `" ` + "was not recognised.")
if viper.GetBool("enableInteractiveConsole") {
// TODO: This should be tweaked so ">" is always at the bottom.
fmt.Print("> ")
command, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
command = strings.TrimRight(command, "\r\n")
args := strings.Split(command, " ")

switch strings.ToLower(args[0]) {
case "status":
commandStatus()
case "reloadirc":
commandIrcReconnect()
case "reloaddiscord":
color.HiBlack("Attempting to reload Discord")
InitDiscord()
case "exit", "quit", "stop":
color.HiBlack("Shutting down...")
commandShutdown()
default:
color.Red("[goTES3MP]: " + "Command" + ` "` + command + `" ` + "was not recognised.")
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"encoding/json"
"log"
"strconv"
"strings"
"time"

color "github.com/fatih/color"
"github.com/spf13/viper"
)

// ServerStatus struct
Expand Down Expand Up @@ -57,6 +59,7 @@ func getStatus(firstLaunch bool, showModules bool) {
color.HiBlack("goTES3MP: " + Build)
color.HiBlack("Commit: " + GitCommit)
color.HiBlack("Github: " + "https://github.com/hotarublaze/goTES3MP" + "\n")
color.HiBlack("Interactive Console: " + strconv.FormatBool(viper.GetBool("enableInteractiveConsole")))
if firstLaunch {
color.HiBlack(strings.Repeat("=", 32))
}
Expand Down

0 comments on commit 94a68ec

Please sign in to comment.