init code

This commit is contained in:
thuanle
2024-10-24 09:53:23 +07:00
parent a7559b3f9d
commit 92a63c7885
43 changed files with 1115 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package envx
import (
"encoding/json"
"errors"
"github.com/rs/zerolog/log"
"me.thuanle/bbot/internal/configs"
"os"
)
func GetEnvJsonArray[T any](key string) ([]T, error) {
values, found := os.LookupEnv(key)
if !found {
return nil, errors.New("env not found. Key: " + key)
}
var des []T
err := json.Unmarshal([]byte(values), &des)
if configs.LogEnv {
if err != nil {
log.Error().Err(err).
Str("key", key).
Str("value", values).
Msg("Failed to load env")
} else {
log.Info().
Str("key", key).
Interface("value", des).
Msg("Loaded env")
}
}
return des, err
}
func GetEnvBool(key string) (bool, error) {
value, found := os.LookupEnv(key)
if !found {
return false, errors.New("env not found. Key: " + key)
}
val := value == "true" || value == "1"
if configs.LogEnv {
log.Info().
Str("key", key).
Bool("value", val).
Msg("Loaded env")
}
return val, nil
}