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
|
@ -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
|
||||||
@ -28,7 +32,7 @@ func (dtype DataType) parse(key string, str string)(ret cValue) {
|
|||||||
}
|
}
|
||||||
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
|
||||||
|
26
envconf.go
26
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,7 +24,7 @@ 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)
|
||||||
@ -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,12 +175,12 @@ 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
|
||||||
}
|
}
|
||||||
|
13
parsers.go
13
parsers.go
@ -1,11 +1,14 @@
|
|||||||
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)
|
||||||
@ -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