-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
61 lines (51 loc) · 1.43 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
package main
import (
"fmt"
"io"
"log"
"os"
"gopkg.in/yaml.v3"
)
const CONFIGFILE = "config.yaml"
// Represents the underlying configuration
type Config struct {
AddonPath string `yaml:"addonPath"`
Addons map[string]bool `yaml:"addons"`
ModIDs map[string]int `yaml:"modIDs"`
API string `yaml:"api"`
}
// Constructor for the Config struct
func NewConfig() (Config, error) {
file, err := os.Open(CONFIGFILE)
if err != nil {
return Config{}, fmt.Errorf("failed to open YAML file: %v", err)
}
defer file.Close()
yamlContent, err := io.ReadAll(file)
if err != nil {
return Config{}, fmt.Errorf("failed to read YAML content: %v", err)
}
var config Config
if err := yaml.Unmarshal(yamlContent, &config); err != nil {
return Config{}, fmt.Errorf("failed to unmarshal YAML: %v", err)
}
if _, err := os.Stat(config.AddonPath); os.IsNotExist(err) {
return Config{}, fmt.Errorf("addonPath does not exist: %v\nPlease edit the %s file", CONFIGFILE, err)
}
return config, nil
}
// Returns a map of addonName: addonID of enabled addons
func getEnabledAddonModIDs(config Config) map[string]int {
enabledAddonModIDs := make(map[string]int)
for addon, enabled := range config.Addons {
if enabled {
modID, exists := config.ModIDs[addon]
if exists {
enabledAddonModIDs[addon] = modID
} else {
log.Println("Oh no! ModID not found for addon:", addon)
}
}
}
return enabledAddonModIDs
}