resolve

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Overview

Package resolve provides a shorthand resolution for command context, config and logger as a one-liner.

This factorizes common CLI use-cases where injected stuff boils down to just a logger and a configuration registry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectedSLogConfig

func InjectedSLogConfig(c *cobra.Command, opts ...Option) (context.Context, *slog.Logger, *viper.Viper)

ResolveInjectSLogConfig returns in a single call the standard structure logger and viper config stored in the command context as injectables.

Options may specify defaults whenever these items cannot be found in the context.

func InjectedZapConfig

func InjectedZapConfig(c *cobra.Command, opts ...Option) (context.Context, *zap.Logger, *viper.Viper)

ResolveInjectZapConfig returns in a single call the zap logger and viper config stored in the command context as injectables.

Options may specify defaults whenever these items cannot be found in the context.

func ReinjectSLogger

func ReinjectSLogger(c *cobra.Command, slg *slog.Logger) context.Context

ReinjectSLogger updates the command context with a new logger.

It returns the new context of this command.

This is useful for instance to relevel a logger after CLI flags parsing.

func ReinjectZapLogger

func ReinjectZapLogger(c *cobra.Command, zlg *zap.Logger) context.Context

ReinjectZapLogger updates the command context with a new logger.

It returns the new context of this command.

This is useful for instance to relevel a logger after CLI flags parsing.

func RelevelInjectedZapLogger

func RelevelInjectedZapLogger(c *cobra.Command, level string, opts ...Option) (context.Context, *zap.Logger)

RelevelInjectedZapLogger reinjects into the command's context a relevel logger with a new level.

NOTE: the new level can only be higher (more restrictive) than the initially configured one, otherwise this is a no-op.

Types

type Option

type Option func(*options)

Option to tune the retrieval of injected dependencies from a command's context

func WithConfigDefaulter

func WithConfigDefaulter(defaulter func() *viper.Viper) Option

WithConfigDefaulter applies a default config generation whenever the injected context is not found.

This guarantees on the presence of a config.

func WithSLoggerDefaulter

func WithSLoggerDefaulter(defaulter func() *slog.Logger) Option

WithSLoggerDefaulter applies a default logger generation whenever the injected context is not found.

This guarantees the presence of a logger.

func WithZapLoggerDefaulter

func WithZapLoggerDefaulter(defaulter func() *zap.Logger) Option

WithZapLoggerDefaulter applies a default logger generation whenever the injected context is not found.

This guarantees the presence of a logger.

Jump to

Keyboard shortcuts

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