Documentation ¶
Overview ¶
Package util contains utility functions for Panto.
Option ¶
Use Option to define configuration options for your program. This allows quick and consistent bindings for configuration file options, flags and environment variables.
Start by configuring an option for your program.
var nameOption = util.Option{ Name: "name", Default: "World", Usage: "the name of the person you wish to greet", Env: "HELLO_NAME", Flag: "name", Short: "n", }
At init time, add the option to your program.
func init() { // Add name option if err := util.AddOption(nameOption, pflag.CommandLine); err != nil { panic(err) } }
When running your executable, bind your option and parse the flags.
func main() { // Bind env var and flag to viper util.BindOption(nameOption, pflag.CommandLine) // Parse command-line flags pflag.Parse() // Read configuration file viper.SetConfigName("hello") viper.AddConfigPath(".") viper.SetConfigType("yaml") viper.ReadInConfig() fmt.Println("Hello", viper.GetString(nameOption.Name)) }
Example usage:
$ ./hello Hello World $ ./hello -h Usage of ./hello: -n, --name string the name of the person you wish to greet (default "World") pflag: help requested $ ./hello --name="Steve" Hello Steve $ HELLO_NAME="Brooklyn" ./hello Hello Brooklyn $ HELLO_NAME="Brooklyn" ./hello --name="Steve" Hello Steve $ echo 'name: "Sunshine"' > hello.yaml $ ./hello Hello Sunshine
Index ¶
- func AddOption(opt Option, flags *pflag.FlagSet) error
- func Base64Decode(s string) (u uuid.UUID, err error)
- func Base64Encode(u uuid.UUID) string
- func BindOption(opt Option, flags *pflag.FlagSet) error
- func Contains(l []string, s string) bool
- func LiveTest(t *testing.T) bool
- func NextPageToken(req proto.Message, a ...interface{}) (string, error)
- func ValidatePageToken(token string, req proto.Message, a ...interface{}) (bool, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddOption ¶
AddOption adds an option to the program. If opt.Default is set, it sets the default value in viper. If flags is not nil and opt.Flag is set, the option is configuration-only. If opt.Name is empty, and opt.Flag is set, the option is flag-only. Use this when setting up flags and configuration options, typically at init time.
func Base64Decode ¶
Base64Decode decodes a url-safe base64 string with no padding to a UUID.
func Base64Encode ¶
Base64Encode encodes a UUID to a url-safe base64 string with no padding.
func BindOption ¶
BindOption binds the environment variables and flags to viper. Use this when running the executable, typically at the start of a cobra command.
func LiveTest ¶
LiveTest tests if the environment variable for live testing is set. "Live" testing means that the unit tests will rely on an existing instance of the TSDB at localhost:8086
func NextPageToken ¶
NextPageToken creates a `next_page_token` for a paginated response from a request, and adds additional data. `req` must be a protobuf message containing a `PageToken` field. Returns a URL-safe base64 encoded string.
func ValidatePageToken ¶
ValidatePageToken validates a page token against a request. It decodes the given token, verifies it matches the request, and stores the extra data into successive arguments. Arguments must be pointers. Behavior is undefined if the arguments do not match the encoded buffer. Returns true if the token matches the request.
Types ¶
type Option ¶
type Option struct { Name string // The name of the option. Supports viper nesting using dot '.' in names. Default interface{} // The default value of the option. Mandatory, as the default value is used to infer the option type. Usage string // A description of the option. Flag string // The name of the command-line flag. Short string // A shorthand for the flag (optional). Env string // An environment variable to bind this option to (optional). }
Option describes a confifuration option for the program.