goconfig

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

README

GOCONFIG

Go Report Card tests Go Reference

goconfig is a simple but configurable library for adding configuration file -> environment variables -> cli flag parsing to your app.

Usage

go run examples/main.go -config examples/example.yaml

package main

import (
    "fmt"

    "github.com/jacobweinstock/goconfig"
)

// config fields must be exported (uppercase)
// CLI flags by default split camelCase field names with dashes
// e.x. `KeyOne` would be a cli flag of `-key-one`
// To modify this, add a struct tag
// KeyOne string `flag:"keyone"` will give you a cli flag of `-keyone`
type config struct {
    KeyOne  string `flag:"keyone"`
    Workers int
    Config  string
}

func main() {
    // set any default values
    cfg := config{Workers: 4}
    // create a config parser with any options; see option.go for all options
    config := goconfig.NewParser(
        goconfig.WithPrefix("TEST"),
        goconfig.WithFile("example.yaml"),
    )
    // run the config/env/flag parser
    err := config.Parse(&cfg)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("%+v\n", cfg)
}

Customization

goconfig is fully customizable. If you prefer to parse config files, environment variables or cli flags in a different way you can pass in your own implementations.

  • WithFileInterface(myFileImplementation)
  • WithEnvInterface(myEnvImplementation)
  • WithFlagInterface(myFlagImplementation)

The interface definitions:

// EnvParser interface for environment variables
type EnvParser interface {
    // Parse environment variables and update the config
    // interface with the values
    Parse(log logr.Logger, prefix string, config interface{}) error
}

// FlagParser interface for environment variables
type FlagParser interface {
    // Parse environment variables and update the config
    // interface with the values
    Parse(log logr.Logger, config interface{}) error
}
// WithFileInterface() takes a `*registrar.Registry` so that
// multiple file parsers can be registered and used
r := registrar.NewRegistry()
// example, register a yaml file parser you write
r.Register(
    fileInterfaceNameYaml,
    fileInterfaceProtocolYaml,
    fileInterfaceFeaturesYaml,
    nil,
    new(yamlParser),
)


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseEnv

func ParseEnv(log logr.Logger, prefix string, config interface{}, p EnvParser) error

ParseEnv environment

func ParseFile

func ParseFile(log logr.Logger, name string, config interface{}, o []FileParser) (err error)

ParseFile parses a file, trying all interface implementations passed in

func ParseFileFromInterfaces

func ParseFileFromInterfaces(log logr.Logger, name string, config interface{}, generic []interface{}) (err error)

ParseFileFromInterfaces pass through to ParseFile function

func ParseFlags

func ParseFlags(log logr.Logger, config interface{}, p FlagParser) error

ParseFlags cli flags

Types

type EnvParser

type EnvParser interface {
	// Parse environment variables and update the config
	// interface with the values
	Parse(log logr.Logger, prefix string, config interface{}) error
}

EnvParser interface for environment variables

type FileParser

type FileParser interface {
	// Parse config file  and update the config
	// interface with the values
	Parse(log logr.Logger, name string, config interface{}) error
}

FileParser interface for configuration files

type FlagParser

type FlagParser interface {
	// Parse environment variables and update the config
	// interface with the values
	Parse(log logr.Logger, config interface{}) error
}

FlagParser interface for environment variables

type Option

type Option func(*Parser)

Option for config

func WithEnvInterface

func WithEnvInterface(ei EnvParser) Option

WithEnvInterface sets the env parser to use

func WithFile

func WithFile(name string) Option

WithFile sets the config file name

func WithFileInterface

func WithFileInterface(fi *registrar.Registry) Option

WithFileInterface sets the file parser to use

func WithFlagInterface

func WithFlagInterface(fi FlagParser) Option

WithFlagInterface sets the flag parser to use

func WithLogger

func WithLogger(logger logr.Logger) Option

WithLogger sets the logger

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the prefix for env vars PREFIX_

func WithUsage

func WithUsage(usage func()) Option

WithUsage sets the usage func

type Parser

type Parser struct {
	Logger        logr.Logger
	Prefix        string
	File          string
	Usage         func()
	FlagInterface FlagParser
	EnvInterface  EnvParser
	FileInterface *registrar.Registry
}

Parser struct

func NewParser

func NewParser(opts ...Option) *Parser

NewParser parser struct

func (*Parser) Parse

func (c *Parser) Parse(confStruct interface{}) error

Parse a config file, env vars and cli flags (override is in that order).

The fields of the confStruct passed in must be exported (uppercase).

CLI flags by default split camelCase field names with dashes. e.x. `KeyOne` would be a cli flag of `-key-one`. To modify this, add a struct tag. KeyOne string `flag:"keyone"` will give you a cli flag of `-keyone`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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