format hook
This commit is contained in:
parent
002ab7d205
commit
46207396ef
3
.hooks/pre-commit
Executable file
3
.hooks/pre-commit
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
go fmt *.go
|
||||||
|
git add *.go
|
10
datatype.go
10
datatype.go
@ -1,7 +1,11 @@
|
|||||||
package envconf
|
package envconf
|
||||||
import ("time")
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type DataType int
|
type DataType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TypeNone DataType = iota
|
TypeNone DataType = iota
|
||||||
TypeInt DataType = iota
|
TypeInt DataType = iota
|
||||||
@ -21,14 +25,14 @@ type cValue struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dtype DataType) parse(key string, str string)(ret cValue) {
|
func (dtype DataType) parse(key string, str string) (ret cValue) {
|
||||||
info, ok := tInfo[dtype]
|
info, ok := tInfo[dtype]
|
||||||
if ok {
|
if ok {
|
||||||
return info.parser(key, str)
|
return info.parser(key, str)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (dtype DataType) String()(string) {
|
func (dtype DataType) String() string {
|
||||||
info, ok := tInfo[dtype]
|
info, ok := tInfo[dtype]
|
||||||
if ok {
|
if ok {
|
||||||
return info.name
|
return info.name
|
||||||
|
38
envconf.go
38
envconf.go
@ -1,11 +1,13 @@
|
|||||||
package envconf
|
package envconf
|
||||||
|
|
||||||
import ("strings"
|
import (
|
||||||
"unicode"
|
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os")
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
type cEntry struct {
|
type cEntry struct {
|
||||||
value string
|
value string
|
||||||
@ -22,11 +24,11 @@ type Config struct {
|
|||||||
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
|
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
|
||||||
// The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called
|
// The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called
|
||||||
// will not be taken into account.
|
// will not be taken into account.
|
||||||
func NewConfig()(*Config) {
|
func NewConfig() *Config {
|
||||||
config := new(Config)
|
config := new(Config)
|
||||||
config.parsed = false
|
config.parsed = false
|
||||||
config.env = make(map[string]cEntry)
|
config.env = make(map[string]cEntry)
|
||||||
for _,v := range os.Environ() {
|
for _, v := range os.Environ() {
|
||||||
splitted := strings.SplitN(v, "=", 2)
|
splitted := strings.SplitN(v, "=", 2)
|
||||||
if len(splitted) == 2 {
|
if len(splitted) == 2 {
|
||||||
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
|
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
|
||||||
@ -89,7 +91,7 @@ func (c *Config) Parse() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.parsed = true
|
c.parsed = true
|
||||||
for k,v := range c.env {
|
for k, v := range c.env {
|
||||||
if v.empty {
|
if v.empty {
|
||||||
if v.unset {
|
if v.unset {
|
||||||
v.parsed.err = errors.New(fmt.Sprintf(`Environment variable "%s" not found. It should have been of type %s.`, k, v.dtype))
|
v.parsed.err = errors.New(fmt.Sprintf(`Environment variable "%s" not found. It should have been of type %s.`, k, v.dtype))
|
||||||
@ -106,10 +108,10 @@ func (c *Config) Parse() {
|
|||||||
// Variables that have been defined without a default value and are
|
// Variables that have been defined without a default value and are
|
||||||
// missing from the environment will be considered a failure.
|
// missing from the environment will be considered a failure.
|
||||||
// If parsing of any of the variables has failed Status will return false.
|
// If parsing of any of the variables has failed Status will return false.
|
||||||
func (c *Config) Status()(ok bool) {
|
func (c *Config) Status() (ok bool) {
|
||||||
ok = c.parsed
|
ok = c.parsed
|
||||||
if ok {
|
if ok {
|
||||||
for _,v := range c.env {
|
for _, v := range c.env {
|
||||||
err := v.parsed.err
|
err := v.parsed.err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ok = false
|
ok = false
|
||||||
@ -120,7 +122,7 @@ func (c *Config) Status()(ok bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
|
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||||
val.dtype = TypeNone
|
val.dtype = TypeNone
|
||||||
if c.parsed {
|
if c.parsed {
|
||||||
upper := strings.ToUpper(key)
|
upper := strings.ToUpper(key)
|
||||||
@ -134,14 +136,14 @@ func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
|
|||||||
|
|
||||||
// GetInt returns the value of an environment variable.
|
// GetInt returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeInt the function will return 0.
|
// If the variable is not defined as envconf.TypeInt the function will return 0.
|
||||||
func (c *Config) GetInt(key string)(int64) {
|
func (c *Config) GetInt(key string) int64 {
|
||||||
val := c.getRaw(key, TypeInt)
|
val := c.getRaw(key, TypeInt)
|
||||||
return val.intval
|
return val.intval
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMetric returns the value of an environment variable.
|
// GetMetric returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeMetric the function will return 0.
|
// If the variable is not defined as envconf.TypeMetric the function will return 0.
|
||||||
func (c *Config) GetMetric(key string)(int64) {
|
func (c *Config) GetMetric(key string) int64 {
|
||||||
val := c.getRaw(key, TypeMetric)
|
val := c.getRaw(key, TypeMetric)
|
||||||
return val.intval
|
return val.intval
|
||||||
}
|
}
|
||||||
@ -149,7 +151,7 @@ func (c *Config) GetMetric(key string)(int64) {
|
|||||||
// GetDirectory returns the value of an environment variable.
|
// GetDirectory returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeDirectory the
|
// If the variable is not defined as envconf.TypeDirectory the
|
||||||
// function will return the empty string.
|
// function will return the empty string.
|
||||||
func (c *Config) GetDirectory(key string)(string) {
|
func (c *Config) GetDirectory(key string) string {
|
||||||
val := c.getRaw(key, TypeDirectory)
|
val := c.getRaw(key, TypeDirectory)
|
||||||
return val.strval
|
return val.strval
|
||||||
}
|
}
|
||||||
@ -157,7 +159,7 @@ func (c *Config) GetDirectory(key string)(string) {
|
|||||||
// GetString returns the value of an environment variable.
|
// GetString returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeString the
|
// If the variable is not defined as envconf.TypeString the
|
||||||
// function will return the empty string.
|
// function will return the empty string.
|
||||||
func (c *Config) GetString(key string)(string) {
|
func (c *Config) GetString(key string) string {
|
||||||
val := c.getRaw(key, TypeString)
|
val := c.getRaw(key, TypeString)
|
||||||
return val.strval
|
return val.strval
|
||||||
}
|
}
|
||||||
@ -165,7 +167,7 @@ func (c *Config) GetString(key string)(string) {
|
|||||||
// GetDuration returns the value of an environment variable.
|
// GetDuration returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeDuration the
|
// If the variable is not defined as envconf.TypeDuration the
|
||||||
// function will return time.Duration(0).
|
// function will return time.Duration(0).
|
||||||
func (c *Config) GetDuration(key string)(time.Duration) {
|
func (c *Config) GetDuration(key string) time.Duration {
|
||||||
val := c.getRaw(key, TypeDuration)
|
val := c.getRaw(key, TypeDuration)
|
||||||
return val.durval
|
return val.durval
|
||||||
}
|
}
|
||||||
@ -173,13 +175,13 @@ func (c *Config) GetDuration(key string)(time.Duration) {
|
|||||||
// GetBool returns the value of an environment variable.
|
// GetBool returns the value of an environment variable.
|
||||||
// If the variable is not defined as envconf.TypeBool the
|
// If the variable is not defined as envconf.TypeBool the
|
||||||
// function will return false.
|
// function will return false.
|
||||||
func (c *Config) GetBool(key string)(bool) {
|
func (c *Config) GetBool(key string) bool {
|
||||||
val := c.getRaw(key, TypeBool)
|
val := c.getRaw(key, TypeBool)
|
||||||
return val.boolval
|
return val.boolval
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFirstRune(str string)(rune) {
|
func getFirstRune(str string) rune {
|
||||||
for _,v := range str {
|
for _, v := range str {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return rune(0)
|
return rune(0)
|
||||||
|
25
parsers.go
25
parsers.go
@ -1,13 +1,16 @@
|
|||||||
package envconf
|
package envconf
|
||||||
import ("strconv"
|
|
||||||
"strings"
|
import (
|
||||||
"fmt"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time")
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func parseInt(key string, str string)(ret cValue) {
|
func parseInt(key string, str string) (ret cValue) {
|
||||||
val, err := strconv.ParseInt(str, 10, 64)
|
val, err := strconv.ParseInt(str, 10, 64)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ret.intval = val
|
ret.intval = val
|
||||||
@ -17,7 +20,7 @@ func parseInt(key string, str string)(ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMetric(key string, str string)(ret cValue) {
|
func parseMetric(key string, str string) (ret cValue) {
|
||||||
mod := int64(1)
|
mod := int64(1)
|
||||||
str = strings.ToUpper(str)
|
str = strings.ToUpper(str)
|
||||||
if strings.HasSuffix(str, "K") {
|
if strings.HasSuffix(str, "K") {
|
||||||
@ -42,7 +45,7 @@ func parseMetric(key string, str string)(ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDuration(key string, str string)(ret cValue) {
|
func parseDuration(key string, str string) (ret cValue) {
|
||||||
val, err := time.ParseDuration(str)
|
val, err := time.ParseDuration(str)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ret.durval = val
|
ret.durval = val
|
||||||
@ -52,7 +55,7 @@ func parseDuration(key string, str string)(ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseBool(key string, str string)(ret cValue) {
|
func parseBool(key string, str string) (ret cValue) {
|
||||||
val, err := strconv.ParseBool(str)
|
val, err := strconv.ParseBool(str)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ret.boolval = val
|
ret.boolval = val
|
||||||
@ -62,7 +65,7 @@ func parseBool(key string, str string)(ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDirectory(_ string, str string)(ret cValue) {
|
func parseDirectory(_ string, str string) (ret cValue) {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if path.IsAbs(str) {
|
if path.IsAbs(str) {
|
||||||
@ -76,9 +79,7 @@ func parseDirectory(_ string, str string)(ret cValue) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseString(_ string, str string) (ret cValue) {
|
||||||
func parseString(_ string, str string)(ret cValue) {
|
|
||||||
ret.strval = str
|
ret.strval = str
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package envconf
|
package envconf
|
||||||
|
|
||||||
type dataTypeInfo struct {
|
type dataTypeInfo struct {
|
||||||
parser func(string,string)(cValue)
|
parser func(string, string) cValue
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
var tInfo map[DataType]dataTypeInfo
|
var tInfo map[DataType]dataTypeInfo
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
tInfo = make(map[DataType]dataTypeInfo)
|
tInfo = make(map[DataType]dataTypeInfo)
|
||||||
var intInfo dataTypeInfo
|
var intInfo dataTypeInfo
|
||||||
|
Loading…
x
Reference in New Issue
Block a user