gocmder

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

README

Go CMDER

Go Cmder combine Cobra and Viper to auto generate command-line tool where the user can control configuration in a precedence order: flags > environment variables > configuration files and the defaults defined by the config.

This library was inspired by Sting of the viper article from Carolyn Van Slyck.

Build codecov Go Report Card License

Install

go get github.com/ergagnon/gocmder

How to use

Create the config struct with tags

Supported tags:

  1. desc: used for the Flag description.
  2. default: default value used in Flag and Viper config. Supported value type: string, int, float32, bool
  3. required: set a required Flag.
  4. hidden: don't create a Flag when hidden is true.

Example:

type AppConfig struct {
    Directory string `desc:"Directory to browse" default:"."`
    Server ServerConfig // support multi-level config          
}

type ServerConfig struct {
    Url string `desc:"Server url" default:"localhost"`
    Port int `desc:"Username" default:"8080"`
}
Create a Go CMDER with the config and options

Call the function

func NewCmder(cfg any, onFinalize OnFinalizeFunc, opts ...CmderOption) (*Cmder, error)
cli, err := gocmder.NewCmder(AppConfig{}, func(cfg any) error {
    // At this step, the config object has been filled
    // with all the values. 
    // You can use it in your application.
    return nil
})

This will auto-generate

Flags:

Usage:
   [flags]

Flags:
      --directory string    Directory to browse (default ".")
  -h, --help                help for this command
      --server-port int     Username (default 8080)
      --server-url string   Server url (default "localhost")

Environment Variable

DIRECTORY
SERVER_PORT
SERVER_URL

Config file (optional)
Configuration will use Viper defaults.

reading from JSON, TOML, YAML, HCL, envfile and Java properties config files

Yaml example:

directory: "my_dir"
server:
  url: "127.0.0.1"
  port: 8080

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmder

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

func NewCmder

func NewCmder(cfg any, onFinalize OnFinalizeFunc, opts ...CmderOption) (*Cmder, error)

NewCmder creates a new Cmder instance. It takes a config struct, a callback function when the command is finalized and a variadic list of options.

func (*Cmder) Cobra

func (c *Cmder) Cobra() *cobra.Command

Returns the Cobra instance.

func (*Cmder) Execute

func (c *Cmder) Execute() error

Executes the command.

func (*Cmder) Viper

func (c *Cmder) Viper() *viper.Viper

Returns the Viper instance.

type CmderOption

type CmderOption func(*Cmder)

func WithConfigFile

func WithConfigFile(file string) CmderOption

WithConfigFile sets the config file to use for the command. explicitly defines the path, name and extension of the config file.

func WithFS

func WithFS(fs afero.Fs) CmderOption

WithFS sets the filesystem to use for the command. This is useful for testing.

func WithLongDesc

func WithLongDesc(long string) CmderOption

WithLongDesc sets the long description for the command. This is used by the "help" command.

func WithPrefix

func WithPrefix(prefix string) CmderOption

WithPrefix sets the prefix for environment variables. For example, if the prefix is "APP", then the environment variable "APP_DEBUG" will be used to set the value of the flag "--debug".

func WithShortDesc

func WithShortDesc(short string) CmderOption

WithShortDesc sets the short description for the command. This is used by the "help" command.

func WithVersion

func WithVersion(version string) CmderOption

WithVersion sets the version string for the command. This is used by the "version" command.

type OnFinalizeFunc

type OnFinalizeFunc func(cfg any)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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