happy

package module
v0.1.5 Latest Latest
Warning

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

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

README

examples

hello

most minimal usage

go run ./examples/hello/
# increase verbosity
go run ./examples/hello/ --debug
go run ./examples/hello/ --system-debug
# help
go run ./examples/hello/ -h

kitchensink

main application when no subcommand is provided

go run ./examples/kitchensink/
# increase verbosity
go run ./examples/kitchensink/ --verbose
go run ./examples/kitchensink/ --debug
go run ./examples/kitchensink/ --system-debug

# main application help
go run ./examples/kitchensink/ -h

hello command with flags

go run ./examples/kitchensink/ hello --name me --repeat 10 
# or shorter
go run ./examples/kitchensink/ hello -n me -r 10 

# help for hello command
go run ./examples/kitchensink/ hello -h

Documentation

Overview

Package happy provides a modular framework for rapid prototyping in Go. With this SDK, developers of all levels can easily bring their ideas to life. Whether you're a hacker or a creator, Package happy has everything you need to tackle your domain problems and create working prototypes or MVPs with minimal technical knowledge and infrastructure planning.

Its modular design enables you to package your commands and services into reusable addons, so you're not locked into any vendor tools. It also fits well into projects where different components are written in different programming languages.

Let Package happy help you bring your projects from concept to reality and make you happy along the way.

Index

Constants

View Source
const (
	// Levels
	LevelSystemDebug    = hlog.LevelSystemDebug
	LevelDebug          = hlog.LevelDebug
	LevelInfo           = hlog.LevelInfo
	LevelTask           = hlog.LevelTask
	LevelOk             = hlog.LevelOk
	LevelNotice         = hlog.LevelNotice
	LevelWarn           = hlog.LevelWarn
	LevelNotImplemented = hlog.LevelNotImplemented
	LevelDeprecated     = hlog.LevelDeprecated
	LevelIssue          = hlog.LevelIssue
	LevelError          = hlog.LevelError
	LevelOut            = hlog.LevelOut
)

Variables

View Source
var (
	ErrApplication      = errors.New("application error")
	ErrCommand          = errors.New("command error")
	ErrCommandFlags     = errors.New("command flags error")
	ErrCommandAction    = errors.New("command action error")
	ErrInvalidVersion   = errors.New("invalid version")
	ErrEngine           = errors.New("engine error")
	ErrSessionDestroyed = errors.New("session destroyed")
	ErrService          = errors.New("service error")
)
View Source
var (
	ErrOption           = errors.New("option error")
	ErrOptionReadOnly   = fmt.Errorf("%w: readonly option", ErrOption)
	ErrOptionValidation = fmt.Errorf("%w: option validation error", ErrOption)
	ErrOptions          = errors.New("options error")
)

Functions

func GetAPI added in v0.1.5

func GetAPI[A API](sess *Session, addonName string) (api A, err error)

Types

type API

type API interface {
	Set(key string, value any)
	Get(key string) vars.Variable
}

type Action added in v0.1.5

type Action func(sess *Session) error

type ActionTick added in v0.1.5

type ActionTick func(sess *Session, ts time.Time, delta time.Duration) error

ActionTickFunc is operation set in given minimal time frame it can be executed. You can throttle tick/tocks to cap FPS or for [C|G]PU throttling.

Tock is helper called after each tick to separate logic processed in tick and do post processing on tick. Tocks are useful mostly for GPU ops which need to do post proccessing of frames rendered in tick.

type ActionTock added in v0.1.5

type ActionTock func(sess *Session, delta time.Duration, tps int) error

type ActionWithArgs added in v0.1.5

type ActionWithArgs func(sess *Session, args Args) error

type ActionWithEvent added in v0.1.5

type ActionWithEvent func(sess *Session, ev Event) error

type Addon

type Addon interface {
	Register(*Session) (AddonInfo, error)
	Commands() []*Command
	Services() []*Service
	// Returns slice of event regsistration events
	// created using RegisterEvent
	Events() []Event
	API() API
}

type AddonInfo

type AddonInfo struct {
	Name        string
	Description string
	Version     string
}

type Application

type Application struct {
	// contains filtered or unexported fields
}

func New added in v0.1.5

func New(opts ...OptionAttr) *Application

New returns new happy application instance. It panics if there is critical internal error or bug.

func (*Application) AddCommand added in v0.1.5

func (a *Application) AddCommand(cmd *Command)

func (*Application) AddFlag added in v0.1.5

func (a *Application) AddFlag(f varflag.Flag)

func (*Application) AfterAlways added in v0.1.5

func (a *Application) AfterAlways(action Action)

func (*Application) AfterFailure added in v0.1.5

func (a *Application) AfterFailure(action func(s *Session, err error) error)

func (*Application) AfterSuccess added in v0.1.5

func (a *Application) AfterSuccess(action Action)

func (*Application) Before added in v0.1.5

func (a *Application) Before(action ActionWithArgs)

func (*Application) Cron added in v0.1.5

func (a *Application) Cron(setup func(schedule CronScheduler))

func (*Application) Do added in v0.1.5

func (a *Application) Do(action ActionWithArgs)

func (*Application) Main

func (a *Application) Main()

func (*Application) OnTick added in v0.1.5

func (a *Application) OnTick(action ActionTick)

func (*Application) OnTock added in v0.1.5

func (a *Application) OnTock(action ActionTock)

func (*Application) RegisterService

func (a *Application) RegisterService(svc *Service) error

func (*Application) WithAddons added in v0.1.5

func (a *Application) WithAddons(addon ...Addon)

type Args added in v0.1.5

type Args interface {
	Arg(i uint) vars.Value
	ArgDefault(i uint, value any) (vars.Value, error)
	ArgVarDefault(i uint, key string, value any) (vars.Variable, error)
	Args() []vars.Value
	Flag(name string) varflag.Flag
}

type Assets added in v0.1.5

type Assets interface{}

type Command

type Command struct {
	// contains filtered or unexported fields
}

func NewCommand added in v0.1.5

func NewCommand(name string, options ...OptionAttr) *Command

func (*Command) AddFlag added in v0.1.5

func (c *Command) AddFlag(f varflag.Flag)

func (*Command) AddSubCommand added in v0.1.5

func (c *Command) AddSubCommand(cmd *Command)

func (*Command) AfterAlways added in v0.1.5

func (c *Command) AfterAlways(action func(s *Session) error)

func (*Command) AfterFailure added in v0.1.5

func (c *Command) AfterFailure(action func(s *Session, err error) error)

func (*Command) AfterSuccess added in v0.1.5

func (c *Command) AfterSuccess(action func(s *Session) error)

func (*Command) Before added in v0.1.5

func (c *Command) Before(action ActionWithArgs)

func (*Command) Do added in v0.1.5

func (c *Command) Do(action ActionWithArgs)

func (*Command) Err

func (c *Command) Err() error

func (*Command) Name added in v0.1.5

func (c *Command) Name() string

func (*Command) Usage added in v0.1.5

func (c *Command) Usage() string

type Cron

type Cron struct {
	// contains filtered or unexported fields
}

func (*Cron) Job added in v0.1.5

func (cs *Cron) Job(expr string, cb Action)

func (*Cron) Start added in v0.1.5

func (cs *Cron) Start() error

func (*Cron) Stop added in v0.1.5

func (cs *Cron) Stop() error

type CronScheduler

type CronScheduler interface {
	Job(expr string, cb Action)
}

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

type Event

type Event interface {
	Key() string
	Scope() string
	Payload() *vars.Map
	Time() time.Time
}

func NewEvent added in v0.1.5

func NewEvent(scope, key string, payload *vars.Map, err error) Event

func RegisterEvent added in v0.1.5

func RegisterEvent(scope, key, desc string, example *vars.Map) Event

func StartServicesEvent added in v0.1.5

func StartServicesEvent(svcs ...string) Event

func StopServicesEvent added in v0.1.5

func StopServicesEvent(svcs ...string) Event

type EventListener

type EventListener interface {
	OnEvent(scope, key string, cb ActionWithEvent)
	OnAnyEvent(ActionWithEvent)
}

type OptionAttr added in v0.1.5

type OptionAttr struct {
	// contains filtered or unexported fields
}

Option is used to define option and apply given key value to options.

func Option added in v0.1.5

func Option(key string, value any) OptionAttr

Opt creates option for given key value pair which can be applied to any Options set.

type OptionKind added in v0.1.5

type OptionKind uint

type OptionValueValidator added in v0.1.5

type OptionValueValidator func(key string, val vars.Value) error

OptionValueValidator is callback function to validate given value, it recieves copy of value for validation. It MUST return error if validation fails, returned boolean indicates shoulkd that option be marked as radonly if validation succeeds.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options is general collection of settings attached to specific application component.

func NewOptions added in v0.1.5

func NewOptions(name string, defaults []OptionAttr) (*Options, error)

NewOptions returns new named options with optiona validator when provided.

func (*Options) Accepts added in v0.1.5

func (opts *Options) Accepts(key string) bool

Accepts reports whether given option key is accepted by Options.

func (*Options) Get added in v0.1.5

func (opts *Options) Get(key string) vars.Variable

func (*Options) Has added in v0.1.5

func (opts *Options) Has(key string) bool

Has reports whether options has given key

func (*Options) Load added in v0.1.5

func (opts *Options) Load(key string) (vars.Variable, bool)

func (*Options) Name added in v0.1.5

func (opts *Options) Name() string

Name is name for this Option collection.

func (*Options) Set added in v0.1.5

func (opts *Options) Set(key string, value any) error

type Service

type Service struct {
	EventListener
	TickerFuncs
	// contains filtered or unexported fields
}

func NewService added in v0.1.5

func NewService(name string, opts ...OptionAttr) *Service

func (*Service) Cron added in v0.1.5

func (s *Service) Cron(setupFunc func(schedule CronScheduler))

func (*Service) OnAnyEvent added in v0.1.5

func (s *Service) OnAnyEvent(cb ActionWithEvent)

func (*Service) OnEvent added in v0.1.5

func (s *Service) OnEvent(scope, key string, cb ActionWithEvent)

func (*Service) OnInitialize

func (s *Service) OnInitialize(action Action)

OnInitialize is called when app is preparing runtime and attaching services.

func (*Service) OnStart

func (s *Service) OnStart(action Action)

OnStart is called when service is requested to be started. For instace when command is requiring this service or whenever service is required on runtime via sess.RequireService call.

Start can be called multiple times in case of service restarts. If you do not want to allow service restarts you should implement your logic in OnStop when it's called first time and check that state OnStart.

func (*Service) OnStop

func (s *Service) OnStop(action Action)

func (*Service) OnTick added in v0.1.5

func (s *Service) OnTick(action ActionTick)

func (*Service) OnTock added in v0.1.5

func (s *Service) OnTock(action ActionTock)

type ServiceInfo added in v0.1.5

type ServiceInfo struct {
	// contains filtered or unexported fields
}

func (*ServiceInfo) Addr added in v0.1.5

func (s *ServiceInfo) Addr() *address.Address

func (*ServiceInfo) Errs added in v0.1.5

func (s *ServiceInfo) Errs() map[time.Time]error

func (*ServiceInfo) Failed added in v0.1.5

func (s *ServiceInfo) Failed() bool

func (*ServiceInfo) Name added in v0.1.5

func (s *ServiceInfo) Name() string

func (*ServiceInfo) Running added in v0.1.5

func (s *ServiceInfo) Running() bool

func (*ServiceInfo) StartedAt added in v0.1.5

func (s *ServiceInfo) StartedAt() time.Time

func (*ServiceInfo) StoppedAt added in v0.1.5

func (s *ServiceInfo) StoppedAt() time.Time

type ServiceLoader

type ServiceLoader struct {
	// contains filtered or unexported fields
}

func NewServiceLoader added in v0.1.5

func NewServiceLoader(sess *Session, svcs ...string) *ServiceLoader

func (*ServiceLoader) Err

func (sl *ServiceLoader) Err() error

func (*ServiceLoader) Load added in v0.1.5

func (sl *ServiceLoader) Load() <-chan struct{}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) API added in v0.1.5

func (s *Session) API(addonName string) (API, error)

func (*Session) Deadline added in v0.1.5

func (s *Session) Deadline() (deadline time.Time, ok bool)

Deadline returns the time when work done on behalf of this context should be canceled. Deadline returns ok==false when no deadline is set. Successive calls to Deadline return the same results.

func (*Session) Destroy

func (s *Session) Destroy(err error)

func (*Session) Dispatch

func (s *Session) Dispatch(ev Event)

func (*Session) Done added in v0.1.5

func (s *Session) Done() <-chan struct{}

Done enables you to hook into chan to know when application exits however DO NOT use that for graceful shutdown actions. Use Application.AddExitFunc instead.

func (*Session) Err added in v0.1.5

func (s *Session) Err() error

Err returns session error if any or nil If Done is not yet closed, Err returns nil. If Done is closed, Err returns a non-nil error explaining why: Canceled if the context was canceled or DeadlineExceeded if the context's deadline passed. After Err returns a non-nil error, successive calls to Err return the same error.

func (*Session) Get

func (s *Session) Get(key string) vars.Variable

func (*Session) Has added in v0.1.5

func (s *Session) Has(key string) bool

func (*Session) Log

func (s *Session) Log() *hlog.Logger

func (*Session) Ready

func (s *Session) Ready() <-chan struct{}

func (*Session) ServiceInfo added in v0.1.5

func (s *Session) ServiceInfo(svcurl string) (*ServiceInfo, error)

func (*Session) Set added in v0.1.5

func (s *Session) Set(key string, val any) error

func (*Session) String added in v0.1.5

func (s *Session) String() string

func (*Session) Value added in v0.1.5

func (s *Session) Value(key any) any

Value returns the value associated with this context for key, or nil

type TickerFuncs

type TickerFuncs interface {
	// OnTick enables you to define func body for operation set
	// to call in minimal timeframe until session is valid and
	// service is running.
	OnTick(ActionTick)

	// OnTock is helper called right after OnTick to separate
	// your primary operations and post prossesing logic.
	OnTock(ActionTick)
}

Directories

Path Synopsis
examples
pkg
hlog/internal/buffer
Package buffer provides a pool-allocated byte buffer.
Package buffer provides a pool-allocated byte buffer.
varflag
Package varflag implements command-line flag parsing into compatible with var library.
Package varflag implements command-line flag parsing into compatible with var library.
vars
Package vars provides the API to parse variables from various input formats/kinds to common key value pair.
Package vars provides the API to parse variables from various input formats/kinds to common key value pair.
sdk
cli
Package cli is provides implementations of happy.Application command line interfaces
Package cli is provides implementations of happy.Application command line interfaces

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳