This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
91 lines (74 loc) · 2.01 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
type action struct {
Day int `yaml:"day"`
Action string `yaml:"action"`
Last bool `yaml:"last"`
Message string `yaml:"message"`
}
type actionSlice []action
//Len is part of sort.Interface.
func (d actionSlice) Len() int {
return len(d)
}
// Swap is part of sort.Interface.
func (d actionSlice) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
// Less is part of sort.Interface. We use count as the value to sort by
func (d actionSlice) Less(i, j int) bool {
return d[i].Day > d[j].Day
}
type options struct {
Token string `yaml:"token"`
Actions actionSlice `yaml:"actions"`
Repos []string `yaml:"repos"`
OnlyWorkdays bool `yaml:"only_workdays"`
}
func parseConfig(filePath string) {
var data []byte
var err error
data, err = ioutil.ReadFile(filePath)
checkError("Failed to read config file.", err)
err = yaml.Unmarshal([]byte(data), &runConfig)
checkError("Failed to parse config file.", err)
// Check syntax and defaults
configDefaults()
}
func configDefaults() {
// Token section
if runConfig.Token == "" {
runConfig.Token = os.Getenv("GITHUB_TOKEN")
if runConfig.Token == "" {
checkError("You need to define github in config or ENV 'GITHUB_TOKEN'", errors.New("token not defined"))
}
}
// Repo section check
if len(runConfig.Repos) == 0 {
printError("You must define a repo section in your config")
}
// Sort by day
sort.Sort(runConfig.Actions)
for _, repository := range runConfig.Repos {
repositoryValue := strings.Split(repository, "/")
if len(repositoryValue) != 2 {
printError(fmt.Sprintf("Repository %s is in wrong format", repository))
}
}
if len(runConfig.Actions) == 0 {
printError("No actions defined.")
}
for _, actionItem := range runConfig.Actions {
if strings.ToLower(actionItem.Action) != "warn" && strings.ToLower(actionItem.Action) != "close" {
printError(fmt.Sprintf("Unsupported action '%s'.", actionItem.Action))
}
}
}