cobrax

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 14 Imported by: 4

README

cobrax

CI Status Go Reference Go report

A utility library for spf13/cobra, spf13/viper and spf13/afero.

Usage

var version string // main.version

cmd := cobrax.NewRoot(viper.New())
cmd.Use = "app"
cmd.Short = "description of app"
cmd.Version = cobrax.VersionFunc(version, "", "")

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRootFlagOption = RootFlagOption{
	Version: FlagOption{Name: "version", Shorthand: "V", Usage: "Print version information and quit"},
	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"},
}
View Source
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 DebugViper(v *viper.Viper) string

func NewRoot added in v0.1.2

func NewRoot(v *viper.Viper) *cobra.Command

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 RootPersistentPreRunE added in v0.1.2

func RootPersistentPreRunE(cmd *cobra.Command, v *viper.Viper, _ afero.Fs, _ []string) error

func SetLogger added in v0.1.0

func SetLogger(l *slog.Logger)

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 VerbosityCount added in v0.1.2

func VerbosityCount(v *viper.Viper) int

func VerbosityLevel added in v0.1.2

func VerbosityLevel(v *viper.Viper) slog.Level

func VersionFunc added in v0.1.0

func VersionFunc(version, commit, date string) string

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 FlagOption struct {
	Name      string
	Shorthand string
	Usage     string
}

type Option added in v0.2.0

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

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 RootFlagOption added in v0.1.3

type RootFlagOption struct {
	Version FlagOption
	Config  FlagOption
	NoColor FlagOption
	Verbose FlagOption
	Quiet   FlagOption
}

Jump to

Keyboard shortcuts

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