cmd

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 17 Imported by: 3

README

Logo

Go Report Card Build Status Coverage Status GoDoc GitHub release GitHub license

Go cmd helper.

This provides helpers on top of github.com/urfave/cli.

Overview

Install with:

go get github.com/hamba/cmd

Example

func yourAction(c *cli.Context) error {
    ctx, err := cmd.NewContext(c)
    if err != nil {
        return err
    }

    // Run your application here...

    <-cmd.WaitForSignals()
}

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

View Source
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

View Source
var CommonFlags = Flags{}.Merge(LogFlags, StatsFlags)

CommonFlags are flags that configure logging and stats.

Common flags include LogFlags and StatsFlags.

View Source
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.

View Source
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.

View Source
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 NewLogger

func NewLogger(c *cli.Context) (logger.Logger, error)

NewLogger creates a new logger.

func NewStats

func NewStats(c *cli.Context, l log.Logger) (stats.Statter, error)

NewStats creates a new statter.

func SplitTags

func SplitTags(slice []string, sep string) ([]string, error)

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

func WaitForSignals() chan os.Signal

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

func NewContext(c *cli.Context) (*Context, error)

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

func (c *Context) AttachLogger(fn func(l log.Logger) log.Logger)

AttachLogger attaches a Logger to the Context.

func (*Context) AttachStatter

func (c *Context) AttachStatter(fn func(s stats.Statter) stats.Statter)

AttachStatter attaches a Statter to the Context.

func (*Context) Close

func (c *Context) Close() error

Close closes the context.

func (*Context) Logger

func (c *Context) Logger() log.Logger

Logger returns the Logger attached to the Context.

func (*Context) Statter

func (c *Context) Statter() stats.Statter

Statter returns the Statter attached to the Context.

type Flags

type Flags []cli.Flag

Flags represents a set of CLI flags.

func (Flags) Merge

func (f Flags) Merge(flags ...Flags) Flags

Merge joins one or more Flags together, making a new set.

Jump to

Keyboard shortcuts

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