-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
143 lines (126 loc) · 3.26 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
package config
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
var configHolder Config
// Config contains general configuration details
type Config struct {
Kms bool
Token string
DefaultLanguage string
Blacklist []string
Whitelist []string
Languages map[string]languageConfig
LanguageDir string
}
type languageConfig struct {
Plugins map[string]LanguagePluginDetails
Language map[string]string
}
// LanguagePluginDetails holds the details for a plugin in a language
type LanguagePluginDetails struct {
Description string
Commands map[string]LanguagePluginCommandDetails
}
// LanguagePluginCommandDetails holds the details for a plugin command in a language
type LanguagePluginCommandDetails struct {
Description string
Command string
Texts map[string]string
}
var configFile []byte
var jsonConfig = true
var fallbackLanguage = "english.yml"
// GeneralConfig reads the configuration file and parses its general information
func GeneralConfig() (Config, error) {
if configHolder.Token == "" {
config := Config{}
err := ParseConfig(&config)
if err != nil {
return config, err
}
config.Token, err = decryptValue(config.Kms, config.Token)
if err != nil {
return config, err
}
if config.LanguageDir == "" {
config.LanguageDir = "language"
}
languageFiles, err := ioutil.ReadDir(config.LanguageDir)
if err != nil {
return config, err
}
languages := make(map[string]languageConfig)
for _, file := range languageFiles {
lConfig := languageConfig{}
err = parseLanguageFile(config.LanguageDir+"/"+file.Name(), &lConfig)
if err != nil {
return config, err
}
languages[file.Name()] = lConfig
}
config.Languages = languages
if config.DefaultLanguage == "" {
config.DefaultLanguage = fallbackLanguage
} else {
config.DefaultLanguage = strings.Replace(config.DefaultLanguage, ".yml", "", -1) + ".yml"
}
configHolder = config
}
return configHolder, nil
}
// GetConfigFile retrieves the contents of the config file as a byte array
func getConfigFile() ([]byte, error) {
if len(configFile) != 0 {
return configFile, nil
}
envConf := os.Getenv("IGOR_CONFIG")
if envConf != "" {
return []byte(envConf), nil
}
filename, _ := filepath.Abs("./config.json")
if _, err := os.Stat(filename); err != nil {
jsonConfig = false
filename, _ = filepath.Abs("./config.yml")
}
configFile, err := ioutil.ReadFile(filename)
if err != nil {
return configFile, err
}
return configFile, nil
}
// ParseConfig parses the config file and unmarshals it into the
// provided interface
func ParseConfig(values interface{}) error {
configFile, err := getConfigFile()
if err != nil {
return err
}
if jsonConfig {
if err := json.Unmarshal(configFile, &values); err != nil {
return err
}
} else {
if err := yaml.Unmarshal(configFile, values); err != nil {
return err
}
}
return nil
}
// parseLanguageFile returns the requested language file
func parseLanguageFile(languagefile string, values interface{}) error {
filename, _ := filepath.Abs(languagefile)
configFile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err := yaml.Unmarshal(configFile, values); err != nil {
return err
}
return nil
}