sconfig

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 10 Imported by: 7

README

sconfig

Configure your Go applications with a single struct

Example

import "github.com/psiemens/sconfig"

type Config struct {
    Environment string        `default:"LOCAL" flag:"env,e" info:"application environment"`
    Port        int           `default:"80" flag:"port,p" info:"port to start the server on"`
    Timeout     time.Duration `default:"1s"`

    // Deprecated Flags
    HostPort int `default:"80" flag:"host-port" deprecated:"use --port flag instead"`
}

var conf Config

var cmd = &cobra.Command{
	Use: "hello-world",
	Run: func(cmd *cobra.Command, args []string) {
		log.Println("Hello world!")
	},
}

func init() {
	err := sconfig.New(&conf).
		FromEnvironment("APP").
		BindFlags(cmd.PersistentFlags()).
		Parse()
	if err != nil {
		log.Fatal(err)
	}
}
# call with env vars and/or command line flags
APP_TIMEOUT=5s hello-world --port 8080 -e PRODUCTION

Documentation

Overview

Package sconfig is a utility to parse application configurations from environment variables and command line flags.

This package uses https://github.com/spf13/viper under the hood and is compatible with CLI apps built with https://github.com/spf13/cobra.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSpecification = errors.New(
	"specification must be a struct pointer")

Functions

This section is empty.

Types

type Config

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

func New

func New(spec interface{}) *Config

func (*Config) BindFlags

func (c *Config) BindFlags(flagSet *pflag.FlagSet) *Config

func (*Config) FromEnvironment

func (c *Config) FromEnvironment(envPrefix string) *Config

func (*Config) Parse

func (c *Config) Parse() error

type ErrInvalidField

type ErrInvalidField struct {
	Field string
	Err   error
}

func (*ErrInvalidField) Error

func (e *ErrInvalidField) Error() string

type ErrInvalidFlagFormat

type ErrInvalidFlagFormat struct {
	Format string
}

func (*ErrInvalidFlagFormat) Error

func (e *ErrInvalidFlagFormat) Error() string

type ErrRequiredFields

type ErrRequiredFields struct {
	Fields []string
}

func (*ErrRequiredFields) Error

func (e *ErrRequiredFields) Error() string

type ErrUnsupportedFieldType

type ErrUnsupportedFieldType struct {
	Type string
}

func (*ErrUnsupportedFieldType) Error

func (e *ErrUnsupportedFieldType) Error() string

Jump to

Keyboard shortcuts

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