~xenrox/ntfy-alertmanager#7: 
Revisit configuration file format

The current configuration file format is not very integration friendly (for example with helm). On the other hand scfg is a small, well-known dependency, which is used by some other projects of mine. Environment variables could be added as a compromise.

Status
REPORTED
Submitter
~xenrox
Assigned to
No-one
Submitted
2 years ago
Updated
1 year, 8 months ago
Labels
v1.0.0

Geno 1 year, 11 months ago ยท edit

Instead of solving #10 maybe replace it with an new configloader.

I really love github.com/knadh/koanf.

here some example code for parsing multiple file formats and enviroment variables into one struct object.

import (
	"github.com/knadh/koanf"
	"github.com/knadh/koanf/parsers/json"
	"github.com/knadh/koanf/parsers/toml"
	"github.com/knadh/koanf/parsers/yaml"
	"github.com/knadh/koanf/providers/env"
	"github.com/knadh/koanf/providers/file"
)

var configExtParser = map[string]koanf.Parser{
	".json": json.Parser(),
	".toml": toml.Parser(),
	".yaml": yaml.Parser(),
	".yml":  yaml.Parser(),
}

type configData struct {
	Log        *zap.Config          `config:"log"`
	Database   database.Database    `config:"database"`
	Webserver  web.Service          `config:"webserver"`
	Oven       oven.Service         `config:"oven"`
	StreamURLs channel.ConfigStream `config:"stream_urls"`
}




func main() {
	// ...
	k := koanf.New("/")

	if configPath != "" {
		fileExt := filepath.Ext(configPath)
		parser, ok := configExtParser[fileExt]
		if !ok {
			log.Panic("unsupported file extension:",
				zap.String("config-path", configPath),
				zap.String("file-ext", fileExt),
			)
		}
		if err := k.Load(file.Provider(configPath), parser); err != nil {
			log.Panic("load file config:", zap.Error(err))
		}
	}

	if err := k.Load(env.Provider("NTFY_ALERT_", "/", func(s string) string {
		return strings.Replace(strings.ToLower(
			strings.TrimPrefix(s, "NTFY_ALERT_")), "__", "/", -1)
	}), nil); err != nil {
		log.Panic("load env:", zap.Error(err))
	}

	config := &configData{}
	if err := k.UnmarshalWithConf("", &config, koanf.UnmarshalConf{Tag: 
"config"}); err != nil {
		log.Panic("reading config", zap.Error(err))
	}


}

~xenrox 1 year, 10 months ago

Thanks I will keep that library in mind. But before switching the configuration file format, I will work on a few features, so that I can keep breaking changes for the configuration file to a minimum.

~xenrox referenced this from #23 2 months ago

Register here or Log in to comment, or comment via email.