From 4cf2d0f9fdfb7929e88bb7d69986eb41b6358304 Mon Sep 17 00:00:00 2001 From: Nico Lube Date: Mon, 16 Sep 2024 23:28:01 +0200 Subject: [PATCH] Add config opt to set config path --- go.mod | 5 ++++- go.sum | 2 ++ server.go | 20 +++++++++++++++++--- util/config.go | 4 ++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 043d5ae..23ffc4a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,9 @@ go 1.20 require ( github.com/Tnze/go-mc v1.20.2 github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021 + github.com/speedata/optionparser v1.0.2 ) -require github.com/google/uuid v1.3.0 // indirect +require ( + github.com/google/uuid v1.3.0 // indirect +) diff --git a/go.sum b/go.sum index fa779d4..1afe4fa 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,5 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021 h1:31Y+Yu373ymebRdJN1cWLLooHH8xAr0MhKTEJGV/87g= github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021/go.mod h1:WERUkUryfUWlrHnFSO/BEUZ+7Ns8aZy7iVOGewxKzcc= +github.com/speedata/optionparser v1.0.2 h1:AzdBomgOBVjH1KyxvoFnEHjiqLch+B1PVxEWYske+aI= +github.com/speedata/optionparser v1.0.2/go.mod h1:JzOMd1kGlM5gtPBy7reOayfHsTXCvd6P4JU8BW0LicE= diff --git a/server.go b/server.go index faea829..8d7238f 100644 --- a/server.go +++ b/server.go @@ -12,10 +12,24 @@ import ( "github.com/LockBlock-dev/MinePot/util" "github.com/Tnze/go-mc/net" "github.com/muesli/cache2go" + "github.com/speedata/optionparser" ) func main() { - config, err := util.GetConfig() + var configPath string = "/etc/minepot/config.json" + + parser := optionparser.NewOptionParser() + + parser.On("-c", "--config ", "Path to the config file", &configPath) + + err := parser.Parse() + if err != nil { + fmt.Println(err) + fmt.Println("\"MinePot -h --help\" for help") + return + } + + config, err := util.GetConfig(configPath) if err != nil { log.Fatal(err) } @@ -39,7 +53,7 @@ func main() { } defer historyFile.Close() - _, err = historyFile.WriteString("datetime, ip, packets_count, reported, handshake, ping") + _, err = historyFile.WriteString("datetime, ip, packets_count, reported, handshake, ping\n") if err != nil { log.Fatal("Failed to write history headers:", err) } @@ -59,7 +73,7 @@ func main() { listener.Close() }() - log.Printf("Server listening on port %d\nYou can edit the config at /etc/minepot/config.json", config.Port) + log.Printf("Server listening on port %d\nYou can edit the config at %s", config.Port, configPath) if config.WriteLogs { // Logs the logs file path diff --git a/util/config.go b/util/config.go index 1a13680..c1b71b6 100644 --- a/util/config.go +++ b/util/config.go @@ -7,8 +7,8 @@ import ( "github.com/LockBlock-dev/MinePot/types" ) -func GetConfig() (*types.Config, error) { - file, err := os.Open("/etc/minepot/config.json") +func GetConfig(path string) (*types.Config, error) { + file, err := os.Open(path) if err != nil { return &types.Config{}, err }