Files
crypto-price-bot/internal/utils/envx/envx.go
2024-10-24 09:53:23 +07:00

48 lines
923 B
Go

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
}