Documentation
¶
Index ¶
- Constants
- Variables
- func BindDebugFlag(cmd *cobra.Command)
- func BindDefaultsTo(cfg interface{}, tag string, k *koanf.Koanf) error
- func BindPostgresFlags(cmd *cobra.Command, dbCfg *config.DBConfig)
- func BindRootDir(cmd *cobra.Command, defaultVal, desc string) *string
- func BindRootDirVar(cmd *cobra.Command, rootDir *string, defaultVal, desc string)
- func EnableCLIDebugging()
- func GetPostgresFlags(cmd *cobra.Command, dbCfg *config.DBConfig) (*pg.ConnConfig, error)
- func MaybeEnableCLIDebug(cmd *cobra.Command, args []string) error
- func MergePostgresFlags(conf *pg.ConnConfig, cmd *cobra.Command) (*pg.ConnConfig, error)
- func PreRunBindEnvMatchingTo(cmd *cobra.Command, args []string, prefix string, k *koanf.Koanf) error
- func RootDir(cmd *cobra.Command) (string, error)
- func SetFlagsFromStruct(fs *pflag.FlagSet, cfg interface{})
- func SetFlagsFromStructTags(fs *pflag.FlagSet, cfg interface{}, nameTag, descTag string)
- type LazyPrinter
- type PreRunCmd
Constants ¶
const (
RootFlagName = "root"
)
Variables ¶
var Debugf = func(string, ...interface{}) {}
Functions ¶
func BindDebugFlag ¶
func BindDefaultsTo ¶
BindDefaultsTo loads the defaults from the provided config struct into a Koanf instance, using field names according to the given struct tag.
func BindPostgresFlags ¶
BindPostgresFlags binds top-level flags to connect to a postgres database. The provided config is used as defaults for the flags.
func BindRootDir ¶
BindRootDir is like BindRootDirVar but the bound variable is internal and returned as a *string, which may be ignored when using RootDir.
func BindRootDirVar ¶
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 ¶
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 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 ¶
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 ¶
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 ¶
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 ¶
PreRunCmd is the function signature used with a cobra.Command.PreRunE or PersistentPreRunE. Use ChainPreRuns to apply multiple PreRunCmds.
func ChainPreRuns ¶
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.