config/config.go

134 lines
2.7 KiB
Go
Raw Normal View History

2017-08-30 09:24:22 +00:00
package config
import (
"bytes"
"io"
"os"
"path/filepath"
)
const (
ConfigEnvPrefix = "CONFIG_ENV_PREFIX"
ConfigTagEnv = "env"
)
2017-08-30 09:24:22 +00:00
type Configurator interface {
SetConfigPath(in string) error
Load(target interface{}, files ...string) error
LoadReader(target interface{}, ext string, in io.Reader) error
2017-08-30 10:41:51 +00:00
SetEnvPrefix(in string)
2017-08-30 09:24:22 +00:00
}
type config struct {
configPath string
2017-08-30 09:24:22 +00:00
// Name of file to look for inside the path
envPrefix string
2017-08-30 09:24:22 +00:00
}
func New() Configurator {
return &config{}
2017-08-30 09:24:22 +00:00
}
var _c *config
2017-08-30 09:24:22 +00:00
2017-09-19 08:15:06 +00:00
func init() {
2017-09-19 08:15:39 +00:00
_c = New().(*config)
2017-09-19 08:15:06 +00:00
}
// SetConfigPath set a path to search for the config file in.
func SetConfigPath(in string) error { return _c.SetConfigPath(in) }
func (c *config) SetConfigPath(in string) error {
2017-08-30 09:24:22 +00:00
if in != "" {
2017-09-21 01:24:07 +00:00
absin, err := ABSPathify(in)
2017-08-30 09:24:22 +00:00
if nil != err {
return err
}
c.configPath = absin
2017-08-30 09:24:22 +00:00
}
return nil
}
// SetEnvPrefix set a prefix to search for the env variable.
func SetEnvPrefix(in string) { _c.SetEnvPrefix(in) }
func (c *config) SetEnvPrefix(in string) {
if in != "" {
c.envPrefix = in
2017-08-30 09:24:22 +00:00
}
}
// Load will unmarshal from configuration file from disk
func Load(target interface{}, files ...string) error {
return _c.Load(target, files...)
2017-08-30 09:24:22 +00:00
}
func (c *config) Load(target interface{}, files ...string) error {
filenames := c.getConfigFiles(files...)
2017-08-30 09:24:22 +00:00
for _, file := range filenames {
if err := unmarshalFile(target, file); err != nil {
return err
2017-08-30 09:24:22 +00:00
}
}
return unmarshalTags(target, c.getENVPrefix())
2017-08-30 11:28:19 +00:00
}
// LoadReader will unmarshal from configuration bytes
func LoadReader(target interface{}, ext string, in io.Reader) error {
return _c.LoadReader(target, ext, in)
2017-08-30 09:24:22 +00:00
}
func (c *config) LoadReader(target interface{}, ext string, in io.Reader) error {
buf := new(bytes.Buffer)
buf.ReadFrom(in)
2017-08-30 09:24:22 +00:00
return unmarshalData(target, ext, buf.Bytes())
2017-08-30 09:24:22 +00:00
}
// Save store to configuration file from disk
func Save(target interface{}, file string, overWrite bool) error {
2017-09-21 01:22:15 +00:00
var absPath string
var err error
2017-09-21 01:24:07 +00:00
if absPath, err = ABSPathify(file); nil != err {
2017-09-21 01:22:15 +00:00
return err
}
return marshalFile(target, absPath, overWrite)
2017-08-30 09:24:22 +00:00
}
// 1. file
// 2. configPath/file
func (c *config) getConfigFiles(files ...string) []string {
var results []string
2017-08-30 09:24:22 +00:00
for _, file := range files {
// check configuration
2017-09-21 01:24:07 +00:00
if absin, err := ABSPathify(file); nil == err {
if Exists(absin) {
2017-09-21 01:22:15 +00:00
results = append(results, absin)
}
2017-08-30 09:24:22 +00:00
}
}
for _, file := range files {
// check configuration
2017-09-21 01:24:07 +00:00
if absin, err := ABSPathify(filepath.Join(c.configPath, file)); nil == err {
if Exists(absin) {
2017-09-19 05:59:22 +00:00
results = append(results, absin)
}
2017-08-30 09:24:22 +00:00
}
}
return results
2017-08-30 09:24:22 +00:00
}
func (c *config) getENVPrefix() string {
if c.envPrefix == "" {
if prefix := os.Getenv(ConfigEnvPrefix); prefix != "" {
return prefix
2017-08-30 09:24:22 +00:00
}
return ""
}
return c.envPrefix
2017-08-30 09:24:22 +00:00
}