-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
167 lines (135 loc) · 4.1 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/fatih/color"
"gopkg.in/ini.v1"
)
/*
.ini General Settings, Discord Settings, and Module Credentials
.json Module Settings
*/
//#region Base Vars / Loading
var (
pathConfig = "config"
)
func loadConfig() map[string]error {
errors := make(map[string]error)
var tmperr error
if tmperr = loadConfig_General(); tmperr != nil {
errors["general"] = tmperr
}
if tmperr = loadConfig_Discord(); tmperr != nil {
errors["discord"] = tmperr
}
if tmperr = loadConfig_Modules_Credentials(); tmperr != nil {
errors["mod-credentials"] = tmperr
}
tmperrs := loadConfig_Modules()
for module, err := range tmperrs {
if err != nil {
errors[module] = err
}
}
return errors
}
func saveConfig(filepath string, i interface{}) error {
json, err := json.MarshalIndent(i, "", "\t")
if err != nil {
return err
}
err = os.WriteFile(filepath, json, 0644)
if err != nil {
return err
}
return nil
}
//#endregion
//#region General
var (
pathConfigGeneralSettings = pathConfig + string(os.PathSeparator) + "general.json"
pathConfigDiscordCredentials = pathConfig + string(os.PathSeparator) + "discord.ini"
pathConfigDiscordSettings = pathConfig + string(os.PathSeparator) + "discord.json"
)
func loadConfig_General() error {
prefixHere := "loadConfig_General(): "
// TODO: Creation prompts if missing
// LOAD JSON CONFIG
if _, err := os.Stat(pathConfigGeneralSettings); err != nil {
return fmt.Errorf("general config file not found: %s", err)
} else {
configBytes, err := os.ReadFile(pathConfigGeneralSettings)
if err != nil {
return fmt.Errorf("failed to read general config file: %s", err)
} else {
// Fix backslashes
configStr := string(configBytes)
configStr = strings.ReplaceAll(configStr, "\\", "\\\\")
for strings.Contains(configStr, "\\\\\\") {
configStr = strings.ReplaceAll(configStr, "\\\\\\", "\\\\")
}
// Parse
if err = json.Unmarshal([]byte(configStr), &generalConfig); err != nil {
return fmt.Errorf("failed to parse general config file: %s", err)
}
// Output?
if generalConfig.OutputSettings {
s, err := json.MarshalIndent(generalConfig, "", "\t")
if err != nil {
log.Println(color.HiRedString(prefixHere+"failed to output...\t%s", err))
} else {
log.Println(color.HiYellowString(prefixHere+"\n%s", color.YellowString(string(s))))
}
}
}
}
return nil
}
var (
generalConfig configGeneralSettings
)
type configGeneralSettings struct {
Verbose bool `json:"verbose"`
Debug bool `json:"debug"`
Debug2 bool `json:"debug2"` // verbose debug
OutputSettings bool `json:"outputSettings"`
DefaultColor string `json:"defaultColor,omitempty"`
}
//#endregion
//#region Modules
var (
pathConfigModules = pathConfig + string(os.PathSeparator) + "modules"
pathConfigModulesCredentials = pathConfigModules + string(os.PathSeparator) + "credentials.ini"
)
func loadConfig_Modules_Credentials() error {
// LOAD INI CREDS
if _, err := os.Stat(pathConfigModulesCredentials); err == nil {
config, err := ini.Load(pathConfigModulesCredentials)
if err != nil {
return fmt.Errorf("failed to parse module credentials file: %s", err)
} else {
flickrKey = config.Section("").Key("flickr_key").String()
instagramUsername = config.Section("").Key("instagram_username").String()
instagramPassword = config.Section("").Key("instagram_password").String()
spotifyClientID = config.Section("").Key("spotify_client_id").String()
spotifyClientSecret = config.Section("").Key("spotify_client_secret").String()
twitterUsername = config.Section("").Key("twitter_username").String()
twitterPassword = config.Section("").Key("twitter_password").String()
}
} else {
return fmt.Errorf("module credentials file not found: %s", err)
}
return nil
}
func loadConfig_Modules() map[string]error {
return map[string]error{
"mod-instagram": loadConfig_Module_Instagram(),
"mod-rss": loadConfig_Module_RSS(),
"mod-twitter": loadConfig_Module_Twitter(),
"mod-spotify": loadConfig_Module_Spotify(),
}
}
//#endregion