envconf/parsers.go

101 lines
2.3 KiB
Go
Raw Normal View History

2021-03-24 10:51:09 +00:00
package envconf
2021-03-26 16:17:52 +00:00
import (
2022-01-16 17:24:24 +00:00
"encoding/hex"
2021-03-26 16:17:52 +00:00
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"time"
)
2021-03-24 10:51:09 +00:00
2022-01-29 12:03:17 +00:00
func parseInt(key string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
val, err := strconv.ParseInt(str, 10, 64)
if err == nil {
ret.intval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type int.`, key))
}
return
2021-03-25 11:06:08 +00:00
}
2022-01-29 12:03:17 +00:00
func parseMetric(key string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
mod := int64(1)
str = strings.ToUpper(str)
if strings.HasSuffix(str, "K") {
mod = 1024
str = strings.TrimSuffix(str, "K")
} else if strings.HasSuffix(str, "M") {
mod = 1024 * 1024
str = strings.TrimSuffix(str, "M")
} else if strings.HasSuffix(str, "G") {
mod = 1024 * 1024 * 1024
str = strings.TrimSuffix(str, "G")
} else if strings.HasSuffix(str, "T") {
mod = 1024 * 1024 * 1024 * 1024
str = strings.TrimSuffix(str, "T")
}
val, err := strconv.ParseInt(str, 10, 64)
if err == nil {
ret.intval = mod * val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type int.`, key))
}
return
2021-03-24 10:51:09 +00:00
}
2022-01-29 12:03:17 +00:00
func parseDuration(key string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
val, err := time.ParseDuration(str)
if err == nil {
ret.durval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type duration.`, key))
}
return
2021-03-24 10:51:09 +00:00
}
2022-01-29 12:03:17 +00:00
func parseBool(key string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
val, err := strconv.ParseBool(str)
if err == nil {
ret.boolval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type bool.`, key))
}
return
2021-03-24 10:51:09 +00:00
}
2021-03-24 14:15:37 +00:00
2022-01-29 12:03:17 +00:00
func parseHex(key string, str string, size int) (ret cValue) {
2022-01-16 17:24:24 +00:00
val, err := hex.DecodeString(str)
2022-01-29 12:03:17 +00:00
if err == nil && (size == 0 || size == len(val)) {
2022-01-16 20:48:29 +00:00
ret.binval = val
} else {
2022-01-28 18:38:27 +00:00
if size == 0 {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex%d.`, key, size))
}
2022-01-16 20:48:29 +00:00
}
return
}
2022-01-29 12:03:17 +00:00
func parseDirectory(_ string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
wd, err := os.Getwd()
if err == nil {
if path.IsAbs(str) {
ret.strval = path.Clean(str)
} else {
ret.strval = path.Join(wd, str)
}
} else {
ret.strval = path.Clean(str)
}
return
2021-03-24 14:15:37 +00:00
}
2021-03-25 08:55:14 +00:00
2022-01-29 12:03:17 +00:00
func parseString(_ string, str string, _ int) (ret cValue) {
2021-03-26 16:17:52 +00:00
ret.strval = str
return
}