Documentation
¶
Overview ¶
Package cmd implements cmd helper.
This provides helpers on top of `github.com/urfave/cli`.
Example usage:
var c *cli.Context // Get this from your action
ctx, err := cmd.NewContext(c)
if err != nil {
// Handle error
}
ctx.Logger() // Get your logger
ctx.Statter() // Get your statter
<-cmd.WaitForSignals()
Index ¶
Examples ¶
Constants ¶
const ( FlagPort = "port" FlagLogFormat = "log.format" FlagLogLevel = "log.level" FlagLogTags = "log.tags" FlagStatsDSN = "stats.dsn" FlagStatsPrefix = "stats.prefix" FlagStatsTags = "stats.tags" )
Flag constants declared for CLI use.
Variables ¶
var CommonFlags = Flags{}.Merge(LogFlags, StatsFlags)
CommonFlags are flags that configure logging and stats.
Common flags include LogFlags and StatsFlags.
var LogFlags = Flags{ &cli.StringFlag{ Name: FlagLogFormat, Usage: "Specify the format of logs. Supported formats: 'logfmt', 'json'", EnvVars: []string{"LOG_FORMAT"}, }, &cli.StringFlag{ Name: FlagLogLevel, Value: "info", Usage: "Specify the log level. E.g. 'debug', 'warning'.", EnvVars: []string{"LOG_LEVEL"}, }, &cli.StringSliceFlag{ Name: FlagLogTags, Usage: "A list of tags appended to every log. Format: key=value.", EnvVars: []string{"LOG_TAGS"}, }, }
LogFlags are flags that configure logging.
var ServerFlags = Flags{ &cli.StringFlag{ Name: FlagPort, Value: "80", Usage: "Port for HTTP server to listen on", EnvVars: []string{"PORT"}, }, }
ServerFlags are flags that configure a server.
var StatsFlags = Flags{ &cli.StringFlag{ Name: FlagStatsDSN, Usage: "The URL of a stats backend.", EnvVars: []string{"STATS_DSN"}, }, &cli.StringFlag{ Name: FlagStatsPrefix, Usage: "The prefix of the measurements names.", EnvVars: []string{"STATS_PREFIX"}, }, &cli.StringSliceFlag{ Name: FlagStatsTags, Usage: "A list of tags appended to every measurement. Format: key=value.", EnvVars: []string{"STATS_TAGS"}, }, }
StatsFlags are flags that configure stats.
Functions ¶
func SplitTags ¶
SplitTags splits a slice of strings into a slice using the given separator.
Example ¶
package main
import (
"fmt"
"github.com/hamba/cmd"
)
func main() {
input := []string{"a=b", "foo=bar"} // Usually from a cli.StringSlice
tags, err := cmd.SplitTags(input, "=")
if err != nil {
// Handle error
}
fmt.Println(tags)
}
Output: [a b foo bar]
func WaitForSignals ¶
WaitForSignals waits for SIGINT or SIGTERM signals.
Example ¶
package main
import (
"github.com/hamba/cmd"
)
func main() {
<-cmd.WaitForSignals() // Will wait for SIGTERM or SIGINT
}
Types ¶
type Context ¶
type Context struct {
*cli.Context
// contains filtered or unexported fields
}
Context represents an application context.
Context implements both log.Loggable and stats.Statable.
func NewContext ¶
NewContext creates a new Context from the CLI Context.
Example ¶
var c *cli.Context // Get this from your action
ctx, err := cmd.NewContext(c)
if err != nil {
// Handle error
}
ctx.Logger() // Get your logger
ctx.Statter() // Get your statter
<-cmd.WaitForSignals()
func (*Context) AttachLogger ¶
AttachLogger attaches a Logger to the Context.
func (*Context) AttachStatter ¶
AttachStatter attaches a Statter to the Context.