cfg

package module
v0.0.0-...-a025c85 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: BSD-3-Clause Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

cfg

Convenience package for abstracting away Viper configurations and Cobra flags.

It takes a Viper configuration file like:

root:
    FirstVar: 'Value1'
    SecondVar: 'Value2'
    ThirdVar: 'Value3'

Cobra flags like:

app --first-var Override1 --third-var Override3

And resolves them into a struct with initial values:

package cmd

import (
    "fmt"

    "github.com/bartdeboer/cfg"
    "github.com/spf13/cobra"
)

type Config struct {
    FirstVar  string `usage:"First var description"`
    SecondVar string `usage:"Second var description"`
    ThirdVar  string `usage:"Third var description"`
}

var initial Config

var rootCmd = &cobra.Command{
    Use:   "root",
    Short: "A brief description of your application",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println(initial.FirstVar) // returns Override1
        fmt.Println(initial.SecondVar) // returns Value2
        fmt.Println(initial.ThirdVar) // returns Override3
    },
}

func init() {
    cfg.BindPersistentFlagsKey("root", rootCmd, &initial)
}

Where Cobra flags are overrides for Viper values

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigLoader = func() {

	home, err := homedir.Dir()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	curDir, err := os.Getwd()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	exec, err := os.Executable()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	name := strings.TrimSuffix(filepath.Base(exec), (".exe"))

	viper.AddConfigPath(home)
	viper.AddConfigPath(".")
	viper.AddConfigPath(curDir)
	viper.SetConfigName("." + name)

	viper.AutomaticEnv()

	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
}

Functions

func BindCollectionItem

func BindCollectionItem(c *cobra.Command, rawVal interface{}, options ...func(*BindCollectionOptions))

func BindCollectionItemFields

func BindCollectionItemFields(colField string, selectField string, c *cobra.Command, rawVal interface{})

func BindFlags

func BindFlags(c *cobra.Command, rawVal interface{}, options ...func(*BindOptions))

BindCobraFlags binds a Struct with a viper config when running a Cobra command. Generates Cobra flags for the Struct so they can be overriden.

func BindFlagsKey

func BindFlagsKey(key string, c *cobra.Command, rawVal interface{})

BindCobraFlagsKey binds a Struct with a viper config at a specific key when running a Cobra command. Generates Cobra flags for the struct so they can be overriden

func BindPersistentFlags

func BindPersistentFlags(c *cobra.Command, rawVal interface{}, options ...func(*BindOptions))

BindCobraPersistentFlags persistently binds a Struct with a viper config when running a Cobra command. Generates persistent flags for the struct so they can be overriden. Runs the parent persistent hooks as well.

func BindPersistentFlagsKey

func BindPersistentFlagsKey(key string, c *cobra.Command, rawVal interface{})

BindCobraFlagsKeyKey persistently binds a Struct with a viper config at a specific key when running a Cobra command. Generates persistent flags for the struct so they can be overriden

func BindTo

func BindTo(rawVal interface{}) func(*BindCollectionOptions)

func Collection

func Collection(collection *[]map[string]interface{}) func(*BindCollectionOptions)

func CollectionField

func CollectionField(name string) func(*BindCollectionOptions)

func Get

func Get(key string) interface{}

func GetInt

func GetInt(key string) int

func GetString

func GetString(key string) string

func IdField

func IdField(name string) func(*BindCollectionOptions)

func Key

func Key(key string) func(*BindOptions)

func NoViper

func NoViper(o *BindOptions)

func ReadInConfig

func ReadInConfig()

func SelectField

func SelectField(name string) func(*BindCollectionOptions)

func SelectValue

func SelectValue(value string) func(*BindCollectionOptions)

func Set

func Set(key string, value interface{})

func Unmarshal

func Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error

Unmashal unmarshals the config into a Struct overriding with any flags that are set

func UnmarshalKey

func UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error

Unmashal takes a single key and unmarshals it into a Struct overriding with any flags that are set

func Write

func Write() error

Types

type BindCollectionOptions

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

type BindOptions

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

Jump to

Keyboard shortcuts

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