config

package
v2.23.3-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Overview

+kubebuilder:validation:Optional

Index

Constants

View Source
const (
	// DefaultConfigFileExt is default config file extension.
	DefaultConfigFileExt = ".yaml"
	// DefaultKoanfDelim is default koanf delimiter.
	DefaultKoanfDelim = "."
)
View Source
const (
	// ConfigPathFlag is the name of the flag for the configuration path.
	ConfigPathFlag = "config_path"
)

Variables

View Source
var (
	// DefaultAssetsDirectory is path to default assets directory.
	DefaultAssetsDirectory = path.Join("/", "etc", info.Prefix, info.Service)
	// DefaultLogDirectory is path to default log directory.
	DefaultLogDirectory = path.Join("/", "var", "log", info.Prefix, info.Service)
	// DefaultTempBase is path to default temporary base.
	DefaultTempBase = path.Join(os.TempDir(), info.Prefix, info.Service)
	// DefaultTempDirectory is path to default temporary directory.
	DefaultTempDirectory = path.Join(DefaultTempBase, guuid.NewString())
	// DefaultConfigDirectory is path to default config directory.
	DefaultConfigDirectory = path.Join(DefaultAssetsDirectory, "config")
	// EnvPrefix is default environment prefix.
	EnvPrefix = strings.ReplaceAll(strings.ToUpper(info.Service), "-", "_") + "_"
)

Functions

func FxLogger

func FxLogger() func(lg *log.Logger) fxevent.Logger

FxLogger overrides fx default logger.

func GetBoolValue

func GetBoolValue(unmarshaller Unmarshaller, key string, defaultVal bool) bool

GetBoolValue returns the boolean value for the given key.

func GetIntValue

func GetIntValue(unmarshaller Unmarshaller, key string, defaultVal int) int

GetIntValue returns the integer value for the given key.

func GetStringValue

func GetStringValue(unmarshaller Unmarshaller, key string, defaultVal string) string

GetStringValue returns the string value for the given key.

func GetValue

func GetValue(unmarshaller Unmarshaller, key string, defaultVal interface{}) interface{}

GetValue returns the value for the given key if config key is present.

func GroupTag

func GroupTag(group string) string

GroupTag returns the group tag for the given group name in the manner of fmt.Sprintf.

func LogModule

func LogModule() fx.Option

LogModule is a fx module that provides a logger and invokes setting global and standard loggers.

func NameTag

func NameTag(name string) string

NameTag returns the name tag for the given name in the manner of fmt.Sprintf.

func NewLogger

func NewLogger(config LogConfig, w []io.Writer, isGlobalLogger bool) (*log.Logger, io.Writer)

NewLogger creates a new logger instances with the provided config, and a list of additional writers.

func RegisterDeprecatedValidator

func RegisterDeprecatedValidator(warnOnly bool) error

RegisterDeprecatedValidator a function to validate "deprecated". If deprecated constraint exists then fail the validation.

func SetDefaults

func SetDefaults(variable interface{})

SetDefaults applies the default values to the struct object, the struct type must have the StructTag with name "default" and the directed value. Example usage:

type ExampleBasic struct {
    Foo bool   `default:"true"`
    Bar string `default:"33"`
    Qux int8
    Dur time.Duration `default:"2m3s"`
}

 foo := &ExampleBasic{}
 SetDefaults(foo)

func UnmarshalJSON

func UnmarshalJSON(bytes []byte, output interface{}) error

UnmarshalJSON unmarshals using _just_ bytes as source of truth (no env, no flags, no other overrides).

func UnmarshalYAML

func UnmarshalYAML(bytes []byte, output interface{}) error

UnmarshalYAML unmarshals using _just_ bytes as source of truth (no env, no flags, no other overrides).

func ValidateStruct

func ValidateStruct(rawVal interface{}) error

ValidateStruct takes interface value and validates its fields of a struct.

Types

type CommandLineConfig

type CommandLineConfig struct {
	UnknownFlags bool
	ExitOnHelp   bool
}

CommandLineConfig holds configuration for CommandLine.

func (CommandLineConfig) NewCommandLine

func (config CommandLineConfig) NewCommandLine(cl CommandLineIn) (CommandLineOut, error)

NewCommandLine returns a new CommandLineOut with new FlagSet.

type CommandLineIn

type CommandLineIn struct {
	fx.In
	// Builders help platform users set flags
	Builders []FlagSetBuilder `group:"FlagSetBuilders"`
	// pFlag error handling mode
	ErrorHandling pflag.ErrorHandling `optional:"true"`
}

CommandLineIn holds parameters for NewCommandLine.

type CommandLineOut

type CommandLineOut struct {
	fx.Out
	FlagSet *pflag.FlagSet
}

CommandLineOut holds the output of NewCommandLine, set of defined flags.

type ConfigFormat

type ConfigFormat string

ConfigFormat specifies the type of the configuration format in string.

const (
	// YAML is one of config formats.
	YAML ConfigFormat = "yaml"
	// JSON is one of config formats.
	JSON ConfigFormat = "json"
)

type ConfigPath

type ConfigPath struct {
	Path string
}

ConfigPath stores the path to config directory for other modules to look for their config files.

type Duration

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

Duration is encoded as a string message which represents a signed span of time. It holds *durationpb.Duration which is generated type for google/protobuf/duration.proto. swagger:strfmt string +kubebuilder:validation:Type=string

func MakeDuration

func MakeDuration(duration time.Duration) Duration

MakeDuration returns a new Duration.

func (*Duration) AsDuration

func (d *Duration) AsDuration() time.Duration

AsDuration returns the Duration as a time.Duration.

func (*Duration) DeepCopy

func (d *Duration) DeepCopy() *Duration

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Duration.

func (*Duration) DeepCopyInto

func (d *Duration) DeepCopyInto(out *Duration)

DeepCopyInto deepcopy function, copying the receiver, writing into out.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON writes a Duration in JSON format.

func (Duration) String

func (d Duration) String() string

String returns the string representation of a Duration.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals and reads given bytes into a new Duration proto message.

type FileUnmarshallerConstructor

type FileUnmarshallerConstructor struct {
	// Optional Merge Config
	MergeConfig map[string]interface{}
	// Config Name -- config file name without the extension -- it is also the annotated name of koanf instance
	Name string
	// Command line flag for reading file path
	PathFlag string
	// If flag is empty or not provided on CL, fallback to explicit path
	Path string
	// Extension of file (exact) - empty = yaml
	FileExt string
	// Enable AutomaticEnv
	EnableEnv bool
	// NoErrOnFile
	NoErrOnFile bool
}

FileUnmarshallerConstructor holds fields to create an annotated instance of FileUnmarshaller.

func (FileUnmarshallerConstructor) Annotate

func (constructor FileUnmarshallerConstructor) Annotate() fx.Option

Annotate creates an annotated instance of FileUnmarshaller.

func (FileUnmarshallerConstructor) NewFileUnmarshaller

func (constructor FileUnmarshallerConstructor) NewFileUnmarshaller(flagSet *pflag.FlagSet) (Unmarshaller, ConfigPath, error)

NewFileUnmarshaller creates a new instance of FileUnmarshaller and ConfigPath that unmarshals the config file.

type FlagSetBuilder

type FlagSetBuilder func(*pflag.FlagSet) error

FlagSetBuilder is a function that helps users to build a Flagset.

type FlagSetBuilderOut

type FlagSetBuilderOut struct {
	fx.Out
	Builder FlagSetBuilder `group:"FlagSetBuilders"`
}

FlagSetBuilderOut wraps the group of FlagSetBuilder and makes it handy to provide FlagSetBuilder via Fx.

type KoanfUnmarshaller

type KoanfUnmarshaller struct {
	sync.Mutex
	// contains filtered or unexported fields
}

KoanfUnmarshaller backed by viper + validator + go-defaults.

func (*KoanfUnmarshaller) Get

func (u *KoanfUnmarshaller) Get(key string) interface{}

Get returns an interface value for the given key path in the config map.

func (*KoanfUnmarshaller) IsSet

func (u *KoanfUnmarshaller) IsSet(key string) bool

IsSet checks if the given key is set in the config map.

func (*KoanfUnmarshaller) Marshal

func (u *KoanfUnmarshaller) Marshal() ([]byte, error)

Marshal returns the underlying koanf as bytes.

func (*KoanfUnmarshaller) Reload

func (u *KoanfUnmarshaller) Reload(bytes []byte) error

Reload reloads the config using the underlying koanf.

func (*KoanfUnmarshaller) Unmarshal

func (u *KoanfUnmarshaller) Unmarshal(i interface{}) error

Unmarshal unmarshals given i using the underlying koanf.

func (*KoanfUnmarshaller) UnmarshalKey

func (u *KoanfUnmarshaller) UnmarshalKey(keyPath string, i interface{}) error

UnmarshalKey binds the given interface to the given key path in the config map and unmarshals given i using the underlying koanf with additional configuration.

type KoanfUnmarshallerConstructor

type KoanfUnmarshallerConstructor struct {
	// Command line flags
	FlagSet *pflag.FlagSet
	// Optional Merge Config
	MergeConfig map[string]interface{}
	// Config format (yaml, json)
	ConfigFormat ConfigFormat
	// Enable AutomaticEnv
	EnableEnv bool
}

KoanfUnmarshallerConstructor holds fields to create an annotated instance of KoanfUnmarshaller.

func (KoanfUnmarshallerConstructor) NewKoanfUnmarshaller

func (constructor KoanfUnmarshallerConstructor) NewKoanfUnmarshaller(bytes []byte) (Unmarshaller, error)

NewKoanfUnmarshaller creates a new Unmarshaller instance that can be used to unmarshal configs.

type LogConfig

type LogConfig struct {
	// Log level
	LogLevel string `` /* 172-byte string literal not displayed */

	// Log writers
	Writers []LogWriterConfig `json:"writers,omitempty" validate:"omitempty,dive,omitempty"`

	// Use non-blocking log writer (can lose logs at high throughput)
	NonBlocking bool `json:"non_blocking" default:"true"`

	// Additional log writer: pretty console (`stdout`) logging (not recommended for prod environments)
	PrettyConsole bool `json:"pretty_console" default:"false"`
}

LogConfig holds configuration for a logger and log writers. swagger:model +kubebuilder:object:generate=true

func (*LogConfig) DeepCopy

func (in *LogConfig) DeepCopy() *LogConfig

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LogConfig.

func (*LogConfig) DeepCopyInto

func (in *LogConfig) DeepCopyInto(out *LogConfig)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

type LogWriterConfig

type LogWriterConfig struct {
	// Output file for logs. Keywords allowed - [`stderr`, `default`]. `default` maps to `/var/log/fluxninja/<service>.log`
	File string `json:"file" default:"stderr"`
	// Log file max size in MB
	MaxSize int `json:"max_size" validate:"gte=0" default:"50"`
	// Max log file backups
	MaxBackups int `json:"max_backups" validate:"gte=0" default:"3"`
	// Max age in days for log files
	MaxAge int `json:"max_age" validate:"gte=0" default:"7"`
	// Compress
	Compress bool `json:"compress" default:"false"`
}

LogWriterConfig holds configuration for a log writer. swagger:model +kubebuilder:object:generate=true

func (*LogWriterConfig) DeepCopy

func (in *LogWriterConfig) DeepCopy() *LogWriterConfig

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LogWriterConfig.

func (*LogWriterConfig) DeepCopyInto

func (in *LogWriterConfig) DeepCopyInto(out *LogWriterConfig)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

type LoggerConstructor

type LoggerConstructor struct {
	// Name of logger instance
	Name string
	// Config key
	ConfigKey string
	// Default Config
	DefaultConfig LogConfig
	// Global logger
	IsGlobal bool
}

LoggerConstructor holds fields used to create an annotated instance of a logger.

func (LoggerConstructor) Annotate

func (constructor LoggerConstructor) Annotate() fx.Option

Annotate creates an annotated instance of loggers which can be used to create multiple loggers.

type ModuleConfig

type ModuleConfig struct {
	MergeConfig  map[string]interface{}
	UnknownFlags bool
	ExitOnHelp   bool
}

ModuleConfig holds configuration for the config module.

func (ModuleConfig) Module

func (config ModuleConfig) Module() fx.Option

Module is a fx Module that invokes CreateGlobalPanicHandlerRegistry and provides an annotated instance of CommandLine and FileUnmarshaller.

type ProtobufUnmarshaller

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

ProtobufUnmarshaller is an unmarshaller that can unmarshalls protobuf-encoded data into messages.

No defaults handling nor validation is performed.

Note: Unmarshalling keys or unmarshalling into non-protobuf structs in not supported by this unmarshaller.

func (*ProtobufUnmarshaller) Get

func (u *ProtobufUnmarshaller) Get(key string) interface{}

Get is not supported by ProtobufUnmarshaler.

func (*ProtobufUnmarshaller) IsSet

func (u *ProtobufUnmarshaller) IsSet(key string) bool

IsSet is not supported by ProtobufUnmarshaler.

func (*ProtobufUnmarshaller) Marshal

func (u *ProtobufUnmarshaller) Marshal() ([]byte, error)

Marshal the config into bytes.

func (*ProtobufUnmarshaller) Reload

func (u *ProtobufUnmarshaller) Reload(bytes []byte) error

Reload sets the protobuf-encoded bytes.

Previous state is completely forgotten.

func (*ProtobufUnmarshaller) Unmarshal

func (u *ProtobufUnmarshaller) Unmarshal(output interface{}) error

Unmarshal unmarshals previously set protobuf-encoded bytes into output.

Output should be a proto.Message.

func (*ProtobufUnmarshaller) UnmarshalKey

func (u *ProtobufUnmarshaller) UnmarshalKey(key string, output interface{}) error

UnmarshalKey is not supported by ProtobufUnmarshaler.

type Time

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

Time is encoded as a string message which represents a point in time independent of any time zone. It holds *timestamppb.Time which is generated types for google/protobuf/timestamp.proto. swagger:strfmt string

func MakeTime

func MakeTime(timestamp time.Time) Time

MakeTime returns a new Timestamp.

func (*Time) AsTime

func (t *Time) AsTime() time.Time

AsTime returns the Timestamp as a time.Time.

func (*Time) DeepCopy

func (t *Time) DeepCopy() *Time

DeepCopy deepcopy function for Timestamp.

func (*Time) DeepCopyInto

func (t *Time) DeepCopyInto(out *Time)

DeepCopyInto deepcopy function for Timestamp.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON writes a Timestamp in JSON format.

func (Time) String

func (t Time) String() string

String returns the string representation of a Timestamp.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals and reads given bytes into a new Timestamp proto message.

type Unmarshaller

type Unmarshaller interface {
	// Check whether config key is present
	IsSet(string) bool
	// Unmarshal the key into a user provided config struct
	UnmarshalKey(string, interface{}) error
	// Get specific key from config -- use cast lib to convert to bool, string etc.
	Get(string) interface{}
	// Unmarshal entire config into a struct
	Unmarshal(interface{}) error
	// Reload config
	Reload(bytes []byte) error
	// Marshal the config into bytes
	Marshal() ([]byte, error)
}

Unmarshaller provides common interface for unmarshallers.

func NewProtobufUnmarshaller

func NewProtobufUnmarshaller(bytes []byte) (Unmarshaller, error)

NewProtobufUnmarshaller crates a new ProtobufUnmarshaller.

Jump to

Keyboard shortcuts

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