format hook

This commit is contained in:
Roy Olav Purser 2021-03-26 17:17:52 +01:00
parent 002ab7d205
commit 46207396ef
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
5 changed files with 258 additions and 245 deletions

3
.hooks/pre-commit Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
go fmt *.go
git add *.go

View File

@ -1,7 +1,11 @@
package envconf
import ("time")
import (
"time"
)
type DataType int
const (
TypeNone DataType = iota
TypeInt DataType = iota
@ -21,14 +25,14 @@ type cValue struct {
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]
if ok {
return info.parser(key, str)
}
return
}
func (dtype DataType) String()(string) {
func (dtype DataType) String() string {
info, ok := tInfo[dtype]
if ok {
return info.name

View File

@ -1,11 +1,13 @@
package envconf
import ("strings"
"unicode"
import (
"errors"
"time"
"fmt"
"os")
"os"
"strings"
"time"
"unicode"
)
type cEntry struct {
value string
@ -22,11 +24,11 @@ type Config struct {
// 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
// will not be taken into account.
func NewConfig()(*Config) {
func NewConfig() *Config {
config := new(Config)
config.parsed = false
config.env = make(map[string]cEntry)
for _,v := range os.Environ() {
for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
@ -89,7 +91,7 @@ func (c *Config) Parse() {
return
}
c.parsed = true
for k,v := range c.env {
for k, v := range c.env {
if v.empty {
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))
@ -106,10 +108,10 @@ func (c *Config) Parse() {
// Variables that have been defined without a default value and are
// missing from the environment will be considered a failure.
// 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
if ok {
for _,v := range c.env {
for _, v := range c.env {
err := v.parsed.err
if err != nil {
ok = false
@ -120,7 +122,7 @@ func (c *Config) Status()(ok bool) {
return
}
func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
val.dtype = TypeNone
if c.parsed {
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.
// 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)
return val.intval
}
// GetMetric returns the value of an environment variable.
// 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)
return val.intval
}
@ -149,7 +151,7 @@ func (c *Config) GetMetric(key string)(int64) {
// GetDirectory returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDirectory the
// 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)
return val.strval
}
@ -157,7 +159,7 @@ func (c *Config) GetDirectory(key string)(string) {
// GetString returns the value of an environment variable.
// If the variable is not defined as envconf.TypeString the
// 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)
return val.strval
}
@ -165,7 +167,7 @@ func (c *Config) GetString(key string)(string) {
// GetDuration returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDuration the
// 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)
return val.durval
}
@ -173,13 +175,13 @@ func (c *Config) GetDuration(key string)(time.Duration) {
// GetBool returns the value of an environment variable.
// If the variable is not defined as envconf.TypeBool the
// function will return false.
func (c *Config) GetBool(key string)(bool) {
func (c *Config) GetBool(key string) bool {
val := c.getRaw(key, TypeBool)
return val.boolval
}
func getFirstRune(str string)(rune) {
for _,v := range str {
func getFirstRune(str string) rune {
for _, v := range str {
return v
}
return rune(0)

View File

@ -1,13 +1,16 @@
package envconf
import ("strconv"
"strings"
"fmt"
import (
"errors"
"fmt"
"os"
"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)
if err == nil {
ret.intval = val
@ -17,7 +20,7 @@ func parseInt(key string, str string)(ret cValue) {
return
}
func parseMetric(key string, str string)(ret cValue) {
func parseMetric(key string, str string) (ret cValue) {
mod := int64(1)
str = strings.ToUpper(str)
if strings.HasSuffix(str, "K") {
@ -42,7 +45,7 @@ func parseMetric(key string, str string)(ret cValue) {
return
}
func parseDuration(key string, str string)(ret cValue) {
func parseDuration(key string, str string) (ret cValue) {
val, err := time.ParseDuration(str)
if err == nil {
ret.durval = val
@ -52,7 +55,7 @@ func parseDuration(key string, str string)(ret cValue) {
return
}
func parseBool(key string, str string)(ret cValue) {
func parseBool(key string, str string) (ret cValue) {
val, err := strconv.ParseBool(str)
if err == nil {
ret.boolval = val
@ -62,7 +65,7 @@ func parseBool(key string, str string)(ret cValue) {
return
}
func parseDirectory(_ string, str string)(ret cValue) {
func parseDirectory(_ string, str string) (ret cValue) {
wd, err := os.Getwd()
if err == nil {
if path.IsAbs(str) {
@ -76,9 +79,7 @@ func parseDirectory(_ string, str string)(ret cValue) {
return
}
func parseString(_ string, str string)(ret cValue) {
func parseString(_ string, str string) (ret cValue) {
ret.strval = str
return
}

View File

@ -1,9 +1,12 @@
package envconf
type dataTypeInfo struct {
parser func(string,string)(cValue)
parser func(string, string) cValue
name string
}
var tInfo map[DataType]dataTypeInfo
func init() {
tInfo = make(map[DataType]dataTypeInfo)
var intInfo dataTypeInfo