snakecharmer

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 10 Imported by: 1

README

snakecharmer

Go package that helps to get Cobra and Viper work together

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CharmingOption

type CharmingOption func(*SnakeCharmer) error

CharmingOption represents Functional Options Pattern. See this article for details - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis CharmingOption is a type for a function that accepts a pointer to an empty or minimal SnakeCharmer struct created in the constructor.

func WithCobraCommand

func WithCobraCommand(cmd *cobra.Command) CharmingOption

WithCobraCommand sets the pointer to the cobra.Command instance REQUIRED

func WithConfigFileBaseName

func WithConfigFileBaseName(s string) CharmingOption

WithConfigFileBaseName sets the base name of the config file (without extension) that will be passed to viper.SetConfigName(). REQUIRED in case of the config file path is a directory, otherwise ignored. This defaults to "config"

func WithConfigFilePath

func WithConfigFilePath(s string) CharmingOption

WithConfigFilePath sets the config file path that will be passed to viper.AddConfigPath() if path is a directory, or to viper.SetConfigFile() if path is a file. This defaults to "", which means config file won't be used.

func WithConfigFileType

func WithConfigFileType(s string) CharmingOption

WithConfigFileType sets the type that will be passed to viper.SetConfigType(). REQUIRED in case if the config file does not have the extension or if the config file extension is not in the list of supported extensions. See viper.SupportedExts for full list of supported extensions. This defaults to "yaml"

func WithDecoderConfigOption

func WithDecoderConfigOption(opt viper.DecoderConfigOption) CharmingOption

WithDecoderConfigOption adds a viper.DecoderConfigOption that will be passed to viper.Unmarshal for configuring mapstructure.DecoderConfig options See https://pkg.golang.ir/github.com/spf13/[email protected]#DecoderConfigOption

func WithEnvTagName

func WithEnvTagName(s string) CharmingOption

WithEnvTagName sets the tag name that snakecharmer reads for setting ENV var name. This defaults to "env"

func WithFieldTagName

func WithFieldTagName(s string) CharmingOption

WithFieldTagName sets the tag name that snakecharmer reads for field names. This defaults to "mapstructure"

func WithFlagHelpTagName

func WithFlagHelpTagName(s string) CharmingOption

WithFlagHelpTagName sets the tag name that snakecharmer reads for flag usage help. This defaults to "usage"

func WithIgnoreUntaggedFields

func WithIgnoreUntaggedFields(on bool) CharmingOption

WithIgnoreUntaggedFields allows to ignore all struct fields without explicit fieldTagName, comparable to `mapstructure:"-"` as default behaviour. See WithFieldTagName

func WithResultStruct

func WithResultStruct(rs interface{}) CharmingOption

WithResultStruct sets the pointer to the struct that will contain the decoded config parameters. REQUIRED NOTE: the struct must be initialized with default values which will be used as flags

func WithViper

func WithViper(viper *viper.Viper) CharmingOption

WithViper sets the pointer to the viper.Viper instance This defaults to viper.New()

type SnakeCharmer

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

SnakeCharmer helps to get Cobra and Viper work together. It uses a user defined Struct for reading field tags and default values. Please note, that the struct must be initialized (with the default values) before passing to the SnakeCharmer. It automatically creates flags and adds them to cobra PersistentFlags flagset. It also creates viper's config params, and sets their default values, binds viper's config param with a corresponding flag from the cobra flagset, binds viper's config param with a corresponding ENV var SnakeCharmer sets the following priority of values: 1. flags (if passed) 2. ENV variables (if env tag set) 3. config file (if used) 4. defaults (from user defined Struct)

func NewSnakeCharmer

func NewSnakeCharmer(opts ...CharmingOption) (*SnakeCharmer, error)

NewSnakeCharmer creates a new snakecharmer instance. charmer, err = NewSnakeCharmer(

WithResultStruct(result),
WithFieldTagName("snakecharmer"),
WithViper(vpr),
WithCobraCommand(cmd),
WithConfigFilePath(defaultConfigFile),
WithConfigFileType("yaml"),
WithIgnoreUntaggedFields(true),
WithDecoderConfigOption(
	func(dc *mapstructure.DecoderConfig) { dc.WeaklyTypedInput = true },
),

)

func (*SnakeCharmer) AddFlags

func (sch *SnakeCharmer) AddFlags()

AddFlags creates flags from tags of a given Result Struct. Adds flags to cobra PersistentFlags flagset, creates viper's config param and sets default value (viper.SetDefault()), binds viper's config param with a corresponding flag from the cobra flagset, binds viper's config param with a corresponding ENV var

func (*SnakeCharmer) ConfigFileBaseName

func (sch *SnakeCharmer) ConfigFileBaseName() string

ConfigFileBaseName returns the base name of the config file (without extension) that will be passed to viper.SetConfigName().

func (*SnakeCharmer) ConfigFilePath

func (sch *SnakeCharmer) ConfigFilePath() string

ConfigFilePath returns the config file path that will be passed to viper.AddConfigPath() if path is a directory, viper.SetConfigFile() if path is a file.

func (*SnakeCharmer) ConfigFileType

func (sch *SnakeCharmer) ConfigFileType() string

ConfigFileType returns the type that will be passed to viper.SetConfigType().

func (*SnakeCharmer) DecoderConfigOptions

func (sch *SnakeCharmer) DecoderConfigOptions() []viper.DecoderConfigOption

DecoderConfigOptions returns the slice of viper.DecoderConfigOption that will be passed to viper.Unmarshal()

func (*SnakeCharmer) EnvTagName

func (sch *SnakeCharmer) EnvTagName() string

EnvTagName returns the tag name that snakecharmer reads for ENV var names.

func (*SnakeCharmer) FieldTagName

func (sch *SnakeCharmer) FieldTagName() string

FieldTagName returns the tag name that snakecharmer reads for field names.

func (*SnakeCharmer) FlagHelpTagName

func (sch *SnakeCharmer) FlagHelpTagName() string

FlagHelpTagName returns the tag name that snakecharmer reads for flag usage help.

func (*SnakeCharmer) IgnoreUntaggedFields

func (sch *SnakeCharmer) IgnoreUntaggedFields() bool

IgnoreUntaggedFields returns the SnakeCharmer.ignoreUntaggedFields value that will be passed as viper.DecoderConfigOption

func (*SnakeCharmer) ResultStruct

func (sch *SnakeCharmer) ResultStruct() interface{}

ResultStruct returns the pointer to the struct that contains the decoded values.

func (*SnakeCharmer) Set

func (sch *SnakeCharmer) Set(opts ...CharmingOption) error

Set sets the snakecharmer options

func (*SnakeCharmer) UnmarshalExact

func (sch *SnakeCharmer) UnmarshalExact() (err error)

UnmarshalExact unmarshals the config into a Struct, erroring if a field is nonexistent in the destination struct.

Jump to

Keyboard shortcuts

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