init code
This commit is contained in:
47
internal/utils/envx/envx.go
Normal file
47
internal/utils/envx/envx.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user