summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-09-03 02:36:24 +0200
committerDJ O'Leary <dijitol@proton.me>2026-09-03 02:36:24 +0200
commita1fcf7cfde975d04bcc85c53a46219e0a4bca773 (patch)
treeb17a6a19789612107288f117a811f781cf57233a /src
parent1fe1e04c3b413109bd3d7fddea975513c455b098 (diff)
feat(config): create config package for parsing environment
Diffstat (limited to 'src')
-rw-r--r--src/config/config.go102
1 files changed, 102 insertions, 0 deletions
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,
+ )
+}