libstandard

package module
v0.0.0-...-18b1ef5 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 16 Imported by: 2

README

libstandard

Shared code for config-handling (lightweight viper-replacement) and utility functions.

test

Security

When discovering security issues please refer to the Security process.

License

Contributing

Please refer to the Contribution guildelines.

Code of conduct

Please refer to the Conduct guildelines.

Credits

The config-code is mostly ported from the great cleanenv library which is MIT licensed.

Documentation

Index

Constants

View Source
const (
	// Name of the environment variable or a list of names
	TagEnv = "env"
	// Default value
	TagEnvDefault = "env-default"
	// Flag name
	TagFlagName = "flag"
	// Custom list and map separator
	TagEnvSeparator = "env-separator"
	// Flag to mark a field as required
	TagEnvRequired = "env-required"
	// Flag to specify prefix for structure fields
	TagEnvPrefix = "env-prefix"
)

Supported tags

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

Variables

This section is empty.

Functions

func AddConfigFlag

func AddConfigFlag(cmd *cobra.Command)

func AddVerbosityFlag

func AddVerbosityFlag(cmd *cobra.Command)

func Compress

func Compress(data []byte) ([]byte, error)

func Decompress

func Decompress(data []byte) ([]byte, error)

func DefaultInitializer

func DefaultInitializer(cfg interface{}, cmd *cobra.Command, name string) error

DefaultInitializer loads the config and initializes the logging. This assumes that there are "config" and "verbosity" flags present on the cobra-command.

func FirstOrEmpty

func FirstOrEmpty(slice []string) string

FirstOrEmpty returns the first string from the slice.

func Read

func Read(cfg interface{}, flags *pflag.FlagSet, file string, defaultCfg DefaultFileConfig) error

Read reads configuration from a file, environment variables and cmd-flags, parses them depending on tags in structure provided. Then it reads and parses

Example:

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

var cfg ConfigDatabase

cmd.Flags().Int32("port", 5432, "Server-Port")
...

err := config.Read(&cfg, cmd.Flags(), "config.yml", , DefaultFileConfig{})
if err != nil {
    ...
}

func ReadFromEnv

func ReadFromEnv(cfg interface{}) error

ReadFromEnv reads configuration from environment variables, parses them depending on tags in structure provided. Then it reads and parses

Example:

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

var cfg ConfigDatabase

err := config.ReadFromEnv(&cfg)
if err != nil {
    ...
}

func ReadFromFile

func ReadFromFile(cfg interface{}, file string, defaultCfg DefaultFileConfig) error

ReadFromFile reads configuration from a file and environment variables, parses them 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 := config.ReadFromFile(&cfg, "config.yml", DefaultFileConfig{})
if err != nil {
    ...
}

func ReadFromFlags

func ReadFromFlags(cfg interface{}, flags *pflag.FlagSet) error

ReadFromFlags reads configuration from environment variables and cmd-flags, parses them depending on tags in structure provided. Then it reads and parses

Example:

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

var cfg ConfigDatabase

cmd.Flags().Int32("port", 5432, "Server-Port")
...

err := config.ReadFromFlags(&cfg, cmd.Flags())
if err != nil {
    ...
}

func SetupLogging

func SetupLogging(out io.Writer, level string) error

SetupLogging set the log output as the log level

func ToMap

func ToMap(slice []string) map[string]string

ToMap converts a string-slice to a map[string]string

func Unescape

func Unescape(s string) string

Unescape removes backslashes and double-quotes from strings

func Unique

func Unique(stringSlice []string) []string

Unique removes all duplicate values from the given slice

Types

type DefaultFileConfig

type DefaultFileConfig struct {
	Name       string
	Extensions []string
	Paths      []string
}

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
}

Jump to

Keyboard shortcuts

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