mains

package module
Version: v0.4.26 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: ISC Imports: 19 Imported by: 2

Documentation

Overview

Package mains contains functions for implementing a service or command-line utility

Index

Constants

View Source
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
)
View Source
const (
	NoOK = "-"
)
View Source
const (
	SilentString = "-" + silentOption
)

Variables

This section is empty.

Functions

func GetStringSliceValue

func GetStringSliceValue(p *[]string, value []string) (v flag.Value)

GetStringSliceValue initializes

func Keystrokes

func Keystrokes(send func(s string), g0 parl.Go)

Keystrokes emits keystroke events on g0.Context() shutdown, scannerThread is left running until the next newline

func ProcessStartTime

func ProcessStartTime() (createTime time.Time)

ProcessStartTime returns the time the executing process was started. Resolution is seconds, time zone is local

func Timer

func Timer(ctx ev.Callee)

Timer displays a second counter and help on long-running invocations

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) PrintErr

func (ex *Executable) PrintErr(err error)

PrintErr prints an error

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

func GetStatus

func GetStatus() (stat *Status)

GetStatus gets instance of status printer

func (*Status) Clear

func (stat *Status) Clear()

Clear remove previously printed status

func (*Status) Print

func (stat *Status) Print(s string)

Print status information

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"
)

type YamlValue

type YamlValue struct {
	Name    string
	Pointer interface{}
}

func NewYamlValue

func NewYamlValue(instancePointer interface{}, fieldPointer interface{}) (yv *YamlValue)

Jump to

Keyboard shortcuts

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