app

package
v0.0.0-...-7ee513c Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package app provides an opinionated common infrastructure to application structure.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Name is the full name of the application
	Name string
	// Flags is the main application flags.
	Flags AppFlags
	// 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 * 10
)
View Source
var LogHandler log.Indirect

LogHandler is the primary application logger target. It is assigned to the main context on startup and is closed on shutdown.

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 AddInterruptHandler

func AddInterruptHandler(f func()) func()

AddInterruptHandler adds a function that will be called on the next interrupt. It returns a function that can be called to remove the value from the list

func MakeDoc

func MakeDoc(ctx context.Context)

MakeDocs

func Run

func Run(main task.Task)

Run wraps doRun in order to let doRun use deferred functions. This is because os.Exit does not execute deferred functions.

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
	Analytics   string `help:"_If non-empty enable analytics using the specified user-id"`
	CrashReport bool   `help:"_Automatically send crash reports to Google"`
	DecodeStack string `help:"_Decode a stackdump generated by this executable"`
	FullHelp    bool   `help:"_Display the full help"`
	Args        string `help:"_A single string that will be parsed into extra individual arguments"`
}

type Cleanup

type Cleanup func(ctx context.Context)

Cleanup is a function that is invoked at a later time to perform the cleanup.

func (Cleanup) Invoke

func (c Cleanup) Invoke(ctx context.Context) Cleanup

Invoke invokes the possibly nil cleanup safely. Returns a nil Cleanup, so this can be chained when invoking the cleanup as part of the error handling.

func (Cleanup) Then

func (c Cleanup) Then(next Cleanup) Cleanup

Then combines two clean up functions into a single cleanup function.

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
)

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"`
	Status bool         `help:"_Log status updates as they happen"`
}

type ProfileFlags

type ProfileFlags struct {
	CPU   string `help:"_write cpu profile to file"`
	Mem   string `help:"_write mem profile to file"`
	Trace string `help:"_write a trace to file"`
	Pprof bool   `help:"_enable pprof profiling"`
}

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 VersionSpecFromTag

func VersionSpecFromTag(tag string) (VersionSpec, error)

VersionSpecFromTag parses the version from a git tag name.

func (VersionSpec) Equal

func (v VersionSpec) Equal(o VersionSpec) bool

Equal returns true if v is equal to o, ignoring the Build field.

func (VersionSpec) Format

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

Format implements fmt.Formatter to print the version.

func (VersionSpec) GetDevVersion

func (v VersionSpec) GetDevVersion() int

GetDevVersion returns the dev version, or -1 if no dev version is found.

func (VersionSpec) GreaterThan

func (v VersionSpec) GreaterThan(o VersionSpec) bool

GreaterThan returns true if v is greater than o.

func (VersionSpec) GreaterThanDevVersion

func (v VersionSpec) GreaterThanDevVersion(o VersionSpec) bool

GreaterThanDevVersion returns true if v is greater than o, respecting dev versions.

func (VersionSpec) IsValid

func (v VersionSpec) IsValid() bool

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

func (VersionSpec) String

func (v VersionSpec) String() string

Directories

Path Synopsis
param
Package param holds Google Analytics parameter names.
Package param holds Google Analytics parameter names.
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 crash provides functions for reporting application crashes (uncaught panics).
Package crash provides functions for reporting application crashes (uncaught panics).
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.
Package linker provides functions for findings addresses of app functions by name.
Package linker provides functions for findings addresses of app functions by name.
Package status provides methods for reporting application status.
Package status provides methods for reporting application status.

Jump to

Keyboard shortcuts

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