-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
56 lines (46 loc) · 1.33 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
package main
import (
"encoding/json"
"log"
"os"
"path/filepath"
)
// HonkConfig struct to hold the honk configuration
type HonkConfig struct {
TwitterConsumerKey string
TwitterConsumerSecretKey string
TwitterAccessToken string
TwitterAccessSecret string
UnsplashClientID string
TwitterSearchCounts string
}
// Config for honk bot
var Config = &HonkConfig{}
func loadConfig(fileName string) error {
fileName = findConfigFile(fileName)
log.Println("Loading " + fileName)
file, err := os.Open(fileName)
if err != nil {
log.Println("Error opening config file=" + fileName + ", err=" + err.Error())
return err
}
decoder := json.NewDecoder(file)
err = decoder.Decode(Config)
if err != nil {
log.Println("Error decoding config file=" + fileName + ", err=" + err.Error())
return err
}
return nil
}
func findConfigFile(fileName string) string {
if _, err := os.Stat("/tmp/" + fileName); err == nil {
fileName, _ = filepath.Abs("/tmp/" + fileName)
} else if _, err := os.Stat("./config/" + fileName); err == nil {
fileName, _ = filepath.Abs("./config/" + fileName)
} else if _, err := os.Stat("../config/" + fileName); err == nil {
fileName, _ = filepath.Abs("../config/" + fileName)
} else if _, err := os.Stat(fileName); err == nil {
fileName, _ = filepath.Abs(fileName)
}
return fileName
}