diff --git a/go.mod b/go.mod index 43273647..5662ed51 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/anaskhan96/soup v1.2.5 github.com/bwmarrin/discordgo v0.27.1 github.com/darklab8/go-typelog v0.3.3 - github.com/darklab8/go-utils v0.15.0 + github.com/darklab8/go-utils v0.16.0 github.com/pkg/profile v1.7.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 525d9b09..9eb36629 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/darklab8/go-typelog v0.3.3 h1:ZBj26swTbqe0LtutxG2abnX5JJHz1QWOIvxM8GI github.com/darklab8/go-typelog v0.3.3/go.mod h1:AwwOf3dkp/tpevHFNbkB+PbwlDrUUgO1CVFkEnj+q5w= github.com/darklab8/go-utils v0.15.0 h1:Xsgl7chxJoUYgWwu5NnVo8jSqggKoxazT3I8T7407tM= github.com/darklab8/go-utils v0.15.0/go.mod h1:zApy0zIFiGCw496OIot7dl8i2cAsQMyPyf3PCMMpS3g= +github.com/darklab8/go-utils v0.16.0 h1:xgzYrea74ps2EOWZQghnP0ah9TvD+2/iSsFg3jnUGSo= +github.com/darklab8/go-utils v0.16.0/go.mod h1:zApy0zIFiGCw496OIot7dl8i2cAsQMyPyf3PCMMpS3g= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/vendor/github.com/darklab8/go-utils/utils/enverant/env.go b/vendor/github.com/darklab8/go-utils/utils/enverant/env.go new file mode 100644 index 00000000..3c173001 --- /dev/null +++ b/vendor/github.com/darklab8/go-utils/utils/enverant/env.go @@ -0,0 +1,116 @@ +package enverant + +/* +Manager for getting values from Environment variables +*/ + +import ( + "fmt" + "os" + "strconv" + "strings" + + "github.com/darklab8/go-typelog/typelog" + "github.com/darklab8/go-utils/utils/utils_logus" +) + +type Enverant struct { + file_envs map[string]interface{} +} + +type EnverantOption func(m *Enverant) + +func NewEnverant(opts ...EnverantOption) *Enverant { + e := &Enverant{ + file_envs: map[string]interface{}{}, + } + for _, opt := range opts { + opt(e) + } + return e +} + +func WithEnvFile(path string) EnverantOption { + return func(m *Enverant) { + m.file_envs = ReadJson(path) + } +} + +func EnrichStr(value string) string { + // unhardcode later + if strings.Contains(value, "${env:HOME}") { + value = strings.ReplaceAll(value, "${env:HOME}", os.Getenv("HOME")) + } + return value +} + +func (e *Enverant) GetStr(key string) string { + return e.GetStrOr(key, "") +} + +func (e *Enverant) GetStrOr(key string, default_ string) string { + + if value, ok := os.LookupEnv(key); ok { + return value + } + + if value, ok := e.file_envs[key]; ok { + return EnrichStr(value.(string)) + } + + return default_ +} + +func (e *Enverant) GetBool(key string) bool { + return e.GetBoolOr(key, false) +} + +func (e *Enverant) GetBoolOr(key string, default_ bool) bool { + + if value, ok := os.LookupEnv(key); ok { + return value == "true" + } + + if value, ok := e.file_envs[key]; ok { + switch v := value.(type) { + case bool: + return v + case string: + return v == "true" + default: + panic(fmt.Sprintln("unrecognized type for value", v, " in GetBoolOr")) + } + } + + return default_ +} + +func (e *Enverant) GetInt(key string) int { + return e.GetIntOr(key, 0) +} + +func (e *Enverant) GetIntOr(key string, default_ int) int { + + if value, ok := os.LookupEnv(key); ok { + int_value, err := strconv.Atoi(value) + utils_logus.Log.CheckPanic(err, "expected to be int", typelog.String("key", key)) + return int_value + } + + if value, ok := e.file_envs[key]; ok { + switch v := value.(type) { + case int: + return v + case float64: + return int(v) + case string: + int_value, err := strconv.Atoi(v) + utils_logus.Log.CheckPanic(err, "expected to be int", typelog.String("key", key)) + return int_value + default: + panic(fmt.Sprintln("unrecognized type for value", v, " in GetIntOr")) + } + } + + return default_ +} diff --git a/vendor/github.com/darklab8/go-utils/utils/enverant/json.go b/vendor/github.com/darklab8/go-utils/utils/enverant/json.go new file mode 100644 index 00000000..c507446b --- /dev/null +++ b/vendor/github.com/darklab8/go-utils/utils/enverant/json.go @@ -0,0 +1,45 @@ +package enverant + +import ( + "bufio" + "bytes" + "encoding/json" + "os" + "regexp" + + "github.com/darklab8/go-typelog/typelog" + "github.com/darklab8/go-utils/utils/regexy" + "github.com/darklab8/go-utils/utils/utils_logus" +) + +var regexConfiglines *regexp.Regexp + +func init() { + regexy.InitRegexExpression(®exConfiglines, `^(.*)(?:// .*)$`) +} + +func ReadJson(path string) map[string]interface{} { + env_map := make(map[string]interface{}) + + file, err := os.Open(path) + if utils_logus.Log.CheckWarn(err, "not found env file at path", typelog.String("path", path)) { + return env_map + } + defer file.Close() + + scanner := bufio.NewScanner(file) + var cleaned_json bytes.Buffer + + for scanner.Scan() { + input_line := scanner.Text() + match := regexConfiglines.FindStringSubmatch(input_line) + if len(match) > 0 { + input_line = match[1] + } + cleaned_json.WriteString(input_line) + } + + err = json.Unmarshal(cleaned_json.Bytes(), &env_map) + utils_logus.Log.CheckPanic(err, "failed to parse json with env vars") + return env_map +} diff --git a/vendor/github.com/darklab8/go-utils/utils/ptr.go b/vendor/github.com/darklab8/go-utils/utils/ptr.go deleted file mode 100644 index 43c9c9b3..00000000 --- a/vendor/github.com/darklab8/go-utils/utils/ptr.go +++ /dev/null @@ -1,3 +0,0 @@ -package utils - -func Ptr[T any](v T) *T { return &v } diff --git a/vendor/github.com/darklab8/go-utils/utils/regex.go b/vendor/github.com/darklab8/go-utils/utils/regexy/regexy.go similarity index 69% rename from vendor/github.com/darklab8/go-utils/utils/regex.go rename to vendor/github.com/darklab8/go-utils/utils/regexy/regexy.go index e7d8c6d9..7f555036 100644 --- a/vendor/github.com/darklab8/go-utils/utils/regex.go +++ b/vendor/github.com/darklab8/go-utils/utils/regexy/regexy.go @@ -1,9 +1,10 @@ -package utils +package regexy import ( "regexp" "github.com/darklab8/go-utils/utils/utils_logus" + "github.com/darklab8/go-utils/utils/utils_os" "github.com/darklab8/go-utils/utils/utils_types" ) @@ -12,5 +13,5 @@ func InitRegexExpression(regex **regexp.Regexp, expression utils_types.RegExp) { *regex, err = regexp.Compile(string(expression)) utils_logus.Log.CheckPanic(err, "failed to init regex", - utils_logus.Regex(expression), utils_logus.FilePath(GetCurrentFile())) + utils_logus.Regex(expression), utils_logus.FilePath(utils_os.GetCurrentFile())) } diff --git a/vendor/github.com/darklab8/go-utils/utils/utils_env/env.go b/vendor/github.com/darklab8/go-utils/utils/utils_env/env.go deleted file mode 100644 index be2dc338..00000000 --- a/vendor/github.com/darklab8/go-utils/utils/utils_env/env.go +++ /dev/null @@ -1,47 +0,0 @@ -package utils_env - -import ( - "os" - "strconv" - - "github.com/darklab8/go-typelog/typelog" - "github.com/darklab8/go-utils/utils/utils_logus" -) - -type EnvConfig struct{} - -func NewEnvConfig() EnvConfig { - return EnvConfig{} -} - -func (e EnvConfig) GetEnvWithDefault(key string, default_ string) string { - if value, ok := os.LookupEnv(key); ok { - return value - } else { - return default_ - } -} - -func (e EnvConfig) GetEnvBool(key string) bool { - return os.Getenv(key) == "true" -} - -func (e EnvConfig) GetEnvBoolWithDefault(key string, default_ bool) bool { - if value, ok := os.LookupEnv(key); ok { - return value == "true" - } - return default_ -} - -func (e EnvConfig) GetEnv(key string) string { - return os.Getenv(key) -} - -func (e EnvConfig) GetIntWithDefault(key string, default_ int) int { - if value, ok := os.LookupEnv(key); ok { - int_value, err := strconv.Atoi(value) - utils_logus.Log.CheckPanic(err, "expected to be int", typelog.String("key", key)) - return int_value - } - return default_ -} diff --git a/vendor/github.com/darklab8/go-utils/utils/os.go b/vendor/github.com/darklab8/go-utils/utils/utils_os/get.go similarity index 98% rename from vendor/github.com/darklab8/go-utils/utils/os.go rename to vendor/github.com/darklab8/go-utils/utils/utils_os/get.go index 28448728..c1149a98 100644 --- a/vendor/github.com/darklab8/go-utils/utils/os.go +++ b/vendor/github.com/darklab8/go-utils/utils/utils_os/get.go @@ -1,4 +1,4 @@ -package utils +package utils_os import ( "os" diff --git a/vendor/github.com/darklab8/go-utils/utils/utils_os/os.go b/vendor/github.com/darklab8/go-utils/utils/utils_os/os.go new file mode 100644 index 00000000..76558920 --- /dev/null +++ b/vendor/github.com/darklab8/go-utils/utils/utils_os/os.go @@ -0,0 +1,35 @@ +package utils_os + +import ( + "os" + "path/filepath" + + "github.com/darklab8/go-utils/utils/utils_logus" + "github.com/darklab8/go-utils/utils/utils_types" +) + +func GetDirsSimply(path utils_types.FilePath) []utils_types.FilePath { + dirs := []utils_types.FilePath{} + files, err := os.ReadDir(string(path)) + + utils_logus.Log.CheckError(err, "no such directory", utils_logus.FilePath(path)) + + for _, fileInfo := range files { + if fileInfo.IsDir() { + dirs = append(dirs, utils_types.FilePath(filepath.Join(string(path), fileInfo.Name()))) + } + } + + return dirs +} + +func GetRecursiveDirs(path utils_types.FilePath) []utils_types.FilePath { + dirs := GetDirsSimply(path) + copied_dirs := []utils_types.FilePath{} + copied_dirs = append(copied_dirs, dirs...) + for _, dir := range copied_dirs { + nested_dirs := GetRecursiveDirs(dir) + dirs = append(dirs, nested_dirs...) + } + return dirs +} diff --git a/vendor/github.com/darklab8/go-utils/utils/utils_settings/settings.go b/vendor/github.com/darklab8/go-utils/utils/utils_settings/settings.go index 688199b8..8b028f70 100644 --- a/vendor/github.com/darklab8/go-utils/utils/utils_settings/settings.go +++ b/vendor/github.com/darklab8/go-utils/utils/utils_settings/settings.go @@ -1,6 +1,6 @@ package utils_settings -import "github.com/darklab8/go-utils/utils/utils_env" +import "github.com/darklab8/go-utils/utils/enverant" type UtilsEnvs struct { IsDevEnv bool @@ -10,9 +10,14 @@ type UtilsEnvs struct { var Envs UtilsEnvs func init() { - envs := utils_env.NewEnvConfig() + envs := enverant.NewEnverant() + GetEnvs(envs) +} + +func GetEnvs(envs *enverant.Enverant) UtilsEnvs { Envs = UtilsEnvs{ - IsDevEnv: envs.GetEnvBool("DEV_ENV"), - AreTestsRegenerating: envs.GetEnvBool("DARK_TEST_REGENERATE"), + IsDevEnv: envs.GetBoolOr("DEV_ENV", false), + AreTestsRegenerating: envs.GetBoolOr("DARK_TEST_REGENERATE", false), } + return Envs } diff --git a/vendor/github.com/darklab8/go-utils/utils/utils_types/types.go b/vendor/github.com/darklab8/go-utils/utils/utils_types/types.go index cd5b2be0..43f85260 100644 --- a/vendor/github.com/darklab8/go-utils/utils/utils_types/types.go +++ b/vendor/github.com/darklab8/go-utils/utils/utils_types/types.go @@ -8,6 +8,13 @@ func (f FilePath) ToString() string { return string(f) } func (f FilePath) Base() FilePath { return FilePath(filepath.Base(string(f))) } +func (f FilePath) Dir() FilePath { return FilePath(filepath.Dir(string(f))) } + +func (f FilePath) Join(paths ...string) FilePath { + paths = append([]string{string(f)}, paths...) + return FilePath(filepath.Join(paths...)) +} + type RegExp string type TemplateExpression string diff --git a/vendor/modules.txt b/vendor/modules.txt index 49152f5a..c5422a61 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -7,12 +7,14 @@ github.com/bwmarrin/discordgo # github.com/darklab8/go-typelog v0.3.3 ## explicit; go 1.21.1 github.com/darklab8/go-typelog/typelog -# github.com/darklab8/go-utils v0.15.0 +# github.com/darklab8/go-utils v0.16.0 ## explicit; go 1.21.1 github.com/darklab8/go-utils/utils +github.com/darklab8/go-utils/utils/enverant +github.com/darklab8/go-utils/utils/regexy github.com/darklab8/go-utils/utils/timeit -github.com/darklab8/go-utils/utils/utils_env github.com/darklab8/go-utils/utils/utils_logus +github.com/darklab8/go-utils/utils/utils_os github.com/darklab8/go-utils/utils/utils_settings github.com/darklab8/go-utils/utils/utils_types github.com/darklab8/go-utils/utils/worker