-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
63 lines (54 loc) · 1.34 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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
func findConfig() string {
if fileExists("./config.toml") {
return "config.toml"
}
config_dir := os.Getenv("XDG_CONFIG_HOME")
path := filepath.Join(config_dir, "genkey", "config.toml")
if fileExists(path) {
return path
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
path = filepath.Join(home, ".config", "genkey", "config.toml")
if fileExists(path) {
return path
}
println("Couldn't find config.toml in any of local directory, $XDG_CONFIG_HOME/genkey/config.toml, or ~/.config/genkey/config.toml.")
os.Exit(1)
return ""
}
func ReadWeights() {
path := findConfig()
b, err := os.ReadFile(path)
if err != nil {
fmt.Printf("There was an issue reading the config file.")
panic(err)
}
_, err = toml.Decode(string(b), &Config)
if err != nil {
panic(err)
}
if !fileExists(filepath.Join(Config.Paths.Corpora, Config.Corpus) + ".json") {
fmt.Printf("Invalid config: Corpus [%s] does not exist.\n", Config.Corpus)
os.Exit(1)
}
if Config.Generation.Selection > Config.Generation.InitialPopulation {
fmt.Println("Invalid config: Generation.Selection cannot be greater than Generation.InitialPopulation.")
os.Exit(1)
}
}