forked from tmtk75/kii-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
282 lines (250 loc) · 7 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package kiicli
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/mitchellh/go-homedir"
"github.com/tmtk75/cli"
"github.com/vaughan0/go-ini"
)
type GlobalConfig struct {
AppId string
AppKey string
ClientId string
ClientSecret string
Token string
Site string
endpointUrl string
devlogUrl string
Curl bool
SuppressExit bool
UTC bool
}
const (
ExitGeneralReason = 1
ExitIllegalNumberOfArgs = 2
ExitNotLoggedIn = 3
ExitMissingParams = 4
)
func (self *GlobalConfig) EndpointUrl() string {
if self.endpointUrl != "" {
return self.endpointUrl
}
hosts := map[string]string{
"us": "api.kii.com",
"jp": "api-jp.kii.com",
"cn": "api-cn2.kii.com",
"sg": "api-sg.kii.com",
}
p := Profile()
host := hosts[p.Site]
if host == "" {
print("missing site, use --site or set KII_SITE\n")
os.Exit(ExitMissingParams)
}
return fmt.Sprintf("https://%s/api", host)
}
func (self *GlobalConfig) EndpointUrlForApiLog() string {
if self.devlogUrl != "" {
return self.devlogUrl
}
hosts := map[string]string{
"us": "apilog.kii.com",
"jp": "apilog-jp.kii.com",
"cn": "apilog-cn2.kii.com",
"sg": "apilog-sg.kii.com",
}
p := Profile()
host := hosts[p.Site]
if host == "" {
print("missing site, use --site or set KII_SITE\n")
os.Exit(ExitMissingParams)
}
return fmt.Sprintf("wss://%s:443/logs", host)
}
func (self *GlobalConfig) HttpHeaders(contentType string) map[string]string {
p := Profile()
m := map[string]string{
"x-kii-appid": p.AppId,
"x-kii-appkey": p.AppKey,
}
if len(contentType) > 0 {
m["content-type"] = contentType
}
return m
}
func (self *GlobalConfig) HttpHeadersWithAuthorization(contentType string) map[string]string {
m := self.HttpHeaders(contentType)
oauth2 := (&OAuth2Response{}).Load()
// overwirte token with given value
token := oauth2.AccessToken
if self.Token != "" {
token = self.Token
}
m["authorization"] = fmt.Sprintf("Bearer %s", token)
return m
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Return ~/.kii/${filename}
func metaFilePath(dir string, filename string) string {
d := DirPath([]string{dir})
return d.MetaFilePath(filename)
}
type DirPath []string
func (dir DirPath) MetaFilePath(filename string) string {
homedir, err := homedir.Dir()
if err != nil {
log.Fatalf("%v", err)
}
confdirpath := path.Join(homedir, ".kii", path.Join(dir...))
err = os.MkdirAll(confdirpath, os.ModeDir|0700)
if err != nil {
log.Fatalf("%v", err)
}
return path.Join(confdirpath, filename)
}
var globalConfig *GlobalConfig
var _config = `# You can configure default profile, or --profile option is available
# profile = jp
[default]
app_id =
app_key =
client_id =
client_secret =
site = us
[jp]
app_id =
app_key =
client_id =
client_secret =
site = jp
`
func loadIniFile(configPath string) (*ini.File, bool /* true: generated config */) {
if b, _ := exists(configPath); !b {
ioutil.WriteFile(configPath, []byte(_config), 0600)
return nil, true
}
file, err := ini.LoadFile(configPath)
if err != nil {
log.Fatalf("%v", err)
}
return &file, false
}
func pickup(a ...string) string {
for _, s := range a {
if s != "" {
return s
}
}
return ""
}
const DEFAULT_PROFILE = "default"
func profilePath(c *cli.Context) string {
p := metaFilePath(".", "config")
if c.String("profile-path") != "" {
return c.String("profile-path")
}
return p
}
func SetupFlags(app *cli.App) {
app.Flags = []cli.Flag{
cli.StringFlag{Name: "app-id", Value: "", Usage: "AppID"},
cli.StringFlag{Name: "app-key", Value: "", Usage: "AppKey"},
cli.StringFlag{Name: "client-id", Value: "", Usage: "ClientID"},
cli.StringFlag{Name: "client-secret", Value: "", Usage: "ClientSecret"},
cli.StringFlag{Name: "token", Value: "", Usage: "Token to be used"},
cli.StringFlag{Name: "site", Value: "", Usage: "us,jp,cn,sg"},
cli.StringFlag{Name: "endpoint-url", Value: "", Usage: "Site URL"},
cli.StringFlag{Name: "log-url", Value: "", Usage: "Log URL"},
cli.BoolFlag{Name: "verbose", Usage: "Verbosely"},
cli.StringFlag{Name: "profile,p", Value: DEFAULT_PROFILE, Usage: "Profile name for ~/.kii/config"},
cli.StringFlag{Name: "profile-path", Usage: "Profile path instead of ~/.kii/config"},
cli.BoolFlag{Name: "curl", Usage: "Print curl command saving body as a tmp file if body exists"},
cli.BoolFlag{Name: "suppress-exit", Usage: "Suppress exit with 1 when receiving status code other than 2xx"},
cli.StringFlag{Name: "http-proxy", Usage: "HTTP proxy URL to be used"},
cli.BoolFlag{Name: "disable-http-proxy", Usage: "Disable HTTP proxy in your profile"},
cli.BoolFlag{Name: "use-utc", Usage: "Format time in UTC"},
}
app.Before = func(c *cli.Context) error {
// Setup logger
if c.Bool("verbose") {
logger = log.New(os.Stderr, "", log.LstdFlags)
}
profilePath := profilePath(c)
logger.Printf("profile-path: %v", profilePath)
inifile, gen := loadIniFile(profilePath)
if gen {
print(fmt.Sprintf("%v was created. please fill it with your credentials.\n", profilePath))
os.Exit(ExitGeneralReason)
}
profile, _ := inifile.Get("", "profile")
if profile == "" {
profile = DEFAULT_PROFILE
}
if optProf := c.GlobalString("profile"); optProf != DEFAULT_PROFILE {
profile = optProf
}
logger.Printf("profile: %v", profile)
if profile != DEFAULT_PROFILE && len((*inifile)[profile]) == 0 {
print(fmt.Sprintf("profile %s is not found in %v\n", profile, profilePath))
os.Exit(ExitMissingParams)
}
getConf := func(gn, en, un string) string {
ev := os.ExpandEnv("${" + en + "}")
uv, _ := inifile.Get(profile, un)
return pickup(c.GlobalString(gn), ev, uv)
}
globalConfig = &GlobalConfig{
AppId: getConf("app-id", "KII_APP_ID", "app_id"),
AppKey: getConf("app-key", "KII_APP_KEY", "app_key"),
ClientId: getConf("client-id", "KII_CLIENT_ID", "client_id"),
ClientSecret: getConf("client-secret", "KII_CLIENT_SECRET", "client_secret"),
Token: getConf("token", "KII_TOKEN", ""),
Site: getConf("site", "KII_SITE", "site"),
endpointUrl: getConf("endpoint-url", "KII_ENDPOINT_URL", "endpoint_url"),
devlogUrl: getConf("log-url", "KII_LOG_URL", "log_url"),
Curl: c.GlobalBool("curl"),
SuppressExit: c.GlobalBool("suppress-exit"),
UTC: c.GlobalBool("use-utc"),
}
proxy := c.String("http-proxy")
if proxy == "" && !c.Bool("disable-http-proxy") {
p, _ := inifile.Get(profile, "http_proxy")
proxy = p
if proxy == "" {
p, _ := inifile.Get("", "http_proxy")
proxy = p
}
}
if proxy != "" {
logger.Printf("http_proxy: %v", proxy)
os.Setenv("HTTP_PROXY", proxy)
}
return nil
}
}
func Flatten(a []cli.Command) []cli.Command {
b := make([]cli.Command, 0, 16)
for _, v := range a {
if v.Subcommands == nil {
b = append(b, v)
} else {
for _, i := range v.Subcommands {
i.Name = v.Name + ":" + i.Name
b = append(b, i)
}
}
}
return b
}