From a1fcf7cfde975d04bcc85c53a46219e0a4bca773 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Thu, 3 Sep 2026 02:36:24 +0200 Subject: feat(config): create config package for parsing environment --- src/config/config.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/config/config.go (limited to 'src/config') diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 0000000..3f3603a --- /dev/null +++ b/src/config/config.go @@ -0,0 +1,102 @@ +package config + +import ( + "errors" + "fmt" + "log/slog" + "os" + "strings" +) + +const ( + CardmarketAppToken = "CM_APP_TOKEN" + CardmarketAppSecret = "CM_APP_SECRET" + CardmarketAccessToken = "CM_ACCESS_TOKEN" + CardmarketAccessTokenSecret = "CM_ACCESS_TOKEN_SECRET" + CardmarketApiBaseURL = "CM_API_BASE_URL" + AppLogLevel = "APP_LOG_LEVEL" +) + +var ( + ErrMissingCardmarketAppToken = errors.New("err: missing " + CardmarketAppToken + " in ENV") + ErrMissingCardmarketAppSecret = errors.New("err: missing " + CardmarketAppSecret + " in ENV") + ErrMissingCardmarketAccessToken = errors.New("err: missing " + CardmarketAccessToken + " in ENV") + ErrMissingCardmarketAccessTokenSecret = errors.New("err: missing " + CardmarketAccessTokenSecret + " in ENV") + ErrMissingCardmarketApiBaseURL = errors.New("err: missing " + CardmarketApiBaseURL + " in ENV") +) + +type CardmarketApiAuth struct { + AppToken []byte + AppSecret []byte + AccessToken []byte + AccessTokenSecret []byte +} + +func (auth *CardmarketApiAuth) String() string { + return fmt.Sprintf( + "CardmarketApiAuth{AppToken: %s, AppSecret: %s, AccessToken: %s, AccessTokenSecret: %s}", + string(auth.AppToken[:3])+strings.Repeat("*", 5), + string(auth.AppSecret[:3])+strings.Repeat("*", 5), + string(auth.AccessToken[:3])+strings.Repeat("*", 5), + string(auth.AccessTokenSecret[:3])+strings.Repeat("*", 5), + ) +} + +type Config struct { + CardmarketApiBaseURL []byte + CardmarketApiAuth CardmarketApiAuth + AppLogLevel slog.Level +} + +func NewConfigFromEnv() (Config, error) { + appToken := os.Getenv(CardmarketAppToken) + if appToken == "" { + return Config{}, ErrMissingCardmarketAppToken + } + + appSecret := os.Getenv(CardmarketAppSecret) + if appSecret == "" { + return Config{}, ErrMissingCardmarketAppSecret + } + + accessToken := os.Getenv(CardmarketAccessToken) + if accessToken == "" { + return Config{}, ErrMissingCardmarketAccessToken + } + + accessTokenSecret := os.Getenv(CardmarketAccessTokenSecret) + if accessTokenSecret == "" { + return Config{}, ErrMissingCardmarketAccessTokenSecret + } + + apiBaseURL := os.Getenv(CardmarketApiBaseURL) + if apiBaseURL == "" { + return Config{}, ErrMissingCardmarketApiBaseURL + } + + lvl := slog.LevelDebug + appLogLevel := os.Getenv(AppLogLevel) + if lvlErr := lvl.UnmarshalText([]byte(appLogLevel)); lvlErr != nil { + return Config{}, errors.Join(errors.New("could not unmarshal "+AppLogLevel), lvlErr) + } + + return Config{ + CardmarketApiBaseURL: []byte(apiBaseURL), + CardmarketApiAuth: CardmarketApiAuth{ + AppToken: []byte(appToken), + AppSecret: []byte(appSecret), + AccessToken: []byte(accessToken), + AccessTokenSecret: []byte(accessTokenSecret), + }, + AppLogLevel: lvl, + }, nil +} + +func (conf *Config) String() string { + return fmt.Sprintf( + "Config{CardmarketApiBaseURL: %s, CardmarketApiAuth: %s, AppLogLevel: %s}", + conf.CardmarketApiBaseURL, + conf.CardmarketApiAuth.String(), + conf.AppLogLevel, + ) +} -- cgit v1.2.3