bind

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RootFlagName = "root"
)

Variables

View Source
var Debugf = func(string, ...interface{}) {}

Functions

func BindDebugFlag

func BindDebugFlag(cmd *cobra.Command)

func BindDefaultsTo

func BindDefaultsTo(cfg interface{}, tag string, k *koanf.Koanf) error

BindDefaultsTo loads the defaults from the provided config struct into a Koanf instance, using field names according to the given struct tag.

func BindPostgresFlags

func BindPostgresFlags(cmd *cobra.Command, dbCfg *config.DBConfig)

BindPostgresFlags binds top-level flags to connect to a postgres database. The provided config is used as defaults for the flags.

func BindRootDir

func BindRootDir(cmd *cobra.Command, defaultVal, desc string) *string

BindRootDir is like BindRootDirVar but the bound variable is internal and returned as a *string, which may be ignored when using RootDir.

func BindRootDirVar

func BindRootDirVar(cmd *cobra.Command, rootDir *string, defaultVal, desc string)

BindRootDirVar is used to bind the RootFlagName with a local command variable. The flag "root" does not have config file analog, so binds with local var, which is then available to all subcommands via RootDir(cmd).

func EnableCLIDebugging

func EnableCLIDebugging()

func GetPostgresFlags

func GetPostgresFlags(cmd *cobra.Command, dbCfg *config.DBConfig) (*pg.ConnConfig, error)

GetPostgresFlags returns the postgres flags from the given command, using the given fallback config, which may be a combination of defaults and values from an existing config file.

func MaybeEnableCLIDebug

func MaybeEnableCLIDebug(cmd *cobra.Command, args []string) error

func MergePostgresFlags

func MergePostgresFlags(conf *pg.ConnConfig, cmd *cobra.Command) (*pg.ConnConfig, error)

MergePostgresFlags merges the given connection config with the flags from the given command. It only sets the fields that are set in the flags.

func PreRunBindEnvMatchingTo

func PreRunBindEnvMatchingTo(cmd *cobra.Command, args []string, prefix string, k *koanf.Koanf) error

PreRunBindEnvMatchingTo is to accomplish all of the following:

KWIL_SECTION_SUBSECTION_SOME_KEY => section.subsection.some-key
KWIL_SECTION_SUBSECTION_KEYNAME => section.subsection.keyname
KWIL_SECTION_SOME_KEY => section.some-key
KWIL_SECTION_KEYNAME => section.keyname

For this to work correctly, a previously loaded default config struct (PreRunBindConfigFile) or pflags (PreRunBindFlags). If neither sources preceded env, they are loaded assuming every "_" is a section delimiter, which may be incorrect for multi-word keys like KWIL_SECTION_SOME_KEY.

func RootDir

func RootDir(cmd *cobra.Command) (string, error)

RootDir complements BindRootDirVar by returning the value of the root directory flag bound by BindRootDirVar. This is available for commands that do not have direct access to the local string pointer bound to the flag, such as subcommands.

func SetFlagsFromStruct

func SetFlagsFromStruct(fs *pflag.FlagSet, cfg interface{})

SetFlagsFromStruct is used to automate the creation of flags in the provided pflag set from a tagged struct. This uses the field tags: "toml" for the flag name, and "comment" for the help string. Any "_" characters in the "toml" tag are converted to "-" when defining the flag. This recurses into nested structs.

func SetFlagsFromStructTags

func SetFlagsFromStructTags(fs *pflag.FlagSet, cfg interface{}, nameTag, descTag string)

SetFlagsFromStructTags is a more generalized version of SetFlagsFromStruct that allows setting the field tags used to get flag name and description.

Types

type LazyPrinter

type LazyPrinter func() string

func (LazyPrinter) String

func (p LazyPrinter) String() string

type PreRunCmd

type PreRunCmd func(*cobra.Command, []string) error

PreRunCmd is the function signature used with a cobra.Command.PreRunE or PersistentPreRunE. Use ChainPreRuns to apply multiple PreRunCmds.

func ChainPreRuns

func ChainPreRuns(preRuns ...PreRunCmd) PreRunCmd

ChainPreRuns chains a list of PreRunCmd functions into a single PreRunCmd. This can be used to set config sources and precedence by chaining [PreRunBindEnvAllSections], [PreRunBindEnvMatching], and [PreRunBindConfigFile]. Use [BindDefaults] to initialize with defaults from a config struct.

Jump to

Keyboard shortcuts

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