config

package
v0.0.0-...-64b0400 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package config contains the configuration handling for dsak.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFromCommandContext

func GetFromCommandContext(cmd *cobra.Command) *viper.Viper

GetFromCommandContext gets the configuration from a command context. If no configuration is found, a new configuration is created.

func New

func New(args []string) (*viper.Viper, error)

New creates parses configuration file and return a new configuration instance.

func RegisterValue

func RegisterValue(name string, valueType ValueType, opts ...ValueOption)

RegisterValue registers a configuration value. `name` is the global name of the configuration value, must be unique. `valueType` is the type of this configuration value. Configurations are taken in this order : 1. command line flag 2. configuration file 3. environment variable. Names must be composed of words of lowercase letters separated by period. Examples : "timeout", "global.verbose" RegisterValue should be called in the init() function of each command.

func SetCommandContext

func SetCommandContext(cmd *cobra.Command, config *viper.Viper)

SetCommandContext sets the command context to contain a new configuration.

func Write

func Write(cfg *viper.Viper) error

Write writes the given configuration to file.

Types

type SetterFunc

type SetterFunc func(cmd *cobra.Command, val string) error

SetterFunc is a function that sets the value from a given string.

type StringerFunc

type StringerFunc func(cmd *cobra.Command) string

StringerFunc is a function that returns a string representing the value.

type Value

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

Value represents a configuration value.

func GetValue

func GetValue(name string) (*Value, error)

GetValue returns the configuration value with the given name.

func GetValues

func GetValues() []*Value

GetValues returns all registered values.

func (Value) Apply

func (c Value) Apply(cmd *cobra.Command, cfg *viper.Viper) error

Apply applies configuration value on a command.

func (Value) AsRawString

func (c Value) AsRawString(cmd *cobra.Command) string

AsRawString gets the configuration value as a raw string, no decorator or quotes.

func (Value) AsString

func (c Value) AsString(cmd *cobra.Command) string

AsString gets the configuration value as a string.

func (Value) GetFlag

func (c Value) GetFlag() string

GetFlag gets the configuration value's flag.

func (Value) GetName

func (c Value) GetName() string

GetName gets the configuration value's name.

func (Value) Set

func (c Value) Set(cmd *cobra.Command, val string) error

Set sets the configuration value from the given string.

type ValueOption

type ValueOption func(*Value) error

ValueOption is a function that can be used to configure a configuration value.

func DefaultValue

func DefaultValue(v interface{}) ValueOption

DefaultValue sets the default value for this configuration value.

func Description

func Description(description string) ValueOption

Description sets configuration description.

func Flag

func Flag(flag string) ValueOption

Flag indicates that this configuration value is controlled by a flag.

func FlagIsPersistent

func FlagIsPersistent() ValueOption

FlagIsPersistant indicates that this configuration is controlled by flag that is persistent in subcommands.

func IgnoreEnv

func IgnoreEnv() ValueOption

IgnoreEnv indicates that this configuration is not controlled by an environment variable.

func Setter

func Setter(f SetterFunc) ValueOption

Setter sets a function to be used when setting value from dsak instead of default func.

func ShortFlag

func ShortFlag(flag byte) ValueOption

ShortFlag indicates that this configuration value is controlled by a short flag.

func Stringer

func Stringer(f StringerFunc) ValueOption

Stringer sets a function to be used when value should be represented as string instead of using fmt.Sprintf("%v").

type ValueType

type ValueType uint16

ValueType is the type of a configuration value.

const (
	// ValueTypeString represents a string.
	ValueTypeString ValueType = iota + 1
	// ValueTypeStrings represents a string slice.
	ValueTypeStrings
	// ValueTypeUint represents an unsigned integer value, on 64 bits.
	ValueTypeUint
	// ValueTypeBool represents a boolean value.
	ValueTypeBool
	// ValueTypeStringsMap represents a map[string][]string.
	ValueTypeStringsMap
)

Jump to

Keyboard shortcuts

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