-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
79 lines (69 loc) · 1.74 KB
/
config.go
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
package main
import (
"fmt"
"os"
"strconv"
"github.com/t-muehlberger/rio-tachograph-downloader/client"
)
const (
defaultTokenUrl = "https://auth.iam.rio.cloud/oauth/token"
defaultTargetDir = "."
defaultFilesPerSec = 4
)
const (
envApiBaseUrl = "API_BASE_URL"
envTokenUrl = "API_TOKEN_URL"
envClientID = "API_CLIENT_ID"
envClientSecret = "API_CLIENT_SECRET"
envIntegrationID = "API_INTEGRATION_ID"
envTargetDir = "TARGET_DIR"
envFilesPerSec = "FILES_PER_SEC"
)
type config struct {
apiBaseUrl string
toeknUrl string
clientID string
clientSecret string
integrationID string
targetDir string
filesPerSec int
}
func getConfig() (config, error) {
c := config{
apiBaseUrl: os.Getenv(envApiBaseUrl),
toeknUrl: os.Getenv(envTokenUrl),
clientID: os.Getenv(envClientID),
clientSecret: os.Getenv(envClientSecret),
integrationID: os.Getenv(envIntegrationID),
targetDir: os.Getenv(envTargetDir),
}
filesPerSecStr := os.Getenv(envFilesPerSec)
if filesPerSecStr == "" {
c.filesPerSec = defaultFilesPerSec
} else {
filesPerSec, err := strconv.Atoi(filesPerSecStr)
if err != nil {
return c, err
}
c.filesPerSec = filesPerSec
}
if c.apiBaseUrl == "" {
c.apiBaseUrl = client.DefaultSchemes[0] + "://" + client.DefaultHost + client.DefaultBasePath
}
if c.toeknUrl == "" {
c.toeknUrl = defaultTokenUrl
}
if c.targetDir == "" {
c.targetDir = defaultTargetDir
}
if c.clientID == "" {
return c, fmt.Errorf("variable '%s' is not set", envClientID)
}
if c.clientSecret == "" {
return c, fmt.Errorf("variable '%s' is not set", envClientSecret)
}
if c.integrationID == "" {
return c, fmt.Errorf("variable '%s' is not set", envIntegrationID)
}
return c, nil
}