Documentation
¶
Index ¶
- Constants
- Variables
- func Context() context.Context
- func Fork(pidPath *Dir, cfg ...interface{})
- func IsDevelopment() bool
- func Manager(g interface{})
- func NewExpire(path ...string)
- func Parser(arg []string, path *string, cfg ...interface{})
- func Ready()
- func Shutdown()
- func Stop()
- func Summary(cfg ...interface{})
- type Dir
- type Expire
- type Message
- type Persist
- type PersistMap
- type Shell
Constants ¶
const ( MDevelopment mode = 1 << iota MSilent // Silence summary output flag MShell // Shell interactive interface flag MScript // Shell file script processor flag MCLI // Shell command line interface flag )
Variables ¶
var ( Mode mode // env operational settings and status Version string // Version information, set by a builder.sh Build string // Build information, set by a builder.sh EtcPath Dir = "/etc" // env Dir type /etc path location SrvPath Dir = "/srv" // env Dir type /srv path location VarPath Dir = "/var" // env Dir type /var path location TmpPath Dir = "/tmp" // ent Dir type /tmp path location FileMode os.FileMode = 0644 // env os.FileMode (default: 0644) ExitStatusCode int // exit status code in range [0, 125] (default: 0) Identity = filepath.Base(os.Args[0]) // Identity of app, as configured by env Description string // Brief description, license, copyright )
var Development = func(flag *bool) { if flag == nil || *flag || runtime.GOOS != "linux" { Mode |= MDevelopment } if flag != nil && !*flag { Mode &^= MDevelopment } if Mode&MDevelopment == MDevelopment { Identity = "development" Version = Identity Build = Identity EtcPath = "_dev/etc" SrvPath = "_dev/srv" VarPath = "_dev/var" TmpPath = "_dev/tmp" } }
Development will autodetect production as linux however pass a boolean value to manually override auto detection; called by Init()
var Init = func(cfg ...interface{}) { if Mode&mInit != mInit { Mode |= mInit if len(os.Args) > 1 { req := strings.TrimLeft(os.Args[1], "-") if req == "version" || req == "help" { var n = 18 if len(Identity) > n { n = len(Identity) } if len(Version)+10 > n { n = len(Version) + 10 } if len(Build)+10 > n { n = len(Build) + 10 } fmt.Printf("\n %-s\n%s\n version %s\n build %s\n", Identity, strings.Repeat("-", n+2), Version, Build) if req == "help" { if len(Description) > 0 { fmt.Printf("%s\n", Description) } fmt.Println() var order int for i := range cfg { v := reflect.Indirect(reflect.ValueOf(cfg[i])) for j := 0; j < v.NumField(); j++ { if name, flag, ok := nameFlag(v.Field(j), v.Type().Field(j)); ok { if help, ok := v.Type().Field(j).Tag.Lookup("help"); ok || len(flag) > 0 { if len(flag) > 0 { flag = " " + flag if strings.Contains(flag, "order") { order++ flag = strings.Replace(flag, "order", "arg:"+strconv.Itoa((order)), 1) } } value, _ := v.Type().Field(j).Tag.Lookup("default") if len(value) > 0 { value = " (default: " + value + ")" } fmt.Printf("-%-15s %s%s\n%-17s[%s%s]\n\n", name, help, value, "", v.Type().Field(j).Type.String(), flag) } } } } } fmt.Println() os.Exit(0) } } Development(nil) conf := EtcPath.Join(Identity + ".conf") Parser(os.Args[1:], &conf, cfg...) } }
Init provides version,build help, development detection and modifications, and uses the Parser to process and populate the cfg structs from tag:default valuse, os.Env, os.Args, and /etc/{identity}.conf values based on the cfg tag descriptions and settings
Functions ¶
func Fork ¶
func Fork(pidPath *Dir, cfg ...interface{})
Fork is an Init wrapper that enables a program to run normally or like a daemon start|stop process; pidPath directory must exist and the user must have r/w file level permissions for proper operation; pass nil for default
func IsDevelopment ¶ added in v1.5.0
func IsDevelopment() bool
IsDevelopment reports the current MDevelopment mode state
func Manager ¶ added in v1.4.0
func Manager(g interface{})
Manager places the struct or func under graceful management and references it by name and requires the function sigature of 'func(ctx context.Context)', so any closure must return the graceful signature to operate properly as well.
A graceful struct must have a 'Start(ctx)' method with the proper graceful signature.
The use of any params passed to a struct or function that uses a closure will make the sequence run lock-step. To avoid this, wrap the closure head in a go routine.
func NewExpire ¶ added in v1.1.0
func NewExpire(path ...string)
NewExpire is an Expire wrapper that will create and start a file expiration manger under env.Manager control using default values applied to the direcory paths that are passed as paramaters.
Configure *Expire directly when custom settings are desired.
func Parser ¶
Parser will process and set struct field values obtained from default values, then a conf file, then the os environment based on (tag:env: environ) flag, then arg values when non-nil, then os.Args, followed by any flagless os.Args, values (tag:env: order), with the value pushed back to the os environment when the (tag:env: environ) is present.
tag: 'env' name:order,require,environ tag: 'default' value; string, bool, int, int64, uint, uint64 tag: 'help' description
var Param struct {
File `env:"order,require" default:"result" help:"result file"`
}
func Shutdown ¶
func Shutdown()
Shutdown waits for a termination signal (os.Interrupt, syscall.SIGTERM) and then initiates graceful cleanup; only initalized and configured once
Types ¶
type Dir ¶
type Dir string
Dir type
func (Dir) Create ¶
Create appends a... and return an updated string; create the directory tree when it does not exist and return a string representation of the full composite path. A file is presumend when the last element contains any of the following ._- characters.
conf.VarPath.Create() -> /var/log
conf.VarPath.Create("insite") -> /var/log/insite
conf.VarPath.Create("insite.log") -> /var/log/insite.log
conf.VarPath.Create("insite","insite.log") -> /var/log/insite/iniste.log
type Expire ¶ added in v1.1.0
type Expire struct {
CheckOn time.Duration // frequency of checks (default: hourly)
// contains filtered or unexported fields
}
Expire is a file expiration manager
func (*Expire) Add ¶ added in v1.1.0
Add will register a directory path with customized age timeframe
type Persist ¶ added in v1.5.0
type Persist string
Persist type; filename
type PersistMap ¶ added in v1.5.0
PersistMap tracker type
func (*PersistMap) Next ¶ added in v1.5.0
func (m *PersistMap) Next(age time.Duration) func() (item interface{})
Next returns a valid unexpired item
type Shell ¶ added in v1.5.0
type Shell struct {
// contains filtered or unexported fields
}
Shell configurator type
func (*Shell) Args ¶ added in v1.5.0
func (sh *Shell) Args(cfg ...interface{})
Args process shell line args (os.Args) and populates cfg structs
func (*Shell) Run ¶ added in v1.5.0
func (sh *Shell) Run(cfg ...interface{})
Run starts the shell,script system and calls any graceful or non-graceful Start methods assoiciated with the cfg structs passed in
func (*Shell) SetTimeout ¶ added in v1.5.0
SetTimeout for inactivity automatic shell exit