config

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: MIT Imports: 16 Imported by: 8

README

Config - A minimalist Go configuration library

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Features
  • Support Types
    • YAML
    • JSON
    • TOML
    • INI
    • DotEnv
    • ENV
  • Support Object Storage
    • S3
    • ALI OSS
    • Tencent COS
  • Support Database
    • Redis
    • MongoDB
    • Etcd
  • Support Configuration Center
    • Apollo
    • Nacos

Installation

To install the package, run:

go get github.com/go-zoox/config

Getting Started

type Config struct {
  Version string `config:"version"`
  Server  struct {
    Ports   []int64 `config:"ports"`
    Cleanup string  `config:"cleanup"`
  } `config:"server"`
  Logger struct {
    Level string `config:"level"`
    Trace bool   `config:"trace"`
  } `config:"logger"`
  Rules []struct {
    Host    string `config:"host"`
    Backend struct {
      ServiceName string `config:"service_name"`
      ServicePort int64  `config:"service_port"`
    } `config:"backend"`
  } `config:"rules"`
}

func main() {
  var cfg Config
  if err := Load(&cfg); err != nil {
    panic(err)
  }
}

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

View Source
const DefaultFileType = "YAML"

DefaultFileType is the default file type.

Variables

View Source
var Version = "1.4.0"

Version is the version of this package.

Functions

func Async added in v1.0.10

func Async(config interface{}, fn func() (string, error)) error

Async loads the config from the given service.

func IsNotFoundErr added in v1.2.9

func IsNotFoundErr(err error) bool

IsNotFoundErr returns true if the error is a not found error.

func Load

func Load(config any, options ...*LoadOptions) error

Load loads the config from the given file path. If the file path is empty, it will load the config from the default file path. Default file path is

 custom file path
	> ${PWD}/.{APP_NAME}.{NAME}.yml
	/etc/{APP_NAME}/{NAME}.yml (user is root) | $HOME/.config/{APP_NAME}/{NAME}.yml.
	> ${PWD}/.{APP_NAME}.yml
	> /etc/{APP_NAME}/config.yml (user is root) | $HOME/.config/{APP_NAME}/config.yml.
	> ${PWD}/.config.yml

func LoadFromService added in v1.0.10

func LoadFromService(config interface{}, fn func() (string, error)) error

LoadFromService loads the config from the given service.

func Parse added in v1.0.10

func Parse(data []byte, config interface{}, opts ...*ParseOptions) error

Parse parses the config from the given raw config.

Types

type LoadOptions

type LoadOptions struct {
	// FilePath is the config file path.
	//  If the file path is empty, it will load the config from the default file path.
	//  Default file path is ${PWD}/.config.yml.
	//  If the file path is a URL, it will fetch the config file from the URL.
	//	The URL must be started with http:// or https://.
	FilePath string

	// options: YAML | JSON | TOML | INI | HOST, default: YAML
	Type string

	// Unique AppName for the config file, default: ""
	AppName string

	// Config name, default: config.yml, and type is YAML
	Name string
}

LoadOptions is the options for Load

type ParseOptions added in v1.2.11

type ParseOptions struct {
	// The type of the config file, default is "YAML".
	// options: YAML | JSON | TOML | INI | HOST
	Type string
}

ParseOptions is the options for Parse.

Jump to

Keyboard shortcuts

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