-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (89 loc) · 2.58 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
package main
import "fmt"
import "github.com/spf13/cast"
import "github.com/spf13/viper"
import tea "github.com/charmbracelet/bubbletea"
func (m model) validateOptions() bool {
// Check of there are any languages selected
noLangs := true
for _, lang := range m.session.Languages {
if lang.Selected {
noLangs = false
break
}
}
if noLangs {
return false
}
// Check of there are any algorithms selected
noAlgs := true
for _, alg := range m.session.Algorithms {
if alg.Selected {
noAlgs = false
break
}
}
if noAlgs {
return false
}
return true
}
func (m model) checkConfig() tea.Msg {
algorithms, languages, questionCount := getConfig()
if len(languages) == 0 {
return errMsg{fmt.Errorf("No languages found in config file")}
}
if len(algorithms) == 0 {
return errMsg{fmt.Errorf("No algorithms found in config file")}
}
// Check each algorithm in config file to make sure they have `name`, `type` keys.
for _, alg := range algorithms {
if alg.Name == "" || alg.Type == "" {
return errMsg{fmt.Errorf("One of the Algorithms in the config.toml file is missing either name or type")}
}
}
config := Config{Algorithms: algorithms, Languages: languages, QuestionCount: questionCount}
return configMsg(config)
}
func saveConfig(alg []Algorithm, lang []Language, questionCount int) {
viper.Set("algorithm", alg)
viper.Set("language", lang)
viper.Set("question_count", questionCount)
viper.WriteConfig()
}
func getConfig() ([]Algorithm, []Language, int) {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Error reading config file", err)
panic(err)
}
var c []Algorithm
alg, ok := viper.Get("algorithm").([]interface{})
if !ok {
fmt.Println("Error getting algorithms")
panic(1)
} else {
for _, table := range alg {
if m, ok := table.(map[string]interface{}); ok {
c = append(c, Algorithm{Name: cast.ToString(m["name"]), Type: cast.ToString(m["type"]), Sorted: cast.ToBool(m["sorted"]), Selected: cast.ToBool(m["selected"])})
}
}
}
var languages []Language
lang, ok := viper.Get("language").([]interface{})
if !ok {
fmt.Println("Error getting languages")
panic(1)
} else {
for _, table := range lang {
if m, ok := table.(map[string]interface{}); ok {
languages = append(languages, Language{Name: cast.ToString(m["name"]), Selected: cast.ToBool(m["selected"])})
}
}
}
questionCount := viper.GetInt("question_count")
return c, languages, questionCount
}