Help us shape the future of Go! Take the 2026 Go Developer Survey

config

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2017 License: MIT Imports: 9 Imported by: 44

README

go-config

Documentation

Overview

Yaml Config Loader

Index

Examples

Constants

This section is empty.

Variables

View Source
var Marshal = yaml.Marshal

Marshal serializes the value provided into a YAML document.

Functions

func Load

func Load(conf interface{}, configPaths ...string) error

Load loads YAML files from `configPaths`. and assigns decoded values into the `conf` value.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/kayac/go-config"
)

var dir string

func main() {
	type DBConfig struct {
		Master  string        `yaml:"master"`
		Slave   string        `yaml:"slave"`
		Timeout time.Duration `yml:"timeout"`
	}
	type Conf struct {
		Domain  string        `yaml:"domain"`
		IsDev   bool          `yaml:"is_dev"`
		Timeout time.Duration `yml:"timeout"`
		DB      DBConfig      `yaml:"db"`
	}

	baseConfig := `
# config.yml
domain: example.com
db:
  master:  rw@/example
  slave:   ro@/example
  timeout: 0.5s
`
	localConfig := `
# config_local.yml
domain: dev.example.com
is_dev: true
`
	conf := &Conf{}
	baseConfigYaml, _ := genConfigFile("config.yml", baseConfig)         // /path/to/config.yml
	localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml

	err := config.Load(conf, baseConfigYaml, localConfigYaml)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", conf)
}

func genConfigFile(name string, config string) (string, error) {
	path := filepath.Join(dir, name)
	io, err := os.Create(path)
	if err != nil {
		return "", err
	}
	if _, err := io.WriteString(config); err != nil {
		return "", err
	}
	return path, nil
}
Output:
&{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example Slave:ro@/example Timeout:500ms}}

func LoadJSON

func LoadJSON(conf interface{}, configPaths ...string) error

Load loads JSON files from `configPaths`. and assigns decoded values into the `conf` value.

func LoadTOML

func LoadTOML(conf interface{}, configPaths ...string) error

Load loads TOML files from `configPaths`. and assigns decoded values into the `conf` value.

func LoadWithEnv

func LoadWithEnv(conf interface{}, configPaths ...string) error

LoadWithEnv loads YAML files with Env replace {{ env "ENV" }} to os.Getenv("ENV") if you set default value then {{ env "ENV" "default" }}

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/kayac/go-config"
)

var dir string

func main() {
	type DBConfig struct {
		Master  string        `yaml:"master"`
		Slave   string        `yaml:"slave"`
		Timeout time.Duration `yml:"timeout"`
	}
	type Conf struct {
		Domain  string        `yaml:"domain"`
		IsDev   bool          `yaml:"is_dev"`
		Timeout time.Duration `yml:"timeout"`
		DB      DBConfig      `yaml:"db"`
	}

	baseConfig := `
# config.yml
domain: {{ env "DOMAIN"}}
db:
  master:  rw@/example
  slave:   ro@/example
  timeout: 0.5s
`
	localConfig := `
# config_local.yml
is_dev: true
db:
  master:  {{ env "RW_USER" "rw" }}@/{{ env "DB_NAME" "example_dev" }}
  slave:   {{ env "RO_USER" "ro" }}@/{{ env "DB_NAME" "example_dev" }}
`
	os.Setenv("DOMAIN", "dev.example.com")
	os.Setenv("DB_NAME", "example_local")

	conf := &Conf{}
	baseConfigYaml, _ := genConfigFile("config.yml", baseConfig)         // /path/to/config.yml
	localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml

	err := config.LoadWithEnv(conf, baseConfigYaml, localConfigYaml)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", conf)
}

func genConfigFile(name string, config string) (string, error) {
	path := filepath.Join(dir, name)
	io, err := os.Create(path)
	if err != nil {
		return "", err
	}
	if _, err := io.WriteString(config); err != nil {
		return "", err
	}
	return path, nil
}
Output:
&{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example_local Slave:ro@/example_local Timeout:500ms}}

func LoadWithEnvJSON

func LoadWithEnvJSON(conf interface{}, configPaths ...string) error

LoadWithEnvJSON loads JSON files with Env

func LoadWithEnvTOML

func LoadWithEnvTOML(conf interface{}, configPaths ...string) error

LoadWithEnvTOML loads TOML files with Env

func MarshalJSON

func MarshalJSON(v interface{}) ([]byte, error)

MarshalJSON returns the JSON encoding of v with indent by 2 white spaces.

Types

This section is empty.

Directories

Path Synopsis
cmd
tfstate module

Jump to

Keyboard shortcuts

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