1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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,
)
}
|