~bigbes/core-go

b57564aeae0835c6526e7475a3262f894ade9d28 — Conrad Hoffmann 1 year, 8 months ago 5a81e2c
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.
1 files changed, 24 insertions(+), 17 deletions(-)

M config/config.go
M config/config.go => config/config.go +24 -17
@@ 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
}