Help us shape the future of Go! Take the 2026 Go Developer Survey

berus

package module
v0.0.0-...-7a6a514 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 6 Imported by: 0

README

= Berus

berus is a package containing presets and helpers that I often use for https://github/spf13/viper[viper].
It provide some helpers to interface with https://github.om/spf13/cobra[cobra] as well.

== Documentation

For the documentation, you can check

* https://pkg.go.dev/github.com/normegil/berus[Go.dev]
* https://godoc.org/github.com/normegil/berus[Godoc]

Documentation

Overview

berus is a package containing presets and helpers that I often use for github/spf13/viper. It provide some helpers to interface with github.om/spf13/cobra as well.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CobraBindings

func CobraBindings(root *cobra.Command, configurations []CobraBinding) (map[string]*pflag.Flag, error)

CobraBindings will load Flags for cobra commands (and subcommands), and associate them with a configuration key used by viper

Types

type CobraBinding

type CobraBinding struct {
	// Viper configuration key. Will be used to access associated flag value.
	ConfigurationKey string
	// Cobra flag identifier value to bind
	CobraCommandLineKey CobraCommandLineKey
}

CobraBinding is a class representing a binding between a viper configuration key and a cobra flag usage name.

type CobraCommandLineKey

type CobraCommandLineKey struct {
	// Key is a string that will have the form <mycommand>.<mysubcommand>.<flag name>, where <mycommand> and <mysubcommand>
	// should be the the "use" field of the command and subcommand. <flag name> is the flag name to get and will be searched
	// using Lookup() method on pflag.Flagset returned by PersistentFlags() and Flags() method.
	Key string
}

CobraCommandLineKey represent an identifier for a flag in a command or sub-command.

func (CobraCommandLineKey) AssociatedFlag

func (k CobraCommandLineKey) AssociatedFlag(root *cobra.Command) (*pflag.Flag, error)

AssociatedFlag load flag associated with current key in the given command/subcommand.

type CobraConfiguration

type CobraConfiguration struct {
	// Name of the application, used as (uppercased) prefix for environment variables and file name
	ApplicationName string
	// RootCommand is the root cobra command
	RootCommand *cobra.Command
	// Bindings refere all the bindings between cobra flags and viper configuration key
	Bindings []CobraBinding
}

CobraConfiguration allow to customize a default viper configuration

type Configuration

type Configuration struct {

	// Option to force the usage of a specific file. Leave empty to ignore.
	ForcedFile string
	// Initializers will initialize options of viper for some type of configuration (file, envvar, cli, ...)
	Initializers []Initializer
	// contains filtered or unexported fields
}

Configuration will load viper configuration using different combinaisons of Initializers

func NewCobraDefaultConfiguration

func NewCobraDefaultConfiguration(cfg CobraConfiguration) (*Configuration, error)

NewCobraDefaultConfiguration allow to create a configuration with most used option. It will initialize it using given application name, and use - Command line options: using provided bindings and root command - Environment variables: see NewDefaultEnvironmentVariableConfiguration - Configuration file: see NewDefaultFileConfiguration

func NewConfiguration

func NewConfiguration(initializers []Initializer) *Configuration

func (Configuration) Initialize

func (c Configuration) Initialize(vipercfg *viper.Viper) error

Initialize will setup given viper instance with all registered initializers

func (*Configuration) ReadConfiguration

func (c *Configuration) ReadConfiguration() error

ReadConfiguration will initialize viper configuration and read values to populate the initialized viper instance.

func (*Configuration) ViperInstance

func (c *Configuration) ViperInstance() *viper.Viper

Return viper instance associated with this Configuration

type EnvironmentVariableConfiguration

type EnvironmentVariableConfiguration struct {
	// Prefix will be used as prefix for environment variables
	Prefix string
}

EnvironmentVariableConfiguration allow to configure environment variable configuration for an application. Environment variables are base on requested configuration keys where "." are replaced by "_"

func NewDefaultEnvironmentVariableConfiguration

func NewDefaultEnvironmentVariableConfiguration(applicationName string) *EnvironmentVariableConfiguration

NewDefaultEnvironmentVariableConfiguration create a EnvironmentVariableConfiguration where prefix is based on application name ending with an underscore

func (EnvironmentVariableConfiguration) Initialize

func (c EnvironmentVariableConfiguration) Initialize(viper *viper.Viper) error

type FileConfiguration

type FileConfiguration struct {
	// Name is the searched file name
	Name string
	// Extension is the searched file type
	Extension string
	// Paths is a list of paths to search. Last path will tke precedence of previous path, first as the lowest priority.
	Paths []string
}

FileConfiguration will be used to setup a file based configuration loading.

func NewDefaultFileConfiguration

func NewDefaultFileConfiguration(applicationName string) *FileConfiguration

NewDefaultFileConfiguration load a default configuration, where application name is the file name and confifuration file will be a yaml file

func (FileConfiguration) Initialize

func (c FileConfiguration) Initialize(viper *viper.Viper) error

type Initializer

type Initializer interface {
	Initialize(viper *viper.Viper) error
}

Initialize represent a object that will initialize some viper options for the given instance.

type PFlagConfiguration

type PFlagConfiguration struct {
	Bindings map[string]*pflag.Flag
}

PFlagConfiguration is used to setup binding between cobra flags and viper configuration

func (PFlagConfiguration) Initialize

func (c PFlagConfiguration) Initialize(viper *viper.Viper) error

Jump to

Keyboard shortcuts

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