modulepackage
0.0.0-20230603062139-253a2f5d33f0
Repository: https://github.com/nextlinux/fangs.git
Documentation: pkg.go.dev
# README
fangs
A library that makes integrating Cobra and Viper simpler and more consistent.
Background
NeXTLinux Go-based CLI tools use Cobra and Viper for building the basic CLI handling and configuration, respectively. The use of these tools has evolved over the years and some patterns have emerged that seem to work better than others and avoid some pitfalls.
This library uses some best practices we've found for integrating these tools together in fairly simple ways.
Usage
In order to use this library, a consumer will need to:
- Define configuration structs
- By default, use
yaml
struct tags (can revert this tomapstructure
in theConfig
) - For embedded structs to be inline, these must use the nonstandard
,squash
option
- By default, use
- Define Cobra commands
- Add flags to Cobra using the
*Var*
flag variants - Call
config.Load
during command invocation
A number of examples can be seen in the tests, but a simple example is as follows:
// define configuration structs:
type Options struct {
Output string `yaml:"output"`
Scanning ScanningOptions `yaml:"scanning"`
EmbeddedOptions `yaml:",inline,squash"` // need to use ,squash
}
type ScanningOptions struct {
Depth int `yaml:"output"`
}
type EmbeddedOptions struct {
Embedded string `yaml:"string"`
}
// fangs needs a configuration with a minimum of an app name
cfg := config.NewConfig("my-app")
// in a cobra configuration function:
func makeCommand(cfg config.Config) cobra.Command {
// an instance of options with defaults we use to add flags and configure
opts := Options{
Output: "default",
Scanning: ScanningOptions {
Depth: 1,
},
}
// make a cobra command with the options you need
cmd := cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
// before using opts, call config.Load with the cmd instance,
// after flags have been added
err := config.Load(cfg, cmd, &opts)
// ...
},
}
// add flags like normal, making sure to use the *Var* variants
flags := cmd.Flags()
flags.StringVarP(&opts.Output, "output", "o", opts.Output, "output usage")
flags.IntVar(&opts.Scanning.Depth, "depth", opts.Scanning.Depth, "depth usage")
return cmd
}
# Functions
AddFlags traverses the object graphs from the structs provided and calls all AddFlags methods implemented on them.
BoolPtrVarP adds a boolean pointer flag with no default.
No description provided by the author
FindConfigYamlInCwd looks for ./config.yaml -- NOTE: this is not part of the default behavior.
FindDirect attempts to find a directly configured cfg.File.
FindInAppNameSubdir looks for ./.<appname>/config.<ext>.
FindInCwd looks for ./.<appname>.<ext>.
FindInHomeDir looks for ~/.<appname>.<ext>.
FindInXDG looks for <appname>/config.yaml in xdg locations, starting with xdg home config dir then moving upwards.
No description provided by the author
IntPtrVarP adds an int pointer flag with no default.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewStructDescriptionTagProvider returns a DescriptionProvider that returns "description" field tag values.
StringPtrVarP adds a string pointer flag with no default.
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author
FieldDescriber a struct implementing this interface will have DescribeFields called when Summarize is called.
FieldDescriptionSet accepts field descriptions.
FieldDescriptionSetProvider implements both DescriptionProvider and FieldDescriptionSet.
FlagAdder interface can be implemented by structs in order to add flags when AddFlags is called.
FlagSet defines effectively a subset of the methods exposed by pflag.FieldSet, as fangs requires all flag add calls to use field references in order to match reading configuration and summarization information.
PostLoad is the interface used to do any sort of processing after `config.Load` has been called.
# Type aliases
No description provided by the author