nxconfig

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 License: MIT Imports: 9 Imported by: 0

README

NX Config (Go)

Go Reference

NX Config can be used to map environment variables and flags to struct values. Using reflection (exported) struct field names. Nested structs will be prefixed with the field name.

Usage

import (
    "log"
    "time"

    "github.com/expo21xx/nxconfig"
)

type Config struct {
    Host string
    Port uint16
    PGConfig PGConfig `name:"PG"`
}

type PGConfig struct {
    Host string `default:"localhost"`
    Port uint16 `default:"5432"`
    Username string `usage:"postgres username"`
    Password string
    Timeout time.Duration `name:"connection-timeout"`
}

func main() {
    var cfg Config

    err := nxconfig.Load(&cfg)
    if err != nil {
        log.Fatal(err)
    }

    // use cfg
}

Starting the binary will now parse all flags, and check all environment variables.

NOTE: Flags override environment variables!

Options

The Load function can be customized with a number of helper functions, passed in after the target.

nxconfig.Load(&target, nxconfig.WithArgs([]string{"--override", "foo"}), nxconfig.WithEnv([]string{}))

If you already use spf13/pflag you can pass the *pflag.FlagSet to Load:

nxconfig.Load(&target, nxconfig.WithFlagSet(myflagset))

NOTE: Load will call flagset.Parse() with the arguments it was passed, either with WithArgs or os.Args[1:] if not.

Struct Tags:

  • name: name in PascalCase (overrides automatic inferring). NOTE: Use an empty "" to not export the field.
  • default: default value
  • usage: usage string to print when --help flag is passed

Supported Types

The currently supported types are:

  • string
  • int, int32, int64
  • uint, uint32, uint64
  • flaot32, flaot64
  • bool
  • time.Duration

More types will be added in future on a need to implement basis.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTargetNotAPointer = errors.New("target must be pointer")

ErrTargetNotAPointer is returned if the target passed to Load is not pointer.

View Source
var ErrTargetNotAStruct = errors.New("target must be struct pointer")

ErrTargetNotAStruct is returned if the target passed to Load* is not pointer to a struct type.

Functions

func Load

func Load(target interface{}, opts ...Option) error

Load config values automatically from os.Environ() (no prefix) and os.Args[1:].

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option .

func WithArgs

func WithArgs(args []string) Option

WithArgs overrides the default argument list (os.Args[1:]) used for parsing the flags.

func WithEnv

func WithEnv(env []string) Option

WithEnv overrides the default environment (os.Environ).

func WithFlagset

func WithFlagset(flagset *pflag.FlagSet) Option

WithFlagset overrides the default flagset the flags will be added to.

func WithPrefix

func WithPrefix(p string) Option

WithPrefix sets the env prefix.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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