Documentation
¶
Index ¶
- Variables
- func FullCodeMap() (m map[ExitCode]string)
- func GetAllExitCodeDescriptions() [^ExitCode(0)]string
- func UserCodeMap() (m map[ExitCode]string)
- type ExitCode
- type ExitTaskFn
- type Exiter
- func (e *Exiter) Exit(code ExitCode, err ...error)
- func (e *Exiter) ExitOnPanic()
- func (e *Exiter) ExitWithCantCreateErr(err ...error)
- func (e *Exiter) ExitWithConfigErr(err ...error)
- func (e *Exiter) ExitWithDataErr(err ...error)
- func (e *Exiter) ExitWithError(err ...error)
- func (e *Exiter) ExitWithIOErr(err ...error)
- func (e *Exiter) ExitWithNoHostErr(err ...error)
- func (e *Exiter) ExitWithNoInputErr(err ...error)
- func (e *Exiter) ExitWithNoPermErr(err ...error)
- func (e *Exiter) ExitWithNoUserErr(err ...error)
- func (e *Exiter) ExitWithOK()
- func (e *Exiter) ExitWithOSErr(err ...error)
- func (e *Exiter) ExitWithOSFile(err ...error)
- func (e *Exiter) ExitWithProtocolErr(err ...error)
- func (e *Exiter) ExitWithSoftwareErr(err ...error)
- func (e *Exiter) ExitWithTempFailErr(err ...error)
- func (e *Exiter) ExitWithUnavailableErr(err ...error)
- func (e *Exiter) ExitWithUsageErr(err ...error)
- func (e *Exiter) ExitWithUsageOK()
- func (e *Exiter) GetCancelableContext() context.Context
- func (e *Exiter) GetSignalCatcher() *sigar.SignalCatcher
- func (e *Exiter) ListenForSignals()
- func (e *Exiter) RegisterPostTask(fn ExitTaskFn)
- func (e *Exiter) RegisterPreTask(fn ExitTaskFn)
- func (e *Exiter) SetUsageFunc(usage ExitTaskFn)
- func (e *Exiter) Wait()
- type ExiterConfig
- type WriteFn
Constants ¶
This section is empty.
Variables ¶
var DefaultExiterConfig = ExiterConfig{ ParentCtx: context.Background(), ExitSignals: []os.Signal{ syscall.SIGINT, syscall.SIGTERM, }, }
DefaultExiterConfig is the ExiterConfig used when calling the NewExiter() factory.
Functions ¶
func FullCodeMap ¶
FullCodeMap returns a map of all ExitCodes and their associated description strings.
func GetAllExitCodeDescriptions ¶
GetAllExitCodeDescriptions returns an array of all possible ExitCode description strings (where the index is the exit code). Any unreserved ExitCodes are also included with empty string descriptions.
func UserCodeMap ¶
UserCodeMap returns a map of ExitCodes and their associated description strings with in the User-Definable range.
Types ¶
type ExitCode ¶
ExitCode represents an unsigned 16 bit integer used to define exit statuses.
const ( ExitOK ExitCode = ExitCode(exit.EX_OK) // 0: successful termination ExitError ExitCode = ExitCode(1) // 1: generic error ExitUsage ExitCode = ExitCode(exit.EX_USAGE) // 64: command line usage error ExitDataErr ExitCode = ExitCode(exit.EX_DATAERR) // 65: data format error ExitNoInput ExitCode = ExitCode(exit.EX_NOINPUT) // 66: cannot open input ExitNoUser ExitCode = ExitCode(exit.EX_NOUSER) // 67: addressee unknown ExitNoHost ExitCode = ExitCode(exit.EX_NOHOST) // 68: host name unknown ExitSoftware ExitCode = ExitCode(exit.EX_SOFTWARE) // 70: internal software error ExitOSErr ExitCode = ExitCode(exit.EX_OSERR) // 71: system error (e.g., can't fork) ExitOSFile ExitCode = ExitCode(exit.EX_OSFILE) // 72: critical OS file missing ExitCantCreate ExitCode = ExitCode(exit.EX_CANTCREAT) // 73: can't create (user) output file ExitIOErr ExitCode = ExitCode(exit.EX_IOERR) // 74: input/output error ExitTempFail ExitCode = ExitCode(exit.EX_TEMPFAIL) // 75: temp failure; user is invited to retry ExitProtocol ExitCode = ExitCode(exit.EX_PROTOCOL) // 76: remote error in protocol ExitNoPerm ExitCode = ExitCode(exit.EX_NOPERM) // 77: permission denied ExitConfig ExitCode = ExitCode(exit.EX_CONFIG) // 78: configuration error ExitAbort ExitCode = ExitCode(exit.EX_ABORT) // 134: process was aborted )
func RegisterNewExitCode ¶
RegisterNewExitCode reserves an ExitCode and stores its corresponding description string for recall. It returns an error if the given integer is already in use or outside the usable range. And also if the description string is empty.
func RegisterNextExitCode ¶
RegisterNextExitCode find the next available ExitCode number and associates the given description string with it. It returns an error if there are no more available numbers to reserve.
type ExitTaskFn ¶
ExitTaskFn (`func()`) is a closure represening something that should be executed right before exiting.
type Exiter ¶
type Exiter struct {
// contains filtered or unexported fields
}
Exiter is a tool for cleanly and consistently exiting the program. Instead of creating the struct directly, use the NewExiter() or NewExiterWithConfig() factories.
func NewExiter ¶
func NewExiter() *Exiter
NewExiter is an Exiter factory that uses the DefaultExiterConfig. Be sure to call (Exiter).ListenForSignals() once created to handle all exits properly.
func NewExiterWithConfig ¶
func NewExiterWithConfig(config ExiterConfig) (e *Exiter)
NewExiterWithConfig is an Exiter factory that allows you to customize the configuration. Be sure to call (Exiter).ListenForSignals() once created to handle all exits properly.
func SpawnExiter ¶
func SpawnExiter() (e *Exiter)
SpawnExiter is a convenience function that calls both NewExiter() and (Exiter).ListenForSignals().
func SpawnExiterWithConfig ¶
func SpawnExiterWithConfig(config ExiterConfig) (e *Exiter)
SpawnExiterWithConfig is a convenience function that calls both NewExiterWithConfig() and (Exiter).ListenForSignals().
func (*Exiter) Exit ¶
ExitWithConfigErr exits with the given ExitCode, and prints the first error provided (if any).
func (*Exiter) ExitOnPanic ¶
func (e *Exiter) ExitOnPanic()
ExitOnPanic is a convenience function that is meant to be defered as a means to gracefully exit in the event of a panic.
func (*Exiter) ExitWithCantCreateErr ¶
ExitWithCantCreateErr calls (Exiter).Exit() with ExitCantCreate (73).
It is recommended to use this only when the creation of a user output file is impossible for any reason and the program is unable to continue.
func (*Exiter) ExitWithConfigErr ¶
ExitWithConfigErr calls (Exiter).Exit() with ExitConfig (78).
It is recommended to use this whenever a schema validation is encountered in a config file or other serialiezed configuarion source.
func (*Exiter) ExitWithDataErr ¶
ExitWithDataErr calls (Exiter).Exit() with ExitDataErr (65).
It is recommended to use this when data provided through any means is invalid but NOT when an argument fails basic validation (though schema errors in complex arguments could be appropriate UNLESS they are configuartion settings, in which case you should use (Exiter).ExitWithConfigErr()).
func (*Exiter) ExitWithError ¶
ExitWithError calls (Exiter).Exit() with ExitError (1).
It is recommended to use this when no other exit code makes sense.
func (*Exiter) ExitWithIOErr ¶
ExitWithIOErr calls (Exiter).Exit() with ExitIOErr (74).
It is recommended to use this only when unable to interact with I/O hardware, failure to open or write to an output stream, or when encountering an interuption from an already open input stream (otherwise use (Exiter).ExitWithNoInputErr()). Permissions errors should use (Exiter).ExitWithNoPermErr() instead.
func (*Exiter) ExitWithNoHostErr ¶
ExitWithNoHostErr calls (Exiter).Exit() with ExitNoHost (68).
It is recommended to use this when a provided hostname cannot be resolved.
func (*Exiter) ExitWithNoInputErr ¶
ExitWithNoInputErr calls (Exiter).Exit() with ExitNoInput (66).
It is recommended to use this only when an input channel (such as a pipe, os.STDIN, or virtual device) cannot be opened or read when needed. Permissions errors should use (Exiter).ExitWithNoPermErr() instead.
func (*Exiter) ExitWithNoPermErr ¶
ExitWithNoPermErr calls (Exiter).Exit() with ExitNoPerm (77).
It is recommended to use this whenever user or credential Permissions do not allow you to continue.
func (*Exiter) ExitWithNoUserErr ¶
ExitWithNoUserErr calls (Exiter).Exit() with ExitNoUser (67).
It is recommended to use this only when attempting to address data to a local or network user, and no suitable match can be found. Mostly useful for multi-user machines or intranet communication not using a protocol that has its own suitable statuses for this.
func (*Exiter) ExitWithOK ¶
func (e *Exiter) ExitWithOK()
ExitWithOK calls (Exiter).Exit() with ExitOK (0).
It is recommended to use this for all normal exit conditions.
func (*Exiter) ExitWithOSErr ¶
ExitWithOSErr calls (Exiter).Exit() with ExitOSErr (71).
It is recommended to use this only when unable to continue due to encountering a generalized system error.
func (*Exiter) ExitWithOSFile ¶
ExitWithOSFile calls (Exiter).Exit() with ExitOSFile (72).
It is recommended to use this only when unable to find a file or directory not owned by this or any othe program but is part of the general system configuration. Such as a standard FHS directory, or a device file in /dev.
func (*Exiter) ExitWithProtocolErr ¶
ExitWithProtocolErr calls (Exiter).Exit() with ExitProtocol (76).
It is recommended to use this only when some communication with a process or network location fails due to a protocol violation from the other communication participant. And example would be receiving a response that is not inline with the protocol (like a SYN or ACK in response to the same instead of a SYN/ACK when performing a TCP handshake).
func (*Exiter) ExitWithSoftwareErr ¶
ExitWithSoftwareErr calls (Exiter).Exit() with ExitSoftware (70).
It is recommended to use this as a more meaningful alternative to ExitWithError() or for more graceful exits in the event of a Panic. For the later case consider deferring (Exiter).ExitOnPanic() which will call this function in that event.
func (*Exiter) ExitWithTempFailErr ¶
ExitWithTempFailErr calls (Exiter).Exit() with ExitTempFail (75).
It is recommended to use this when the program chooses (by design) to terminate instead of retrying some operation that is expected to have failed merely intermittently.
func (*Exiter) ExitWithUnavailableErr ¶
ExitWithUnavailableErr calls (Exiter).Exit() with ExitUnavailable (69) (nice).
It is recommended to use this for client programs which cannot communicate with another process or remote service.
func (*Exiter) ExitWithUsageErr ¶
ExitWithUsageErr prints the help/usage and calls (Exiter).Exit() with ExitUsage (64). If no UsageFunc was set, it will not print anything.
It is recommended to use this when bad arguments are passed.
func (*Exiter) ExitWithUsageOK ¶
func (e *Exiter) ExitWithUsageOK()
ExitWithUsageOK prints the help/usage and calls (Exiter).Exit() with ExitOK (0). If no UsageFunc was set, it will not print anything.
It is recommended to use this when responding to help arguments.
func (*Exiter) GetCancelableContext ¶
GetCancelableContext returns the underlying cancelable context used by the Exiter.
func (*Exiter) GetSignalCatcher ¶
func (e *Exiter) GetSignalCatcher() *sigar.SignalCatcher
GetSignalCatcher returns the underlying *sigar.SignalCatcher used by the Exiter.
func (*Exiter) ListenForSignals ¶
func (e *Exiter) ListenForSignals()
ListenForSignals spawns a goroutine to catch signals so that the Exiter can gracefully shutdown the program.
func (*Exiter) RegisterPostTask ¶
func (e *Exiter) RegisterPostTask(fn ExitTaskFn)
RegisterPostTask adds a task to the queue that will be executed after unblocking goroutines blocked at (Exiter).Wait().
func (*Exiter) RegisterPreTask ¶
func (e *Exiter) RegisterPreTask(fn ExitTaskFn)
RegisterPreTask adds a task to the queue that will be executed prior to unblocking goroutines blocked at (Exiter).Wait().
func (*Exiter) SetUsageFunc ¶
func (e *Exiter) SetUsageFunc(usage ExitTaskFn)
SetUsageFunc tells the Exiter what function to call to print the program's help/usage information.