appext

package
v1.32.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 24 Imported by: 1

Documentation

Overview

Package appext contains functionality to work with flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(ctx context.Context, container NameContainer, defaultPort uint16) (net.Listener, error)

Listen listens on the container's port, falling back to defaultPort.

func ReadConfig

func ReadConfig(container NameContainer, value interface{}) error

ReadConfig reads the configuration from the YAML configuration file config.yaml in the configuration directory.

If the file does not exist, this is a no-op. The value should be a pointer to unmarshal into.

func ReadSecret

func ReadSecret(container NameContainer, name string) (string, error)

ReadSecret returns the contents of the file at path filepath.Join(container.ConfigDirPath(), secretRelDirPath, name).

func WriteConfig

func WriteConfig(container NameContainer, value interface{}) error

WriteConfig writes the configuration to the YAML configuration file config.yaml in the configuration directory.

The directory is created if it does not exist. The value should be a pointer to marshal.

Types

type Builder

type Builder interface {
	BindRoot(flagSet *pflag.FlagSet)
	SubCommandBuilder
}

Builder builds run functions for both top-level commands and sub-commands.

func NewBuilder

func NewBuilder(appName string, options ...BuilderOption) Builder

NewBuilder returns a new Builder.

type BuilderOption

type BuilderOption func(*builder)

BuilderOption is an option for a new Builder

func BuilderWithDefaultLogLevel

func BuilderWithDefaultLogLevel(defaultLogLevel zapcore.Level) BuilderOption

BuilderWithDefaultLogLevel adds the given default log level.

func BuilderWithInterceptor

func BuilderWithInterceptor(interceptor Interceptor) BuilderOption

BuilderWithInterceptor adds the given interceptor for all run functions.

func BuilderWithTimeout

func BuilderWithTimeout(defaultTimeout time.Duration) BuilderOption

BuilderWithTimeout returns a new BuilderOption that adds a timeout flag and the default timeout.

func BuilderWithTracing

func BuilderWithTracing() BuilderOption

BuilderWithTracing enables zap tracing for the builder.

type Container

Container contains not just the base app container, but all extended containers.

func NewContainer

func NewContainer(
	baseContainer app.Container,
	appName string,
	logger *zap.Logger,
	verbosePrinter verbose.Printer,
) (Container, error)

NewContainer returns a new Container.

type Interceptor

type Interceptor func(func(context.Context, Container) error) func(context.Context, Container) error

Interceptor intercepts and adapts the request or response of run functions.

type LoggerContainer

type LoggerContainer interface {
	Logger() *zap.Logger
}

LoggerContainer provides a *zap.Logger.

func NewLoggerContainer

func NewLoggerContainer(logger *zap.Logger) LoggerContainer

NewLoggerContainer returns a new LoggerContainer.

type NameContainer

type NameContainer interface {
	// AppName is the application name.
	//
	// The name must be in [a-zA-Z0-9-_].
	AppName() string
	// ConfigDirPath is the config directory path for the named application.
	//
	// First checks for $APP_NAME_CONFIG_DIR.
	// If this is not set, uses app.ConfigDirPath()/app-name.
	// Unnormalized.
	ConfigDirPath() string
	// CacheDirPath is the cache directory path for the named application.
	//
	// First checks for $APP_NAME_CACHE_DIR.
	// If this is not set, uses app.CacheDirPath()/app-name.
	// Unnormalized.
	CacheDirPath() string
	// DataDirPath is the data directory path for the named application.
	//
	// First checks for $APP_NAME_DATA_DIR.
	// If this is not set, uses app.DataDirPath()/app-name.
	// Unnormalized.
	DataDirPath() string
	// Port is the port to use for serving.
	//
	// First checks for $APP_NAME_PORT.
	// If this is not set, checks for $PORT.
	// If this is not set, returns 0, which means no port is known.
	// Returns error on parse.
	Port() (uint16, error)
}

NameContainer is a container for named applications.

Application name foo-bar translates to environment variable prefix FOO_BAR_, which is used for the various functions that NameContainer provides.

func NewNameContainer

func NewNameContainer(envContainer app.EnvContainer, appName string) (NameContainer, error)

NewNameContainer returns a new NameContainer.

The name must be in [a-zA-Z0-9-_].

type SubCommandBuilder

type SubCommandBuilder interface {
	NewRunFunc(func(context.Context, Container) error) func(context.Context, app.Container) error
}

SubCommandBuilder builds run functions for sub-commands.

type TracerContainer

type TracerContainer interface {
	Tracer() trace.Tracer
}

TracerContainer provides a trace.Tracer based on the application name.

func NewTracerContainer

func NewTracerContainer(appName string) TracerContainer

NewTracerContainer returns a new TracerContainer for the application name.

type VerboseContainer

type VerboseContainer interface {
	// VerboseEnabled returns true if verbose mode is enabled.
	VerboseEnabled() bool
	// VerbosePrinter returns a verbose.Printer to use for verbose printing.
	VerbosePrinter() verbose.Printer
}

VerboseContainer provides a verbose.Printer.

func NewVerboseContainer

func NewVerboseContainer(verbosePrinter verbose.Printer) VerboseContainer

NewVerboseContainer returns a new VerboseContainer.

Jump to

Keyboard shortcuts

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