app

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2017 License: Apache-2.0 Imports: 17 Imported by: 253

Documentation

Overview

Package app provides an opinionated common infrastructure to application structure.

Index

Constants

View Source
const (
	// FatalSeverity Is the level at which logging causes panics.
	FatalSeverity = log.Fatal
)

Variables

View Source
var (
	// Name is the full name of the application
	Name string
	// ExitFuncForTesting can be set to change the behaviour when there is a command line parsing failure.
	// It defaults to os.Exit
	ExitFuncForTesting = os.Exit
	// ShortHelp should be set to add a help message to the usage text.
	ShortHelp = ""
	// ShortUsage is usage text for the additional non-flag arguments.
	ShortUsage = ""
	// UsageFooter is printed at the bottom of the usage text
	UsageFooter = ""
	// Version holds the version specification for the application.
	// The default version is the one defined in version.cmake.
	// If valid a command line option to report it will be added automatically.
	Version VersionSpec
	// Restart is the error to return to cause the app to restart itself.
	Restart = fault.Const("Restart")
)
View Source
var (
	// CleanupTimeout is the time to wait for all cleanup signals to fire when shutting down.
	CleanupTimeout = time.Second
)

Functions

func AddCleanup

func AddCleanup(ctx context.Context, f func())

AddCleanup calls f when the context is cancelled. Application will wait (for a maximum of CleanupTimeout) for f to complete before terminiating the application.

func AddCleanupSignal

func AddCleanupSignal(s ...task.Event)

AddCleanupSignal adds a signal the app should wait on when shutting down. The signal will automatically be dropped when it is fired, no need unregister it.

func Run

func Run(main task.Task)

Run performs all the work needed to start up an application. It parsers the main command line arguments, builds a primary context that will be cancelled on exit runs the provided task, cancels the primary context and then waits for either the maximum shutdown delay or all registered signals whichever comes first.

func Usage

func Usage(ctx context.Context, message string, args ...interface{})

Usage prints message with the formatting args to stderr, and then prints the command usage information and terminates the program.

func VerbMain

func VerbMain(ctx context.Context) error

VerbMain is a task that can be handed to Run to invoke the verb handling system.

func WaitForCleanup

func WaitForCleanup(ctx context.Context) bool

WaitForCleanup waits for all the cleanup signals to fire, or the cleanup timeout to expire, whichever comes first.

Types

type Action

type Action interface {
	// Run executes the action.
	Run(ctx context.Context, flags flag.FlagSet) error
}

Action is the interface for verb actions that can be run. Exported fields will be exposed as flags for the verb. Use the `help` tag to expose a flag description.

type AppFlags

type AppFlags struct {
	Version bool `help:"_display the application version"`
	Log     LogFlags
	Profile ProfileFlags
}

type ExitCode

type ExitCode int

ExitCode is the type for named return values from the application main entry point.

const (
	// SuccessExit is the exit code for succesful exit.
	SuccessExit ExitCode = iota
	// FatalExit is the exit code if something logs at a fatal severity (critical or higher by default)
	FatalExit
	// UsageExit is the exit code if the usage function was invoked
	UsageExit
	// RestartExit is the exit code if the app should be restarted.
	RestartExit
)

type LogFlags

type LogFlags struct {
	Level  log.Severity `help:"_The severity to enable logs at"`
	Style  log.Style    `help:"_The style to use when printing the log"`
	Stacks bool         `help:"_If true, stack traces are logged for all errors"`
	File   string       `help:"_The file to store the logs in"`
}

type ProfileFlags

type ProfileFlags struct {
	CPU string `help:"_write cpu profile to file"`
}

type Verb

type Verb struct {
	Name       string // The name of the command
	ShortHelp  string // Help for the purpose of the command
	ShortUsage string // Help for how to use the command
	Action     Action // The verb's action. Must be set.
	// contains filtered or unexported fields
}

Verb holds information about a runnable api command.

func AddVerb

func AddVerb(v *Verb) *Verb

AddVerb adds a new verb to the supported set, it will panic if a duplicate name is encountered. v is returned so the function can be used in a fluent-style.

func FilterVerbs

func FilterVerbs(prefix string) (result []*Verb)

FilterVerbs returns the filtered list of verbs who's names match the specified prefix.

func (*Verb) Add

func (v *Verb) Add(child *Verb)

Add adds a new verb to the supported set, it will panic if a duplicate name is encountered.

func (*Verb) Filter

func (v *Verb) Filter(prefix string) (result []*Verb)

Filter returns the filtered list of verbs who's names match the specified prefix.

func (*Verb) Invoke

func (v *Verb) Invoke(ctx context.Context, args []string) error

Invoke runs a verb, handing it the command line arguments it should process.

type VersionSpec

type VersionSpec struct {
	// Major version, the version structure is in valid if <0
	Major int
	// Minor version, not used if <0
	Minor int
	// Point version, not used if <0
	Point int
	// The build identifier, not used if an empty string
	Build string
}

VersionSpec is the structure for the version of an application.

func (VersionSpec) Format

func (v VersionSpec) Format(f fmt.State, c rune)

Format implements fmt.Formatter to print the version.

func (VersionSpec) IsValid

func (v VersionSpec) IsValid() bool

IsValid reports true if the VersionSpec is valid, ie it has a Major version.

Directories

Path Synopsis
Package auth provides simple token based, stream authorization functions.
Package auth provides simple token based, stream authorization functions.
Package benchmark provides benchmarking facilities to the standard application system.
Package benchmark provides benchmarking facilities to the standard application system.
Package flag provides extended support for flag parsing.
Package flag provides extended support for flag parsing.
Package layout is used to find parts of the application package to load or run.
Package layout is used to find parts of the application package to load or run.

Jump to

Keyboard shortcuts

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