From b57564aeae0835c6526e7475a3262f894ade9d28 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Tue, 19 Nov 2024 14:29:57 +0100 Subject: [PATCH] config: decouple file loading from crypto init Now that the algorithm for loading the config is non-trivial, the code for it should be re-used everywhere. However, it is currently coupled with a call to `InitCrypto()`, which requires certain things like webhook keys to be configured. This is not suitable for many components that still need the configuration. This commit introduces `config.LoadFiles()` to do only the file loading. `LoadConfig()` uses it, and so can any components that do not care about crypto initialization. --- config/config.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/config/config.go b/config/config.go index 458176f5520219eac1fefa3cfdadff82bc92a4ff..f69bca5413b01b4a45a1bdd74e30fbb43054d1db 100644 --- a/config/config.go +++ b/config/config.go @@ -16,27 +16,12 @@ var ( Addr string ) -// Loads the application configuration, reads options from the command line, -// and initializes some internals based on these results. -func LoadConfig(defaultAddr string) ini.File { - Addr = defaultAddr +// Just loads the config files +func LoadFiles() ini.File { var ( config ini.File err error ) - opts, _, err := getopt.Getopts(os.Args, "b:d") - if err != nil { - panic(err) - } - - for _, opt := range opts { - switch opt.Option { - case 'b': - Addr = opt.Value - case 'd': - Debug = true - } - } // Only loads from one of these locations for _, path := range []string{ @@ -68,7 +53,29 @@ func LoadConfig(defaultAddr string) ini.File { if err != nil { log.Fatalf("Failed to load config file: %v", err) } + return config +} + +// Loads the application configuration, reads options from the command line, +// and initializes some internals based on these results. +func LoadConfig(defaultAddr string) ini.File { + Addr = defaultAddr + + opts, _, err := getopt.Getopts(os.Args, "b:d") + if err != nil { + panic(err) + } + + for _, opt := range opts { + switch opt.Option { + case 'b': + Addr = opt.Value + case 'd': + Debug = true + } + } + config := LoadFiles() crypto.InitCrypto(config) return config }