From 167772b87698cce479090dbe5bd3d1cc7c1bc808 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 20 Dec 2025 14:20:42 +0100 Subject: Implement passing user configuration This allows overwriting values of the default configuration with custom ones. --- config.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'config.go') diff --git a/config.go b/config.go index 7d3cab8..dfea550 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,7 @@ import ( "log" "net" "net/url" + "os" "regexp" "strconv" "time" @@ -120,7 +121,21 @@ func (sc *StartupConfig) FromJSON(data []byte) error { return nil } -func GetStartupConfig() StartupConfig { +func (sc *StartupConfig) FromFile(path string) error { + data, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("Failed to read config file '%s': %w", path, err) + } + + err = sc.FromJSON(data) + if err != nil { + return fmt.Errorf("Could not parse config file '%s': %w", path, err) + } + + return nil +} + +func GetStartupConfig(path string) StartupConfig { config := StartupConfig{} err := config.FromJSON(defaultConfig) @@ -128,5 +143,14 @@ func GetStartupConfig() StartupConfig { log.Fatalf("Could not parse default config: %v", err) } + if path != "" { + log.Printf("Config path: %s", path) + + err = config.FromFile(path) + if err != nil { + log.Fatalf("Failed to read configuration: %v", err) + } + } + return config } -- cgit v1.2.3-70-g09d2