-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
60 lines (48 loc) · 1.59 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
package main
import (
"errors"
"io"
"github.com/BurntSushi/toml"
)
// Point holds the points required for a challenge.
type Point struct {
Value int `toml:"value"`
}
// Config holds the main configuration items.
type Config struct {
// Directory where osprogramadores/op-website-hugo is cloned.
WebsiteDir string `toml:"website_dir"`
// Directory where osprogramadores/op-desafios is cloned.
ChallengesDir string `toml:"challenges_dir"`
// Go Template directory.
TemplateDir string `toml:"template_dir"`
// Points per challenge. This is a map where they key is taken from the
// challenge name (currently a number but could be anything in the future)
// and the value is the number of points this challenge is worth.
Points map[string]Point `toml:"points"`
// Ignore these usernames (admins, and others that don't benefit
// from showing in the scoreboard).
IgnoreUsers []string `toml:"ignore_users"`
}
// parseConfig parses the configuration string from the slice of bytes
// containing the TOML config read from disk and performs basic sanity checking
// of configuration items.
func parseConfig(r io.Reader) (Config, error) {
data, err := io.ReadAll(r)
if err != nil {
return Config{}, err
}
config := Config{}
if _, err := toml.Decode(string(data), &config); err != nil {
return Config{}, err
}
switch {
case config.WebsiteDir == "":
return Config{}, errors.New("WebsiteDir is empty")
case config.ChallengesDir == "":
return Config{}, errors.New("ChallengesDir is empty")
case config.TemplateDir == "":
return Config{}, errors.New("TemplateDir is empty")
}
return config, nil
}