CLI
A simple package for building command line applications in Go. The API is influenced by https://github.com/urfave/cli package, but it is way more flexible. It provides the following features:
- Data conversion that allow conversion of data to a compatible data type accepted by the declared flag
- Data providers that allow setting the flag's value from different sources such as environment variables, files, AWS S3 and SSM
- More extensible flag types such as URL, IP, JSON, YAML and so on. For more information see the docs
Installation
Make sure you have a working Go environment. Go version 1.13.x is supported.
See the install instructions for Go.
To install CLI, simply run:
$ go get github.com/phogolabs/cli
Getting Started
import (
"os"
"syscall"
"github.com/phogolabs/cli"
)
var flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Application Config",
EnvVar: "APP_CONFIG",
FilePath: "/etc/app/app.config",
Required: true,
},
}
func main() {
app := &cli.App{
Name: "prana",
HelpName: "prana",
Usage: "Golang Database Manager",
UsageText: "prana [global options]",
Version: "1.0-beta-04",
Flags: flags,
Action: run,
Signals: []os.Signal{syscall.SIGTERM},
OnSignal: signal,
}
app.Run(os.Args)
}
// run executes the application
func run(ctx *cli.Context) error {
fmt.Println("Application started")
return nil
}
// signal handles OS signal
func signal(ctx *cli.Context, signal os.Signal) error {
fmt.Println("Application signal", signal)
return nil
}Validation
You can set the Required field to true if you want to make some flags
mandatory. If you need some customized validation, you can create a custom
validator in the following way:
As a function:
validate := cli.ValidatorFunc(func(ctx *cli.Context, value interface{}) error {
//TODO: your validation logic
return nil
})As a struct that has a Validate function:
type Validator struct {}
func (v *Validator) Validate(ctx *cli.Context, value interface{}) error {
//TODO: your validation logic
return nil
}Then you can set the validator like that:
var flags = []cli.Flag{
&cli.StringSliceFlag{
Name: "endpoint",
Usage: "Endpoint URL",
EnvVar: "APP_ENDPOINT",
Required: true,
Validator: validate,
},
&cli.StringFlag{
Name: "name",
EnvVar: "APP_NAME",
Validator: &Validator{},
},
}Providers
The providers allow setting the flag's value from external sources:
- AWS S3 Provider - reads a flag's value from AWS S3 bucket object
- AWS SSM Provider - reads a flag's value from AWS SSM Parameter store
Contributing
We are open for any contributions. Just fork the project.