Implemented the ability to enable and disable commands in configuration as well as increased scope of the config to the entire project.

This commit is contained in:
Eclipse
2022-09-18 14:15:11 -04:00
parent e0176ca774
commit 44a42a3365
6 changed files with 309 additions and 165 deletions

View File

@@ -1,8 +1,11 @@
package config
import (
"fmt"
"log"
"net"
"os"
"time"
"github.com/spf13/viper"
)
@@ -16,6 +19,7 @@ type Config struct {
DevModeOptions DevModeOptions
Discord Discord
ServerCommands ServerCommands
Database Database
Launcher Launcher
Sign Sign
@@ -59,6 +63,18 @@ type Discord struct {
DevMode bool
}
// Server commands
type ServerCommands struct {
Enabled bool
Commands []ServerCommand
}
type ServerCommand struct {
Name string
Enabled bool
Prefix string
}
// Database holds the postgres database config.
type Database struct {
Host string
@@ -107,6 +123,28 @@ type EntranceChannelInfo struct {
CurrentPlayers uint16
}
var ErupeConfig *Config
func init() {
var err error
ErupeConfig, err = LoadConfig()
if err != nil {
preventClose(fmt.Sprintf("Failed to load config: %s", err.Error()))
}
}
func GetServerCommandByName(cmdName string) ServerCommand {
var val ServerCommand
for _, c := range ErupeConfig.ServerCommands.Commands {
if c.Name == cmdName {
return c
}
}
return val
}
// getOutboundIP4 gets the preferred outbound ip4 of this machine
// From https://stackoverflow.com/a/37382208
func getOutboundIP4() net.IP {
@@ -148,3 +186,20 @@ func LoadConfig() (*Config, error) {
return c, nil
}
func preventClose(text string) {
if ErupeConfig.DisableSoftCrash {
os.Exit(0)
}
fmt.Println("\nFailed to start Erupe:\n" + text)
go wait()
fmt.Println("\nPress Enter/Return to exit...")
fmt.Scanln()
os.Exit(0)
}
func wait() {
for {
time.Sleep(time.Millisecond * 100)
}
}