commands

package
v0.0.0-...-3518944 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2017 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package commands provides functions for registering and executing administrative commands.

To create a new command, start by writing its function. Command functions take the same form as requests handlers e.g.:

func MyCommand(ctx *app.Context)

Then register it using Register() or MustRegister().

 func init() {
	    commands.MustRegister(MyCommand)
 }

This will create a command named my-comand. To specify additional command options, like its name or the flags it might accept, use the Option parameter. See the documentation on the Option functions on this package for information on each one of them, alternatively, check the example down this page.

If you're using gnd.la/app.App.ListenAndServe or gnd.la/app.App.MustListenAndServe, then you don't need to do anything else, since those functions will check if a command was provided, run it and exit. Alternatively, you can also call commands.Execute with a gnd.la/app.App instance manually if you're not using the functions previously mentioned or, if for some reason, you want to check for commands sooner. e.g.

 func main() {
	// Set up ORM, config etc...
	config.MustParse()
	a := app.New()
	// Set up context processors and finalizers, etc...
	// Now check if there's a command and run it
	if !commands.Execute(a) {
	    // No command supplied. Set up your handlers and
	    // start listening.
	    something := anExpensiveCalculationWhichTakesALotOfTime()
	    a.Handle("^/hello/$", HelloHandler)
	    a.MustListenAndServe(-1)
	}
	// Command was executed. Now just exit.
 }

Commands might use the context methods ParamValue() to access flags values. Methods built on top of ParamValue(), like ParseParamValue(), are also supported. Any additional non-flag arguments are passed to the command handler and might be accessed using IndexValue() (0 represents the first non-flag argument). ParseIndexValue() and related methods are also supported.

 commands.MustRegister(FlagsCommand,
	    commands.Help("This command does nothing interesting"),
	    commands.IntFlag("foo", 0, "Help for foo flag"),
     commands.BoolFlag("bar", false, "Help for bar flag")),
 )

 func FlagCommand(ctx *app.Context) {
	var foo int
	var bar bool
	ctx.ParseParamValue(&foo, "foo")
	ctx.ParseParamValue(&bar, "bar")
	// foo and bar now contain the parameters received in the command line
 }

Finally, to invoke the command, pass it to your app binary e.g.

./myapp my-command

Keep in mind that any flags parsed by your application or the Gondola config package must come before the command name.

./myapp -config=conf/production.conf my-command -mycommandflag=7

To list all the available commands together with their respective help, use the help command:

./myapp help

NOTE: These examples assume a UNIX environment. If you're using Windows type "myapp.exe" rather than "./myapp".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(args ...interface{})

Error stops the command and prints the given error.

func Errorf

func Errorf(format string, args ...interface{})

Errorf works like Error, but accepts a format parameter.

func Execute

func Execute(a *app.App) (bool, error)

Execute tries to run a command reading the parameters from the command line. It returs true if a command was executed and false if it wasn't. Note that most users won't need to call this function directly, since gndl.la/app.App will automatically call it before listening (and exit after executing the command if it was provided).

func MustRegister

func MustRegister(f app.Handler, opts ...Option)

MustRegister works like Register, but panics if there's an error

func Register

func Register(f app.Handler, opts ...Option) error

Register registers a new command with the given function and options.

func Remove

func Remove(name string)

Remove eliminates a previously registered command.

func UsageError

func UsageError(args ...interface{})

UsageError stops the command and prints the given error followed by the command usage.

func UsageErrorf

func UsageErrorf(format string, args ...interface{})

UsageErrorf works like UsageError, but accepts a format parameter.

Types

type Option

type Option func(opts options) options

Option is a function type which sets one or several command options. Use the Option implementations in this package.

func BoolFlag

func BoolFlag(name string, def bool, help string) Option

BoolFlag adds a flag of type bool with the given name, default value and help.

func Help

func Help(help string) Option

Help sets help string that will be printed for a command.

func IntFlag

func IntFlag(name string, def int, help string) Option

IntFlag adds a flag of type int with the given name, default value and help.

func Name

func Name(name string) Option

Name sets the name of the command. If no name is provided, it will be obtained from the function name, transforming its name from camel case to words separated by a '-'

func StringFlag

func StringFlag(name string, def string, help string) Option

StringFlag adds a flag of type string with the given name, default value and help.

func Usage

func Usage(usage string) Option

Usage sets the command usage help string. Usage is printed just after the help, prepending the command to it.

Jump to

Keyboard shortcuts

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