cleanconfig

package module
v0.0.0-...-060effb Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 17 Imported by: 0

README

cleanconfig

License

This project is based on ilyakaznacheev/cleanenv and is licensed under the MIT License. See the LICENSE file for more information.

Description

Fork of https://github.com/ilyakaznacheev/cleanenv with an optional secret manager which implements the SecretManger interface:

type SecretManager interface {
	GetSecret(secret string) (string, error)
}

Documentation

Index

Constants

View Source
const (
	// TagEnv name of the environment variable or a list of names
	TagEnv = "env"

	// TagEnvLayout value parsing layout (for types like time.Time)
	TagEnvLayout = "env-layout"

	// TagEnvDefault default value
	TagEnvDefault = "env-default"

	// TagEnvSeparator custom list and map separator
	TagEnvSeparator = "env-separator"

	// TagEnvDescription environment variable description
	TagEnvDescription = "env-description"

	// TagEnvUpd flag to mark a field as updatable
	TagEnvUpd = "env-upd"

	// TagEnvRequired flag to mark a field as required
	TagEnvRequired = "env-required"

	// TagEnvPrefix аlag to specify prefix for structure fields
	TagEnvPrefix = "env-prefix"
)

Supported tags

View Source
const (
	// DefaultSeparator is a default list and map separator character
	DefaultSeparator = ","
)

Variables

This section is empty.

Functions

func FUsage

func FUsage(w io.Writer, cfg interface{}, headerText *string, usageFuncs ...func()) func()

FUsage prints configuration help into the custom output. Other usage instructions can be wrapped in and executed before this usage function

func GetDescription

func GetDescription(cfg interface{}, headerText *string) (string, error)

GetDescription returns a description of environment variables. You can provide a custom header text.

func ParseJSON

func ParseJSON(r io.Reader, str interface{}) error

ParseJSON parses JSON from reader to data structure

func ParseTOML

func ParseTOML(r io.Reader, str interface{}) error

ParseTOML parses TOML from reader to data structure

func ParseYAML

func ParseYAML(r io.Reader, str interface{}) error

ParseYAML parses YAML from reader to data structure

func ReadConfig

func ReadConfig(path string, cfg interface{}) error

ReadConfig reads configuration file and parses it depending on tags in structure provided. Then it reads and parses

Example:

type ConfigDatabase struct {
	Port     string `yaml:"port" env:"PORT" env-default:"5432"`
	Host     string `yaml:"host" env:"HOST" env-default:"localhost"`
	Name     string `yaml:"name" env:"NAME" env-default:"postgres"`
	User     string `yaml:"user" env:"USER" env-default:"user"`
	Password string `yaml:"password" env:"PASSWORD"`
}

var cfg ConfigDatabase

err := cleanenv.ReadConfig("config.yml", &cfg)
if err != nil {
    ...
}

func ReadConfigWithSecretManager

func ReadConfigWithSecretManager(path string, secretManager SecretManager, cfg interface{}) error

ReadConfigWithSecretManager reads configuration file and parses it depending on tags in structure provided. Accesses secrets from secret manager if field has a secret tag.

Then it reads and parses

Example:

type ConfigDatabase struct {
	Port     string `yaml:"port" env:"PORT" env-default:"5432"`
	Host     string `yaml:"host" env:"HOST" env-default:"localhost"`
	Name     string `yaml:"name" env:"NAME" env-default:"postgres"`
	User     string `yaml:"user" env:"USER" env-default:"user"`
	Password string `yaml:"password" env:"PASSWORD" secret:"PASSWORD"`
}

var cfg ConfigDatabase

err := cleanenv.ReadConfig("config.yml", googleSecrets, &cfg)
if err != nil {
    ...
}

func ReadEnv

func ReadEnv(cfg interface{}) error

ReadEnv reads environment variables into the structure.

func UpdateEnv

func UpdateEnv(cfg interface{}) error

UpdateEnv rereads (updates) environment variables in the structure.

func Usage

func Usage(cfg interface{}, headerText *string, usageFuncs ...func()) func()

Usage returns a configuration usage help. Other usage instructions can be wrapped in and executed before this usage function. The default output is STDERR.

Types

type SecretManager

type SecretManager interface {
	GetSecret(secret string) (string, error)
}

type Setter

type Setter interface {
	SetValue(string) error
}

Setter is an interface for a custom value setter.

To implement a custom value setter you need to add a SetValue function to your type that will receive a string raw value:

type MyField string

func (f *MyField) SetValue(s string) error {
	if s == "" {
		return fmt.Errorf("field value can't be empty")
	}
	*f = MyField("my field is: " + s)
	return nil
}

type Updater

type Updater interface {
	Update() error
}

Updater gives an ability to implement custom update function for a field or a whole structure

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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