cli

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: Apache-2.0 Imports: 11 Imported by: 21

Documentation

Overview

Package cli provides the cli for go-orb.

Index

Constants

View Source
const MainActionName = "__main"

MainActionName is the name of the "main" action.

Variables

This section is empty.

Functions

func FlagValue

func FlagValue[T any](f *Flag) (T, error)

FlagValue gets a value back from a Flag and enforces types.

Types

type App

type App struct {
	Name     string
	Version  string
	Usage    string
	Commands []*Command
	Flags    []*Flag

	// NoMultiServiceConfig defines if the config is used with sections or without.
	// If false, the config is used with sections.
	// For example:
	// “`yaml
	// service1: # service1 section
	//   logger:
	//     level: INFO
	// service2: # service2 section
	//   logger:
	//     level: INFO
	// “`
	//
	// If true, the config is used without sections.
	// For example:
	// “`yaml
	// logger:
	//   level: INFO
	// “`
	NoMultiServiceConfig bool

	// NoAction defines if there will be no main action.
	NoAction bool

	// NoGlobalConfig defines if the global config flag should be added and parsed.
	NoGlobalConfig bool

	// HardcodedConfigs defines the hardcoded configs.
	HardcodedConfigs []HardcodedConfig
	// HardcodedConfigURLs defines the hardcoded config URLs.
	HardcodedConfigURLs []string

	// Internal
	InternalAction func() error
}

App represents a CLI Application.

type AppConfigData

type AppConfigData map[string]any

AppConfigData is the config data type.

func ProvideAppConfigData

func ProvideAppConfigData(appContext *AppContext) (AppConfigData, error)

ProvideAppConfigData provides config data from appContext and flags.

type AppContext

type AppContext struct {
	SelectedService string
	SelectedCommand []string

	Context       context.Context
	StopWaitGroup *sync.WaitGroup
	// contains filtered or unexported fields
}

AppContext holds app state of the main application for multiple services.

func NewAppContext

func NewAppContext(app *App) *AppContext

NewAppContext creates a new Application context.

func (*AppContext) App

func (c *AppContext) App() *App

App returns the application.

func (*AppContext) ExitGracefully

func (c *AppContext) ExitGracefully(exitCode int)

ExitGracefully exits the application gracefully.

func (*AppContext) Name

func (c *AppContext) Name() string

Name returns the name of the application.

func (*AppContext) Version

func (c *AppContext) Version() string

Version returns the version of the application.

type Command

type Command struct {
	Name        string
	Service     string
	Category    string
	Usage       string
	Flags       []*Flag
	Subcommands []*Command
	NoAction    bool

	// Internal
	InternalAction func() error
}

Command is a CLI Command for App.

type Flag

type Flag struct {
	Name    string
	EnvVars []string
	Usage   string

	// The path in map(\[string\])+any
	ConfigPaths [][]string

	Default any
	Value   any
}

Flag is a Cli Flag and maybe environment variable.

func NewFlag

func NewFlag[T any](
	name string,
	defaultValue T,
	opts ...FlagOption,
) *Flag

NewFlag creates a new CLI flag.

func ProvideParsedFlagsFromArgs

func ProvideParsedFlagsFromArgs(appContext *AppContext, parser ParserFunc, args []string) ([]*Flag, error)

ProvideParsedFlagsFromArgs provides parsed flags from the app context.

func (*Flag) Clear

func (f *Flag) Clear()

Clear clears the internal value.

func (*Flag) String

func (f *Flag) String() string

type FlagOption

type FlagOption func(*Flag)

FlagOption is an option for NewFlag.

func FlagConfigPaths

func FlagConfigPaths(n ...[]string) FlagOption

FlagConfigPaths appends the config paths for the flag.

func FlagDefault

func FlagDefault[T any](n T) FlagOption

FlagDefault sets the flags default.

func FlagEnvVars

func FlagEnvVars(n ...string) FlagOption

FlagEnvVars set's environment variables for the flag.

func FlagUsage

func FlagUsage(n string) FlagOption

FlagUsage set's the usage string for the flag.

type HardcodedConfig

type HardcodedConfig struct {
	Format string
	Data   string
}

HardcodedConfig represents a hardcoded config with it's format. Format can be any of the importet codecs.

type ParserFunc

type ParserFunc func(appContext *AppContext, args []string) ([]*Flag, error)

ParserFunc is a provider function type for parsing flags.

type ServiceContext

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

ServiceContext holds the service state.

func NewServiceContext

func NewServiceContext(appContext *AppContext, name string, version string) *ServiceContext

NewServiceContext creates a new Service context for the given service.

func ProvideSingleServiceContext

func ProvideSingleServiceContext(appContext *AppContext) (*ServiceContext, error)

ProvideSingleServiceContext provides a single service context for the application.

func (*ServiceContext) App

func (c *ServiceContext) App() *App

App returns the application.

func (*ServiceContext) AppName

func (c *ServiceContext) AppName() string

AppName returns the name of the application.

func (*ServiceContext) AppVersion

func (c *ServiceContext) AppVersion() string

AppVersion returns the version of the application.

func (*ServiceContext) Context

func (c *ServiceContext) Context() context.Context

Context returns the context of the application.

func (*ServiceContext) ExitAppGracefully

func (c *ServiceContext) ExitAppGracefully(exitCode int)

ExitAppGracefully exits the application gracefully.

func (*ServiceContext) Name

func (c *ServiceContext) Name() string

Name returns the name of the service.

func (*ServiceContext) StopWaitGroup

func (c *ServiceContext) StopWaitGroup() *sync.WaitGroup

StopWaitGroup returns the stop wait group of the application.

func (*ServiceContext) Version

func (c *ServiceContext) Version() string

Version returns the version of the service.

type ServiceContextWithConfig

type ServiceContextWithConfig struct {
	*ServiceContext
	// contains filtered or unexported fields
}

ServiceContextWithConfig is a ServiceContext with a config.

func NewServiceContextWithConfig

func NewServiceContextWithConfig(appContext *AppContext, name string, version string, configData map[string]any) *ServiceContextWithConfig

NewServiceContextWithConfig creates a new Service context for the given service with config.

func ProvideServiceConfigData

func ProvideServiceConfigData(
	serviceContext *ServiceContext,
	appConfigData AppConfigData,
	flags []*Flag,
) (*ServiceContextWithConfig, error)

ProvideServiceConfigData provides config data to serviceContext from flags.

func (*ServiceContextWithConfig) Config

func (c *ServiceContextWithConfig) Config() map[string]any

Config returns the configuration of the service.

Jump to

Keyboard shortcuts

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