Documentation
¶
Overview ¶
Package mains contains functions for implementing a service or command-line utility
Index ¶
- Constants
- func GetStringSliceValue(p *[]string, value []string) (v flag.Value)
- func Keystrokes(send func(s string), g0 parl.Go)
- func ProcessStartTime() (createTime time.Time)
- func Timer(ctx ev.Callee)
- type ArgumentSpec
- type BaseOptionsType
- type Executable
- func (ex *Executable) AddErr(err error) (x *Executable)
- func (ex *Executable) ConfigureLog() (ex1 *Executable)
- func (ex *Executable) Exit()
- func (ex *Executable) Init() *Executable
- func (ex *Executable) LongErrors(isLongErrors bool, isErrorLocation bool) *Executable
- func (ex *Executable) PrintBannerAndParseOptions(om []OptionData) (ex1 *Executable)
- func (ex *Executable) PrintErr(err error)
- func (ex *Executable) Recover(errp ...*error)
- type OptionData
- type Status
- type StringSliceValue
- type YamlOption
- type YamlValue
Constants ¶
const ( // NoArguments besides switches, zero trailing arguments is allowed NoArguments = 1 << iota // OneArgument besides switches, exactly one trailing arguments is allowed OneArgument // ManyArguments besides switches, one or more trailing arguments is allowed ManyArguments )
const (
NoOK = "-"
)
const (
SilentString = "-" + silentOption
)
Variables ¶
This section is empty.
Functions ¶
func GetStringSliceValue ¶
GetStringSliceValue initializes
func Keystrokes ¶
Keystrokes emits keystroke events on g0.Context() shutdown, scannerThread is left running until the next newline
func ProcessStartTime ¶
ProcessStartTime returns the time the executing process was started. Resolution is seconds, time zone is local
Types ¶
type ArgumentSpec ¶
type ArgumentSpec uint32
ArgumentSpec bitfield for 0, 1, many arguments following command-line switches
type BaseOptionsType ¶
type BaseOptionsType = struct { YamlFile string YamlKey string Verbosity string Debug bool Silent bool }
var BaseOptions BaseOptionsType
type Executable ¶
type Executable struct { Program string // gonet Version string // 0.0.1 Comment string // [ banner text after program and version] options parsing (about this version) Description string // [Description part of usage] configures firewall and routing Copyright string // © 2020… License string // ISC License OKtext string // Completed successfully ArgumentsUsage string Arguments ArgumentSpec // eg. NoArguments // fields below popualted by .Init() Launch time.Time LaunchString string Host string // short hostname, ie. no dots ArgCount int Arg string Args []string IsLongErrors bool IsErrorLocation bool // contains filtered or unexported fields }
Executable constant strings that describes an executable advisable static values include Program Version Comment Description Copyright License Arguments like:
var exe = mains.Executable{ Program: "getip", Version: "0.0.1", Comment: "first version", Description: "finds ip address for hostname", Copyright: "© 2020-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com)", License: "All rights reserved", Arguments: mains.NoArguments | mains.OneArgument, }
func (*Executable) AddErr ¶
func (ex *Executable) AddErr(err error) (x *Executable)
AddErr extended with immediate printing of first error
func (*Executable) ConfigureLog ¶
func (ex *Executable) ConfigureLog() (ex1 *Executable)
ConfigureLog configures the default log such as parl.Log parl.Out parl.D for silent, debug and regExp. Settings come from BaseOptions.Silent and BaseOptions.Debug.
ConfigureLog supports functional chaining like:
exe.Init(). … ConfigureLog(). ApplyYaml(…)
func (*Executable) Exit ¶
func (ex *Executable) Exit()
Exit terminate from mains.err: exit 0 or echo to stderr and status code 1
func (*Executable) Init ¶
func (ex *Executable) Init() *Executable
Init populate launch time and sets silence if first argument is “-silent.” Init supports function chaining like:
exe.Init(). PrintBannerAndParseOptions(optionData). LongErrors(options.Debug, options.Verbosity != ""). ConfigureLog(). ApplyYaml(options.YamlFile, options.YamlKey, applyYaml, optionData) …
func (*Executable) LongErrors ¶
func (ex *Executable) LongErrors(isLongErrors bool, isErrorLocation bool) *Executable
LongErrors sets if errors are printed with stack trace and values. LongErrors supports functional chaining:
exe.Init(). … LongErrors(options.Debug, options.Verbosity != ""). ConfigureLog()…
isLongErrors prints full stack traces, related errors and error data in string lists and string maps.
isErrorLocation appends the innermost location to the error message when isLongErrors is not set:
error-message at error116.(*csTypeName).FuncName-chainstring_test.go:26
func (*Executable) PrintBannerAndParseOptions ¶
func (ex *Executable) PrintBannerAndParseOptions(om []OptionData) (ex1 *Executable)
PrintBannerAndParseOptions prints greeting like:
parl 0.1.0 parlca https server/client udp server
It then parses options described by []OptionData stroing the values at OptionData.P. If options fail to parse, a proper message is printed to stderr and the process exits with status code 2. PrintBannerAndParseOptions supports functional chaining like:
exe.Init(). PrintBannerAndParseOptions(…). LongErrors(…
Options and yaml is configured likeso:
var options = &struct { noStdin bool *mains.BaseOptionsType }{BaseOptionsType: &mains.BaseOptions} var optionData = append(mains.BaseOptionData(exe.Program, mains.YamlYes), []mains.OptionData{ {P: &options.noStdin, Name: "no-stdin", Value: false, Usage: "Service: do not use standard input", Y: mains.NewYamlValue(&y, &y.NoStdin)}, }...) type YamlData struct { NoStdin bool // nostdin: true } var y YamlData
func (*Executable) Recover ¶
func (ex *Executable) Recover(errp ...*error)
Recover function to be used in main.main:
func main() { defer Recover() …
On panic, the function prints to stderr: "Unhandled panic invoked exe.Recover: stack:" followed by a stack trace. It then adds an error to mains.Executable and terminates the process with status code 1
type OptionData ¶
type OptionData struct { P interface{} Name string Value interface{} Usage string Y *YamlValue // pointer to data value from yaml }
OptionData contain options data for the flag package
func BaseOptionData ¶
func BaseOptionData(program string, yaml YamlOption) (od []OptionData)
func (*OptionData) AddOption ¶
func (om *OptionData) AddOption()
AddOption executes flag.BoolVar and such on the options map
func (*OptionData) ApplyYaml ¶
func (om *OptionData) ApplyYaml() (err error)
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status handles printing of status to stdout
type StringSliceValue ¶
type StringSliceValue struct {
// contains filtered or unexported fields
}
StringSliceValue manages a string-slice value for flag.Var
func (*StringSliceValue) Set ¶
func (v *StringSliceValue) Set(s string) (err error)
Set updates the string slice
func (StringSliceValue) String ¶
func (v StringSliceValue) String() (s string)
type YamlOption ¶
type YamlOption bool
const ( YamlNo YamlOption = false YamlYes YamlOption = true DebugOption = "-debug" )