Skip to content

Parse configuration values into given struct from environment, files, json, bytes, or other input convertible to bytes

License

Notifications You must be signed in to change notification settings

andreiavrammsd/config

Repository files navigation

Config

License build codecov GoDoc Go Report Card Socket.dev

Package config parses configuration values into given struct.

Requirements for configuration struct:

  • A non-nil pointer to the struct must be passed.
  • Fields must be exported. Unexported fields will be ignored.
  • A field can have the env tag which defines the key of the value. If no tag provided, the key will be the uppercase full path of the field (all the fields names starting root until current field, joined by underscore).
  • The json tag will be used for parsing from JSON.

Input sources:

  • environment variables
  • environment variables from files
  • byte array
  • json
package main

import (
	"fmt"
	"log"

	"github.com/andreiavrammsd/config"
)

type Config struct {
	Username string `env:"CUSTOM_USERNAME_TAG"`
	Tag      string `default:"none"`
}

func main() {
	input := []byte(`CUSTOM_USERNAME_TAG=msd # username`)

	cfg := Config{}
	if err := config.New().FromBytes(&cfg, input); err != nil {
		log.Fatalf("cannot parse config: %s", err)
	}

	fmt.Println(cfg.Username)
	fmt.Println(cfg.Tag)
}

Install

go get github.com/andreiavrammsd/config

Usage

See examples and tests.

Testing and QA tools for development

See Makefile and VS Code setup.

Known issues

  • In some cases, quoted values are not parsed correctly.
MULTILINE_QUOTED="this is a multiline
quoted
value"

QUOTED_INCLUDING_QUOTES="{ \"name\": \"John\", \"age\": 30 }"

About

Parse configuration values into given struct from environment, files, json, bytes, or other input convertible to bytes

Topics

Resources

License

Stars

Watchers

Forks