config

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

README

Go Config

Golang Configuration tool, used to parse toml files and load env args.

Usage

package main

import (
    "fmt"

	"github.com/x-punch/go-config"
)

func main() {
	var cfg Config
	if err := config.Load(&cfg, "config.toml"); err != nil {
		panic(err)
    }
    fmt.Println(cfg)
}

Options

package main

import (
    "fmt"

	"github.com/x-punch/go-config"
)

func main() {
	configor := config.NewConfigor(config.Files([]string{"example/config.toml"}), config.ShowLog(false), config.Prefix("GO"))
	var cfg Config
	if err := configor.Load(&cfg); err != nil {
		panic(err)
	}
	fmt.Println(cfg)
}

Environment

package main

import (
    "fmt"
    "os"

	"github.com/x-punch/go-config"
)

func main() {
    os.Setenv("CONFIG_FILES", "example/config.toml")
	os.Setenv("CONFIG_SHOW_LOG", "true")
	os.Setenv("CONFIG_PREFIX", "GO")
	os.Setenv("GO_TIMEOUT", "2h3m4s")
	var cfg Config
	if err := config.Load(&cfg); err != nil {
		panic(err)
	}
	fmt.Println(cfg)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(config interface{}, files ...string) error

Load will load configurations from files or load from env args

Types

type Configor

type Configor struct {
	Options *Options
}

Configor represents configuration loader

func NewConfigor

func NewConfigor(opts ...Option) *Configor

NewConfigor create configor instance

func (*Configor) ApplyEnvOverrides

func (c *Configor) ApplyEnvOverrides(prefix string, val interface{}) error

ApplyEnvOverrides apple env overrides

func (*Configor) Load

func (c *Configor) Load(config interface{}) error

Load will unmarshal configurations to struct from files that you provide

func (*Configor) Log added in v1.1.0

func (c *Configor) Log(format string, a ...interface{})

type Duration

type Duration time.Duration

Duration is a TOML wrapper type for time.Duration.

func (Duration) Duration added in v1.1.1

func (d Duration) Duration() time.Duration

Duration returns the time.Duration type.

func (Duration) MarshalText

func (d Duration) MarshalText() (text []byte, err error)

MarshalText converts a duration to a string for decoding toml

func (Duration) String

func (d Duration) String() string

String returns the string representation of the duration.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

UnmarshalText parses a TOML value into a duration value.

type FileMode

type FileMode uint32

FileMode represents file mode

func (FileMode) MarshalText

func (m FileMode) MarshalText() (text []byte, err error)

MarshalText convert a file mode to a string for decoding toml

func (*FileMode) UnmarshalText

func (m *FileMode) UnmarshalText(text []byte) error

UnmarshalText parses file mode from text

type Option

type Option func(*Options)

Option represents func to set options

func Files

func Files(files []string) Option

Files represents configuration files path

func LoadFromEnv added in v1.1.0

func LoadFromEnv(load bool) Option

LoadFromEnv represents wether load configuration from env args

func Prefix

func Prefix(prefix string) Option

Prefix represents env prefix load from env args

func ShowLog

func ShowLog(show bool) Option

ShowLog represents wether show logs of configuration

type Options

type Options struct {
	Files       []string
	Prefix      string
	ShowLog     bool
	LoadFromEnv bool
}

Options represents options of configor

type Size

type Size uint64

Size represents a TOML parseable file size. Users can specify size using "k" or "K" for kibibytes, "m" or "M" for mebibytes, and "g" or "G" for gibibytes. If a size suffix isn't specified then bytes are assumed.

func (*Size) UnmarshalText

func (s *Size) UnmarshalText(text []byte) error

UnmarshalText parses a byte size from text.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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