Passed
Push — master ( 3ebe4a...96c82d )
by Viktor
01:50
created

bot.*Config.GetLocaleLang   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
package bot
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"io/ioutil"
7
	"os"
8
9
	"github.com/BurntSushi/toml"
10
)
11
12
// WeatherConfig Weather config struct
13
type WeatherConfig struct {
14
	WeatherToken string
15
	City         string
16
}
17
18
// GeneralConfig General config struct
19
type GeneralConfig struct {
20
	Language         string
21
	Timezone         int
22
	GeonamesUsername string
23
	Game             string
24
	EmbedColor       int
25
	ServiceURL       string
26
	MessagePool      int
27
	DatabaseName     string
28
	GeocodingApiKey  string
0 ignored issues
show
introduced by
struct field GeocodingApiKey should be GeocodingAPIKey
Loading history...
29
	AdminID          string
30
}
31
32
// NewsConfig News config struct
33
type NewsConfig struct {
34
	APIKey   string
35
	Country  string
36
	Articles int
37
}
38
39
// MetricsConfig InfluxDB connection settings
40
type MetricsConfig struct {
41
	Address  string
42
	Database string
43
	User     string
44
	Password string
45
}
46
47
// DBLConfig contains bot list configs
48
type DBLConfig struct {
49
	Token    string
50
	TokenDBL string
51
	DBLID    string
52
}
53
54
// Twitch contains twitch api configs
0 ignored issues
show
introduced by
comment on exported type TwitchConfig should be of the form "TwitchConfig ..." (with optional leading article)
Loading history...
55
type TwitchConfig struct {
56
	ClientID string
57
}
58
59
// TranslateConfig Yandex translate config struct
60
type TranslateConfig struct {
61
	APIKey string
62
}
63
64
// CurrencyConfig Currency config struct
65
type CurrencyConfig struct {
66
	Default []string
67
}
68
69
// LocalesMap Map with locales
70
type LocalesMap map[string]map[string]string
71
72
// WeatherCodesMap symbols for font
73
type WeatherCodesMap map[string]string
74
75
// Config Main config struct. Contains all another config structs data.
76
type Config struct {
77
	Weather      WeatherConfig
78
	General      GeneralConfig
79
	News         NewsConfig
80
	Translate    TranslateConfig
81
	Locales      LocalesMap
82
	Currency     CurrencyConfig
83
	WeatherCodes WeatherCodesMap
84
	Metrics      MetricsConfig
85
	DBL          DBLConfig
86
	Twitch       TwitchConfig
87
}
88
89
// GetLocale returns locale string by key
90
func (c *Config) GetLocale(key string) string {
91
	return c.Locales[c.General.Language][key]
92
}
93
94
func (c *Config) GetLocaleLang(key, lang string) string {
0 ignored issues
show
introduced by
exported method Config.GetLocaleLang should have comment or be unexported
Loading history...
95
	if _, ok := c.Locales[lang]; ok {
96
		return c.Locales[lang][key]
97
	}
98
	return c.Locales[c.General.Language][key]
99
}
100
101
// LoadConfig loads configs from file 'config.toml'. Terminate program if error.
102
func LoadConfig() *Config {
103
	var cfg Config
104
	if _, err := toml.DecodeFile("config.toml", &cfg); err != nil {
105
		fmt.Printf("Config loading error: %v\n", err)
106
		os.Exit(1)
107
	}
108
	cfg.LoadLocales()
109
	cfg.LoadWeatherCodes()
110
	return &cfg
111
}
112
113
// LoadLocales loads locales from file 'locales.json'. Terminate program if error.
114
func (c *Config) LoadLocales() {
115
	file, e := ioutil.ReadFile("./locales.json")
116
	if e != nil {
117
		fmt.Printf("Locale file error: %v\n", e)
118
		os.Exit(1)
119
	}
120
121
	err := json.Unmarshal(file, &c.Locales)
122
	if err != nil {
123
		panic(err)
124
	}
125
126
	if _, ok := c.Locales[c.General.Language]; !ok {
127
		fmt.Printf("Locale file not contain language \"%v\"\n", c.General.Language)
128
		os.Exit(1)
129
	}
130
131
	fmt.Printf("Loaded %v translations for '%v' language\n", len(c.Locales[c.General.Language]), c.General.Language)
132
}
133
134
// LoadWeatherCodes loads weather font codes from file 'codes.json' in map. Terminate program if error.
135
func (c *Config) LoadWeatherCodes() {
136
	file, e := ioutil.ReadFile("./codes.json")
137
	if e != nil {
138
		fmt.Printf("Codes file error: %v\n", e)
139
		os.Exit(1)
140
	}
141
142
	err := json.Unmarshal(file, &c.WeatherCodes)
143
	if err != nil {
144
		panic(err)
145
	}
146
147
	fmt.Printf("Loaded %v weather codes\n", len(c.WeatherCodes))
148
}
149