config

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 11 Imported by: 0

README

Config

Package config load configuration values into given struct.

The struct must be passed by reference.

Fields must be exported. Unexported fields will be ignored. They 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 the root until current field, joined by underscore).

The json tag will be used for loading from JSON.

package main

import (
	"fmt"
	"log"

	"github.com/andreiavrammsd/config"
)

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

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

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

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

Docs

GoDoc

Install

go get github.com/andreiavrammsd/config

Usage

See examples and tests.

Testing and QA tools for development

See Makefile.

Documentation

Overview

Package config load configuration values into given struct.

The struct must be passed by reference.

Fields must be exported. Unexported fields will be ignored. They 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 the root until current field, joined by underscore).

The `json` tag will be used for loading from JSON.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

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

Loader provides methods to load configuration values into a struct

func Load

func Load(i interface{}) *Loader

Load creates a Loader with given struct

func (*Loader) Bytes

func (l *Loader) Bytes(input []byte) error

Bytes loads config into struct from byte array

Example
package main

import (
	"fmt"
	"log"

	"github.com/andreiavrammsd/config"
)

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

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

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

	fmt.Println(cfg.Username)

}
Output:

msd

func (*Loader) Env

func (l *Loader) Env() error

Env loads config into struct from environment variables

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/andreiavrammsd/config"
)

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

func main() {
	if err := os.Setenv("USERNAME", "msd"); err != nil {
		log.Fatal(err)
	}

	cfg := Config{}
	if err := config.Load(&cfg).Env(); err != nil {
		log.Fatalf("cannot load config: %s", err)
	}

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

}
Output:

msd
none

func (*Loader) EnvFile

func (l *Loader) EnvFile(files ...string) error

EnvFile loads config into struct from environment variables in one or multiple files (dotenv). If no file is passed, the default is ".env".

func (*Loader) JSON

func (l *Loader) JSON(input json.RawMessage) error

JSON loads config into struct from json

func (*Loader) String

func (l *Loader) String(input string) error

String loads config into struct from a string

Jump to

Keyboard shortcuts

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