launchr

package
v0.16.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package launchr provides common app functionality.

Index

Constants

View Source
const PkgPath = "github.com/launchrctl/launchr"

PkgPath is a main module path.

Variables

View Source
var (
	ErrNoConfigFile = errors.New("config file is not found") // ErrNoConfigFile when config file doesn't exist in FS.
)

Functions

func EnsurePath

func EnsurePath(parts ...string) error

EnsurePath creates all directories in the path.

func EstimateTime added in v0.16.0

func EstimateTime(fn func(diff time.Duration)) func()

EstimateTime returns a function that runs callback with the elapsed time between the call to timer and the call to the returned function. The returned function is intended to be used in a defer statement:

defer EstimateTime("sum", func (diff time.Duration) { ... })().

func GetFsAbsPath

func GetFsAbsPath(fs fs.FS) string

GetFsAbsPath returns absolute path for a fs.FS struct.

func GetTypePkgPathName added in v0.1.0

func GetTypePkgPathName(v any) (string, string)

GetTypePkgPathName returns type package path and name for internal usage.

func InitPluginInfo added in v0.1.0

func InitPluginInfo(pi *PluginInfo, p Plugin)

InitPluginInfo sets private fields for internal usage only.

func InitServiceInfo added in v0.1.0

func InitServiceInfo(si *ServiceInfo, s Service)

InitServiceInfo sets private fields for internal usage only.

func IsCommandErrHelp added in v0.16.0

func IsCommandErrHelp(err error) bool

IsCommandErrHelp checks if an error is a flag help err used for intercommunication.

func IsSELinuxEnabled added in v0.16.0

func IsSELinuxEnabled() bool

IsSELinuxEnabled checks if selinux is enabled on the system.

func NewExitError added in v0.16.4

func NewExitError(code int, msg string) error

NewExitError creates a new ExitError.

func RegisterPlugin added in v0.1.0

func RegisterPlugin(p Plugin)

RegisterPlugin add a plugin to global pull.

func SetLogger added in v0.16.0

func SetLogger(l *Logger)

SetLogger sets the default logger.

Types

type App added in v0.1.0

type App interface {
	// Name returns app name.
	Name() string
	// GetWD provides app's working dir.
	GetWD() string
	// Streams returns application cli.
	Streams() Streams
	// SetStreams sets application streams.
	SetStreams(s Streams)
	// AddService registers a service in the app.
	// Panics if a service is not unique.
	AddService(s Service)
	// GetService retrieves a service of type [v] and assigns it to [v].
	// Panics if a service is not found.
	GetService(v any)

	// RegisterFS registers a File System in launchr.
	// It may be a FS for action discovery, see [action.DiscoveryFS].
	RegisterFS(fs ManagedFS)
	// GetRegisteredFS returns an array of registered File Systems.
	GetRegisteredFS() []ManagedFS
}

App stores global application state.

type AppInternal added in v0.16.0

type AppInternal interface {
	App
	GetRootCmd() *Command
	EarlyParsedFlags() []string
}

AppInternal is an extension to access cobra related functionality of the app. It is intended for internal use only to prevent coupling on volatile functionality.

type AppVersion added in v0.1.0

type AppVersion struct {
	Name        string
	Version     string
	OS          string
	Arch        string
	BuiltWith   string
	CoreVersion string
	CoreReplace string
	Plugins     []string
}

AppVersion stores application version.

func NewVersion added in v0.1.0

func NewVersion(name, ver, bwith string, plugins PluginsMap) *AppVersion

NewVersion creates version info with used plugins info.

func Version added in v0.1.0

func Version() *AppVersion

Version provides app version info.

func (*AppVersion) Full added in v0.1.0

func (v *AppVersion) Full() string

Full outputs version string in a full format.

func (*AppVersion) Short added in v0.1.0

func (v *AppVersion) Short() string

Short outputs a short version string.

func (*AppVersion) String added in v0.2.1

func (v *AppVersion) String() string

String implements Stringer interface.

type CobraPlugin added in v0.1.0

type CobraPlugin interface {
	Plugin
	// CobraAddCommands is a hook called when cobra root command is available.
	// Plugins may register its command line commands here.
	CobraAddCommands(root *Command) error
}

CobraPlugin is an interface to implement a plugin for cobra.

type Command added in v0.16.0

type Command = cobra.Command

Command is a type alias for cobra.Command. to reduce direct dependency on cobra in packages.

type CommandGroup added in v0.16.0

type CommandGroup = cobra.Group

CommandGroup is a type alias for cobra.Group.

type Config added in v0.1.0

type Config interface {
	Service
	// DirPath returns an absolute path to config directory.
	DirPath() string
	// Path provides an absolute path to launchr config directory.
	Path(parts ...string) string
	// Exists checks if key exists in config. Key level delimiter is dot.
	// For example - `path.to.something`.
	Exists(key string) bool
	// Get returns a value by key to a parameter v. Parameter v must be a pointer to a value.
	// Error may be returned on decode.
	Get(key string, v any) error
}

Config is a launchr config storage interface.

func ConfigFromFS added in v0.1.0

func ConfigFromFS(root fs.FS) Config

ConfigFromFS parses launchr app config directory and its content.

type ConfigAware added in v0.1.0

type ConfigAware interface {
	// SetLaunchrConfig sets a launchr config to the struct.
	SetLaunchrConfig(Config)
}

ConfigAware provides an interface for structs to support launchr configuration setting.

type ExitError added in v0.16.4

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

ExitError is an error holding an error code of executed command.

func (ExitError) Error added in v0.16.4

func (e ExitError) Error() string

Error implements [error] interface.

func (ExitError) ExitCode added in v0.16.4

func (e ExitError) ExitCode() int

ExitCode returns the exit code.

type GenerateConfig added in v0.16.3

type GenerateConfig struct {
	WorkDir  string // WorkDir is where the script must consider current working directory.
	BuildDir string // BuildDir is where the script will output the result.
}

GenerateConfig defines generation config.

type GeneratePlugin added in v0.1.0

type GeneratePlugin interface {
	Plugin
	// Generate is a function called when application is generating code and assets for the build.
	Generate(config GenerateConfig) error
}

GeneratePlugin is an interface to generate supporting files before build.

type In added in v0.16.0

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

In is an input stream used by the app to read user input.

func NewIn added in v0.16.0

func NewIn(in io.ReadCloser) *In

NewIn returns a new In object from a io.ReadCloser

func (*In) CheckTty added in v0.16.0

func (i *In) CheckTty(attachStdin, ttyMode bool) error

CheckTty checks if we are trying to attach to a container tty from a non-tty client input stream, and if so, returns an error.

func (*In) Close added in v0.16.0

func (i *In) Close() error

Close implements the io.Closer interface.

func (*In) FD added in v0.16.0

func (s *In) FD() uintptr

FD returns the file descriptor number for this stream.

func (*In) IsTerminal added in v0.16.0

func (s *In) IsTerminal() bool

IsTerminal returns true if this stream is connected to a terminal.

func (*In) Read added in v0.16.0

func (i *In) Read(p []byte) (int, error)

func (*In) RestoreTerminal added in v0.16.0

func (s *In) RestoreTerminal()

RestoreTerminal restores normal mode to the terminal.

func (*In) SetIsTerminal added in v0.16.0

func (s *In) SetIsTerminal(isTerminal bool)

SetIsTerminal sets the boolean used for isTerminal.

func (*In) SetRawTerminal added in v0.16.0

func (i *In) SetRawTerminal() (err error)

SetRawTerminal sets raw mode on the input terminal.

type LogLevel added in v0.16.0

type LogLevel int

A LogLevel is the importance or severity of a log event.

const (
	LogLevelDisabled LogLevel = iota // LogLevelDisabled does never print.
	LogLevelDebug                    // LogLevelDebug is the log level for debug.
	LogLevelInfo                     // LogLevelInfo is the log level for info.
	LogLevelWarn                     // LogLevelWarn is the log level for warnings.
	LogLevelError                    // LogLevelError is the log level for errors.
)

type LogOptions added in v0.16.0

type LogOptions interface {
	// Level returns the currently set log level.
	Level() LogLevel
	// SetLevel sets log level.
	SetLevel(l LogLevel)
	// SetOutput sets logger output.
	SetOutput(w io.Writer)
}

LogOptions is a common interface to allow adjusting the logger.

type Logger added in v0.16.0

type Logger struct {
	*Slog
	LogOptions
}

Logger is a logger and its config holder struct.

func Log added in v0.16.0

func Log() *Logger

Log returns the default logger.

func NewConsoleLogger added in v0.16.0

func NewConsoleLogger(w io.Writer) *Logger

NewConsoleLogger creates a default console logger.

func NewJSONHandlerLogger added in v0.16.0

func NewJSONHandlerLogger(w io.Writer) *Logger

NewJSONHandlerLogger creates a logger with a io.Writer and JSON output.

func NewTextHandlerLogger added in v0.16.0

func NewTextHandlerLogger(w io.Writer) *Logger

NewTextHandlerLogger creates a logger with a io.Writer and plain slog output.

type ManagedFS added in v0.5.5

type ManagedFS interface {
	fs.FS
	FS() fs.FS
}

ManagedFS is a common interface for FS registered in launchr.

type MapItem added in v0.16.3

type MapItem[K, V any] struct {
	K K // K is a key of the map item.
	V V // V is a value of the map item.
}

MapItem is a helper struct used to return an ordered map as a slice.

type OnAppInitPlugin added in v0.1.0

type OnAppInitPlugin interface {
	Plugin
	// OnAppInit is hook function called on application initialisation.
	// Plugins may save app global object, retrieve or provide services here.
	OnAppInit(app App) error
}

OnAppInitPlugin is an interface to implement a plugin for app initialisation.

type Out added in v0.16.0

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

Out is an output stream used by the app to write normal program output.

func NewOut added in v0.16.0

func NewOut(out io.Writer) *Out

NewOut returns a new Out object from a io.Writer.

func (*Out) FD added in v0.16.0

func (s *Out) FD() uintptr

FD returns the file descriptor number for this stream.

func (*Out) GetTtySize added in v0.16.0

func (o *Out) GetTtySize() (uint, uint)

GetTtySize returns the height and width in characters of the tty.

func (*Out) IsTerminal added in v0.16.0

func (s *Out) IsTerminal() bool

IsTerminal returns true if this stream is connected to a terminal.

func (*Out) RestoreTerminal added in v0.16.0

func (s *Out) RestoreTerminal()

RestoreTerminal restores normal mode to the terminal.

func (*Out) SetIsTerminal added in v0.16.0

func (s *Out) SetIsTerminal(isTerminal bool)

SetIsTerminal sets the boolean used for isTerminal.

func (*Out) SetRawTerminal added in v0.16.0

func (o *Out) SetRawTerminal() (err error)

SetRawTerminal sets raw mode on the input terminal.

func (*Out) Write added in v0.16.0

func (o *Out) Write(p []byte) (int, error)

type Plugin added in v0.1.0

type Plugin interface {
	// PluginInfo requests a type to provide information about the plugin.
	PluginInfo() PluginInfo
}

Plugin is a common interface for launchr plugins.

type PluginInfo added in v0.1.0

type PluginInfo struct {
	// Weight defines the order of plugins calling. @todo rework to a real dependency resolving.
	Weight int
	// contains filtered or unexported fields
}

PluginInfo provides information about the plugin and is used as a unique data to identify a plugin.

func (PluginInfo) GetPackagePath added in v0.9.0

func (p PluginInfo) GetPackagePath() string

GetPackagePath returns the package path of the PluginInfo.

func (PluginInfo) String added in v0.1.0

func (p PluginInfo) String() string

type PluginManager added in v0.1.0

type PluginManager interface {
	Service
	All() PluginsMap
}

PluginManager handles plugins.

func NewPluginManagerWithRegistered added in v0.1.0

func NewPluginManagerWithRegistered() PluginManager

NewPluginManagerWithRegistered creates PluginManager with registered plugins.

type PluginsMap added in v0.1.0

type PluginsMap = map[PluginInfo]Plugin

PluginsMap is a type alias for plugins map.

type Service

type Service interface {
	ServiceInfo() ServiceInfo
}

Service is a common interface for a service to register.

type ServiceInfo

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

ServiceInfo provides service info for its initialization.

func (ServiceInfo) String added in v0.1.0

func (s ServiceInfo) String() string

type Slog added in v0.16.0

type Slog = slog.Logger

Slog is an alias for a go structured logger slog.Logger to reduce visible dependencies.

type Streams added in v0.16.0

type Streams interface {
	// In returns the reader used for stdin.
	In() *In
	// Out returns the writer used for stdout.
	Out() *Out
	// Err returns the writer used for stderr.
	Err() io.Writer
}

Streams is an interface which exposes the standard input and output streams.

func NoopStreams added in v0.16.0

func NoopStreams() Streams

NoopStreams provides streams like /dev/null.

func StandardStreams added in v0.16.0

func StandardStreams() Streams

StandardStreams sets a cli in, out and err streams with the standard streams.

type Template added in v0.16.0

type Template struct {
	Tmpl string // Tmpl is a template string.
	Data any    // Data is a template data.
}

Template provides templating functionality to generate files.

func (Template) Generate added in v0.16.0

func (t Template) Generate(w io.Writer) error

Generate executes a template and writes it.

func (Template) WriteFile added in v0.16.0

func (t Template) WriteFile(name string) error

WriteFile creates/overwrites a file and executes the template with it.

type Terminal added in v0.16.0

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

Terminal prints formatted text to the console.

func Term added in v0.16.0

func Term() *Terminal

Term returns default Terminal to print application messages to the console.

func (*Terminal) Basic added in v0.16.0

func (t *Terminal) Basic() TextPrinter

Basic returns a default basic printer.

func (*Terminal) DisableOutput added in v0.16.0

func (t *Terminal) DisableOutput()

DisableOutput disables the output.

func (*Terminal) EnableOutput added in v0.16.0

func (t *Terminal) EnableOutput()

EnableOutput enables the output.

func (*Terminal) Error added in v0.16.0

func (t *Terminal) Error() TextPrinter

Error returns a prefixed printer, which can be used to print text with an "error" prefix.

func (*Terminal) Info added in v0.16.0

func (t *Terminal) Info() TextPrinter

Info returns a prefixed printer, which can be used to print text with an "info" prefix.

func (*Terminal) Print added in v0.16.0

func (t *Terminal) Print(a ...any)

Print implements TextPrinter interface.

func (*Terminal) Printf added in v0.16.0

func (t *Terminal) Printf(format string, a ...any)

Printf implements TextPrinter interface.

func (*Terminal) Printfln added in v0.16.0

func (t *Terminal) Printfln(format string, a ...any)

Printfln implements TextPrinter interface.

func (*Terminal) Println added in v0.16.0

func (t *Terminal) Println(a ...any)

Println implements TextPrinter interface.

func (*Terminal) SetOutput added in v0.16.0

func (t *Terminal) SetOutput(w io.Writer)

SetOutput sets an output to target writer.

func (*Terminal) Success added in v0.16.0

func (t *Terminal) Success() TextPrinter

Success returns a prefixed printer, which can be used to print text with a "success" Prefix.

func (*Terminal) Warning added in v0.16.0

func (t *Terminal) Warning() TextPrinter

Warning returns a prefixed printer, which can be used to print text with a "warning" prefix.

func (*Terminal) Write added in v0.16.0

func (t *Terminal) Write(p []byte) (int, error)

Write implements io.Writer interface.

type TextPrinter added in v0.16.0

type TextPrinter interface {
	// SetOutput sets where the output will be printed.
	SetOutput(w io.Writer)

	// Print formats using the default formats for its operands and writes to standard output.
	// Spaces are added between operands when neither is a string.
	Print(a ...any)

	// Println formats using the default formats for its operands and writes to standard output.
	// Spaces are always added between operands and a newline is appended.
	Println(a ...any)

	// Printf formats according to a format specifier and writes to standard output.
	Printf(format string, a ...any)

	// Printfln formats according to a format specifier and writes to standard output.
	// Spaces are always added between operands and a newline is appended.
	Printfln(format string, a ...any)
}

TextPrinter contains methods to print formatted text to the console or return it as a string.

Jump to

Keyboard shortcuts

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