forked from kishikawakatsumi/deliverbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
126 lines (116 loc) · 3.9 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"github.com/kelseyhightower/envconfig"
toml "github.com/sioncojp/tomlssm"
)
type Config struct {
BotToken string
VerificationToken string
BotID string
ChannelID string
DebugChannelID string
GitHubUsername string
GitHubToken string
GitHubRepositoryOwner string
GitHubRepositoryName string
GitCommitAuthorName string
GitCommitAuthorEmail string
InfoPlistPath string
}
type envConfig struct {
BotToken string `envconfig:"BOT_TOKEN"`
VerificationToken string `envconfig:"VERIFICATION_TOKEN"`
BotID string `envconfig:"BOT_ID"`
ChannelID string `envconfig:"CHANNEL_ID"`
DebugChannelID string `envconfig:"DEBUG_CHANNEL_ID"`
GitHubUsername string `envconfig:"GITHUB_USERNAME"`
GitHubToken string `envconfig:"GITHUB_TOKEN"`
GitHubRepositoryOwner string `envconfig:"GITHUB_REPOSITORY_OWNER"`
GitHubRepositoryName string `envconfig:"GITHUB_REPOSITORY_NAME"`
GitCommitAuthorName string `envconfig:"GIT_COMMIT_AUTHOR_NAME"`
GitCommitAuthorEmail string `envconfig:"GIT_COMMIT_AUTHOR_EMAIL"`
InfoPlistPath string `envconfig:"INFOPLIST_PATH"`
}
type tomlConfig struct {
BotToken string `toml:"bot_token"`
VerificationToken string `toml:"verification_token"`
BotID string `toml:"bot_id"`
ChannelID string `toml:"channel_id"`
DebugChannelID string `toml:"debug_channel_id"`
GitHubUsername string `toml:"github_username"`
GitHubToken string `toml:"github_token"`
GitHubRepositoryOwner string `toml:"github_repository_owner"`
GitHubRepositoryName string `toml:"github_repository_name"`
GitCommitAuthorName string `toml:"github_commit_author_name"`
GitCommitAuthorEmail string `toml:"github_commit_author_email"`
InfoPlistPath string `toml:"infoplist_path"`
}
func LoadConfig(path, region string) (*Config, error) {
var config Config
var env envConfig
if err := envconfig.Process("", &env); err != nil {
sugar.Errorf("Failed to process env var: %s", err)
return nil, err
}
tc, err := loadToml(path, region)
if err != nil {
sugar.Errorf("Failed to load 'config.toml': %s", err)
return nil, err
}
config.BotToken = tc.BotToken
if env.BotToken != "" {
config.BotToken = env.BotToken
}
config.VerificationToken = tc.VerificationToken
if env.VerificationToken != "" {
config.VerificationToken = env.VerificationToken
}
config.BotID = tc.BotID
if env.BotID != "" {
config.BotID = env.BotID
}
config.ChannelID = tc.ChannelID
if env.ChannelID != "" {
config.ChannelID = env.ChannelID
}
config.DebugChannelID = tc.DebugChannelID
if env.DebugChannelID != "" {
config.DebugChannelID = env.DebugChannelID
}
config.GitHubUsername = tc.GitHubUsername
if env.GitHubUsername != "" {
config.GitHubUsername = env.GitHubUsername
}
config.GitHubToken = tc.GitHubToken
if env.GitHubToken != "" {
config.GitHubToken = env.GitHubToken
}
config.GitHubRepositoryOwner = tc.GitHubRepositoryOwner
if env.GitHubRepositoryOwner != "" {
config.GitHubRepositoryOwner = env.GitHubRepositoryOwner
}
config.GitHubRepositoryName = tc.GitHubRepositoryName
if env.GitHubRepositoryName != "" {
config.GitHubRepositoryName = env.GitHubRepositoryName
}
config.GitCommitAuthorName = tc.GitCommitAuthorName
if env.GitCommitAuthorName != "" {
config.GitCommitAuthorName = env.GitCommitAuthorName
}
config.GitCommitAuthorEmail = tc.GitCommitAuthorEmail
if env.GitCommitAuthorEmail != "" {
config.GitCommitAuthorEmail = env.GitCommitAuthorEmail
}
config.InfoPlistPath = tc.InfoPlistPath
if env.InfoPlistPath != "" {
config.InfoPlistPath = env.InfoPlistPath
}
return &config, nil
}
func loadToml(path, region string) (*tomlConfig, error) {
var config tomlConfig
if _, err := toml.DecodeFile(path, &config, region); err != nil {
return nil, err
}
return &config, nil
}