koanfgo

package module
v0.0.0-...-dfd0627 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2023 License: MIT Imports: 4 Imported by: 0

README

Koanf Go(lang)

Koanf Go(lang) allows to write dynamic configuration files for koanf configuration manager. Sometimes dynamic configuration is required.

Based on the idea of https://github.com/mdouchement/koanflua.

Usage

// config.go
package config

import (
	"maps" // https://github.com/containous/yaegi/issues/327
	"os"
)

func Load() (map[string]interface{}, error) {
	config := map[string]interface{}{
		"brokers": []string{"localhost:42", "localhost:4242"},
		"listen":  "localhost",
		"redis": map[string]interface{}{
			"addr":     "localhost:6379",
			"password": "trololo",
			"db":       1,
		},
	}

	if os.Getenv("MAGIC_FEATURE") == "enabled" {
		maps.Set(config, "feature", "testouille")
	}

	return config, nil
}
package main

import (
	"fmt"
	"log"

	"github.com/knadh/koanf"
	"github.com/knadh/koanf/providers/file"
	"github.com/mdouchement/koanfgo"
)

func main() {
	konf := koanf.New(".")
	err := konf.Load(file.Provider("config.go"), koanfgo.Parser())
	if err != nil {
		log.Fatal(err)
	}

	//

	redisAddr := konf.String("redis.addr")
	fmt.Println(redisAddr)
}

Resources

License

MIT

Contributing

All PRs are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Go

type Go struct {
	// contains filtered or unexported fields
}

Go implements a Go parser for koanf.

func Parser

func Parser() *Go

Parser returns a Go Parser.

func (*Go) Parse

func (p *Go) Parse(b []byte) (map[string]interface{}, error)

Parse parses the given Go bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL