config

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 10 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init[Config any](flagSet *flag.FlagSet, cfg Configurator[Config], options ...Option) error

Init initializes Configure cfg from command line flags with options

func IsExitError

func IsExitError(err error) (code int, ok bool)

func Load added in v0.3.5

func Load[T any](cfg Configurator[T], source string, optional bool) error

Load reads config from source

Types

type BasicConfig added in v0.1.2

type BasicConfig[Self any] struct {

	// Core represents core common fields
	Core CoreConfig `json:"core"`
	// contains filtered or unexported fields
}

BasicConfig implments Configurator

func (*BasicConfig[Self]) GetCore added in v0.1.2

func (c *BasicConfig[Self]) GetCore() *CoreConfig

GetCore implements Configurator GetCore method

func (*BasicConfig[Self]) GetSource added in v0.1.2

func (c *BasicConfig[Self]) GetSource() string

GetSource implements Configurator GetSource method

func (*BasicConfig[Self]) OnLoad added in v0.3.5

func (c *BasicConfig[Self]) OnLoad(old Self) error

func (*BasicConfig[Self]) Read added in v0.1.2

func (c *BasicConfig[Self]) Read(self Self, r io.Reader) error

Read implements Configurator Read method

func (*BasicConfig[Self]) SetSource added in v0.1.2

func (c *BasicConfig[Self]) SetSource(source string)

SetSource implements Configurator SetSource method

func (*BasicConfig[Self]) Write added in v0.1.2

func (c *BasicConfig[Self]) Write(self Self, w io.Writer) error

Write implements Configurator Write method

type Configurator

type Configurator[T any] interface {
	// Self returns self
	Self() T
	// Default returns default configuration
	Default() Configurator[T]

	// Read reads config from reader r
	Read(T, io.Reader) error
	// Write writes config to writer w
	Write(T, io.Writer) error
	// GetSource returns source of config
	GetSource() string
	// SetSource sets source of config
	SetSource(string)
	// GetCore returns core configuration
	GetCore() *CoreConfig

	// OnLoad fired after configuration loaded
	OnLoad(previous T) error
}

Configurator represents generic config of application

type CoreConfig added in v0.0.18

type CoreConfig struct {
	Project   string          `json:"project"`
	Mode      Mode            `json:"mode"`
	Name      string          `json:"name"`
	ID        int64           `json:"id"`
	Log       LogConfig       `json:"log"`
	MQ        MQConfig        `json:"mq"`
	Discovery DiscoveryConfig `json:"discovery"`
}

Core configuration

type Discoverable

type Discoverable interface {
	// DiscoveredContent returns a discovered data
	DiscoveredContent() any
}

Discoverable represents a discoverable configurator

type DiscoveryConfig

type DiscoveryConfig struct {
	Off    bool   `json:"off"`
	Name   string `json:"name"`
	Source string `json:"source"`
}

DiscoveryConfig ...

type LogConfig

type LogConfig struct {
	// Prefix to preappend to each log message
	Prefix string `json:"prefix"`
	// Level of log, reload supported
	Level string `json:"level"`
	// Flags of log printer, reload supported
	// @see githug.com/gopherd/log@Flags.
	// -1: no flags
	//  0: default flags
	Flags int `json:"flags"`

	// Writers specified multi-writers, like:
	//	[
	//		"console",
	//		"file:path/to/filename?suffix=.txt"
	//	]
	Writers []string `json:"writers"`
}

LogConfig represents configuration of log

func (LogConfig) FixedFlags added in v0.0.18

func (cfg LogConfig) FixedFlags() int

type MQConfig

type MQConfig struct {
	Off    bool   `json:"off"`
	Name   string `json:"name"`
	Source string `json:"source"`
}

MQConfig ...

type Mode

type Mode int

Process running mode

const (
	Dev Mode = iota
	Preview
	Prod
)

func (Mode) MarshalJSON

func (mode Mode) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler MarshalJSON method

func (*Mode) UnmarshalJSON

func (mode *Mode) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler UnmarshalJSON method

type Option

type Option func(*option)

Option is the option for Init

func WithDefaultSource

func WithDefaultSource(source string) Option

WithDefaultSource specify default config source

func WithInput

func WithInput(flag, usage string) Option

WithInput specify command line flag name and usage for config input

func WithOutput

func WithOutput(flag, usage string) Option

WithOutput specify command line flag name and usage for config output

func WithVersion

func WithVersion(flag, usage string) Option

WithVersion specify command line flag name and usage for version

Jump to

Keyboard shortcuts

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