gonfig

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: Apache-2.0 Imports: 8 Imported by: 2

README

gonfig

All-in-one solution for managing configuration from environment values and flags.

Usage

Running the following using the following command would yield localhost:3000?key=secret

go run ./main.go -api-key=secret
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/axatol/gonfig"
)

type config struct {
	Host   string `env:"HOST"`
	Port   int    `env:"PORT" default:"3000"`
	ApiKey string `flag:"api-key"`
}

func init() {
	os.Setenv("HOST", "localhost")
	os.argv
}

// load config using defaults
func example1() {
	cfg := config{}
	_ = gonfig.Load(&cfg)
	fmt.Printf("%s:%d?key=%s\n", cfg.Host, cfg.Port, cfg.ApiKey)
}

// or manually run each step
func example2() {
	// initialise configurator
	fs, _ := gonfig.NewConfig(&cfg)

	// load env vars first
	_ = fs.ReadEnv()

	// register cli flags
	_ = fs.BindFlags(flag.CommandLine)

	// process args as usual
	flag.Parse()

	// ensure required fields are set
	_ = fs.Validate()

	fmt.Printf("%s:%d?key=%s\n", cfg.Host, cfg.Port, cfg.ApiKey)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTag denotes the value to use if not set by other means. Must be
	// parseable to the same type as the value receiver
	DefaultTag = "default"
	// DelimiterTag denotes the separator used for slice types and enum options
	// (defaults to ",")
	DelimiterTag = "delim"
	// EnumTag denotes list of choices to constrain the value by. Every element
	// must be resolvable to the same type as the value reciver
	EnumTag = "enum"
	// EnvTag denotes the name of the environment variable
	EnvTag = "env"
	// FlagTag denotes the name of the cli flag
	FlagTag = "flag"
	// RequiredTag denotes whether or not the value must be set
	// (defaults to false)
	RequiredTag = "required"
	// UsageTag denotes help text for use with cli flags
	UsageTag = "usage"
)

Functions

func Load

func Load(target any) error

Types

type Config

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

func NewConfig

func NewConfig(target any) (*Config, error)

func (Config) BindFlags

func (c Config) BindFlags(fs *flag.FlagSet) error

func (Config) ReadEnv

func (c Config) ReadEnv() error

func (Config) Validate

func (c Config) Validate() error

type Field

type Field struct {
	Name      string
	Usage     string
	FlagName  *string
	EnvName   *string
	Delimiter string
	Required  bool
	Enum      []string
	Value     *Value
}

Field contains the metadata extracted from struct field tags and the value setter

func NewField

func NewField(t reflect.StructField, v reflect.Value) (*Field, error)

NewField extracts and parses the struct tags of a field and creates a value setter based on the struct field value receiver

func (Field) BindFlag

func (f Field) BindFlag(fs FlagSet)

BindFlag registers the field with the given flagset

func (Field) Set

func (f Field) Set(s string) error

Set accepts a raw string value and validates it based on struct tags, then passes it through to the value setter for further parsing

type FlagSet

type FlagSet interface {
	Var(flag.Value, string, string)
}

FlagSet represents the minimum interface to be able to bind using field.BindFlag

type Value

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

Value is the built-in concrete ementation of Value

func NewValue

func NewValue(t reflect.StructField, v reflect.Value) *Value

func (Value) Get

func (v Value) Get() any

func (Value) IsSet

func (v Value) IsSet() bool

func (Value) IsSlice

func (v Value) IsSlice() bool

func (*Value) Set

func (v *Value) Set(raw string) error

func (Value) String

func (v Value) String() string

type ValueParser

type ValueParser func(string) (any, error)

ValueParser receives a raw string and returns the resolved value or an error

Jump to

Keyboard shortcuts

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