README
¶
cobrax
A utility library for spf13/cobra, spf13/viper and spf13/afero.
Usage
var version, commit, date string // main.version, main.commit, main.date
cmd := cobrax.NewRoot(viper.New())
cmd.Use = "app"
cmd.Short = "description of app"
cmd.Version = cobrax.VersionFunc(version, commit, date)
cmd.AddCommand(someCmd)
cmd.AddCommand(otherCmd)
cmd.Execute()
// Open the file. When pipe is used and the filename is empty, read from stdin.
cobrax.OpenOrStdIn(viper.GetString("filename"), afero.NewOsFs())
// Read the config file and set the values to the viper.
cobrax.BindConfigs(v, "app")
License
This tool is licensed under the MIT License. See the LICENSE file for details.
Links
Documentation
¶
Index ¶
- Variables
- func BindConfigs(v *viper.Viper, rootCmdName string, opts ...ConfigOption) error
- func DebugViper(v *viper.Viper) string
- func GetFlags(cmd *cobra.Command) map[string]any
- func KebabToSnake(f *pflag.FlagSet, name string) pflag.NormalizedName
- func NewRoot(v *viper.Viper) *cobra.Command
- func NewRootWithOption(v *viper.Viper, option RootFlagOption) *cobra.Command
- func OpenOrStdIn(filename string, fs afero.Fs, opts ...OptionFunc) (io.ReadCloser, error)
- func PrintConfig(w io.Writer, m map[string]any, format PrintConfigFormat) error
- func PrintConfigCmd(name string) *cobra.Command
- func RootPersistentPreRunE(cmd *cobra.Command, v *viper.Viper, _ afero.Fs, _ []string) error
- func SetLogger(l *slog.Logger)
- func SnakeToKebab(f *pflag.FlagSet, name string) pflag.NormalizedName
- func VerbosityLevel(v *viper.Viper, opts ...VerbosityLevelOption) slog.Level
- func VersionFunc(version, commit, date string) string
- func WithManualInputEnabled(o *Option)
- type ConfigOption
- func WithConfigFileFlag(cmd *cobra.Command, flagName string) ConfigOption
- func WithConfigFileName(file string) ConfigOption
- func WithConfigFilePaths(paths ...string) ConfigOption
- func WithMergeConfig(merge bool) ConfigOption
- func WithOverrideBy(key string) ConfigOption
- func WithOverrideDisabled() ConfigOption
- type ConfigOptions
- type FlagOption
- type Option
- type OptionFunc
- type PrintConfigFormat
- type RootFlagOption
- type VerbosityLevelOption
- type VerbosityLevelOptions
Constants ¶
This section is empty.
Variables ¶
var DefaultRootFlagOption = RootFlagOption{ Config: FlagOption{Name: "config", Shorthand: "", Usage: "configuration `filename`"}, NoColor: FlagOption{Name: "no-color", Shorthand: "", Usage: "disable colorized output"}, Verbose: FlagOption{Name: "verbose", Shorthand: "v", Usage: "More output per occurrence. (e.g. -vvv)"}, Quiet: FlagOption{Name: "quiet", Shorthand: "q", Usage: "Silence all output"}, }
var ErrNoFileSpecified = errors.New("no file specified")
Functions ¶
func BindConfigs ¶ added in v0.3.0
func BindConfigs(v *viper.Viper, rootCmdName string, opts ...ConfigOption) error
func DebugViper ¶ added in v0.1.0
func KebabToSnake ¶ added in v0.6.2
func KebabToSnake(f *pflag.FlagSet, name string) pflag.NormalizedName
KebabToSnake normalizes flag names from kebab-case to snake_case.
func NewRoot ¶ added in v0.1.2
NewRoot returns the base command used when called without any subcommands
func NewRootWithOption ¶ added in v0.1.3
func NewRootWithOption(v *viper.Viper, option RootFlagOption) *cobra.Command
NewRootWithOption returns the base command used when called without any subcommands
func OpenOrStdIn ¶ added in v0.1.0
func OpenOrStdIn(filename string, fs afero.Fs, opts ...OptionFunc) (io.ReadCloser, error)
func PrintConfig ¶ added in v0.5.0
func PrintConfigCmd ¶ added in v0.6.0
func RootPersistentPreRunE ¶ added in v0.1.2
func SnakeToKebab ¶ added in v0.1.5
func SnakeToKebab(f *pflag.FlagSet, name string) pflag.NormalizedName
SnakeToKebab normalizes flag names from snake_case to kebab-case.
func VerbosityLevel ¶ added in v0.1.2
func VerbosityLevel(v *viper.Viper, opts ...VerbosityLevelOption) slog.Level
func VersionFunc ¶ added in v0.1.0
VersionFunc returns the version string. https://goreleaser.com/cookbooks/using-main.version/
func WithManualInputEnabled ¶ added in v0.2.0
func WithManualInputEnabled(o *Option)
Types ¶
type ConfigOption ¶ added in v0.1.1
type ConfigOption func(*ConfigOptions)
func WithConfigFileFlag ¶ added in v0.3.0
func WithConfigFileFlag(cmd *cobra.Command, flagName string) ConfigOption
func WithConfigFileName ¶ added in v0.3.0
func WithConfigFileName(file string) ConfigOption
func WithConfigFilePaths ¶ added in v0.3.0
func WithConfigFilePaths(paths ...string) ConfigOption
func WithMergeConfig ¶ added in v0.3.0
func WithMergeConfig(merge bool) ConfigOption
func WithOverrideBy ¶ added in v0.3.0
func WithOverrideBy(key string) ConfigOption
func WithOverrideDisabled ¶ added in v0.3.0
func WithOverrideDisabled() ConfigOption
type ConfigOptions ¶ added in v0.3.0
type ConfigOptions struct {
// contains filtered or unexported fields
}
type FlagOption ¶ added in v0.1.3
type OptionFunc ¶ added in v0.2.0
type OptionFunc func(*Option)
func WithStdin ¶ added in v0.2.0
func WithStdin(stdin io.Reader) OptionFunc
type PrintConfigFormat ¶ added in v0.5.0
type PrintConfigFormat string
const ( YAML PrintConfigFormat = "yaml" JSON PrintConfigFormat = "json" TOML PrintConfigFormat = "toml" )
func (*PrintConfigFormat) Set ¶ added in v0.6.0
func (f *PrintConfigFormat) Set(v string) error
Set must have pointer receiver so it doesn't change the value of a copy
func (*PrintConfigFormat) String ¶ added in v0.6.0
func (f *PrintConfigFormat) String() string
String is used both by fmt.Print and by Cobra in help text
func (*PrintConfigFormat) Type ¶ added in v0.6.0
func (f *PrintConfigFormat) Type() string
Type is only used in help text
type RootFlagOption ¶ added in v0.1.3
type RootFlagOption struct { Config FlagOption NoColor FlagOption Verbose FlagOption Quiet FlagOption }
type VerbosityLevelOption ¶ added in v0.5.0
type VerbosityLevelOption func(*VerbosityLevelOptions)
func WithQuietName ¶ added in v0.5.0
func WithQuietName(quietName string) VerbosityLevelOption
func WithVerbosityName ¶ added in v0.5.0
func WithVerbosityName(verboseName string) VerbosityLevelOption
func WithVerbosityStep ¶ added in v0.5.0
func WithVerbosityStep(step int) VerbosityLevelOption
func WithVerbosityZeroLevel ¶ added in v0.5.0
func WithVerbosityZeroLevel(level slog.Level) VerbosityLevelOption
type VerbosityLevelOptions ¶ added in v0.5.0
type VerbosityLevelOptions struct {
// contains filtered or unexported fields
}