-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (84 loc) · 2.02 KB
/
Copy pathmain.go
File metadata and controls
105 lines (84 loc) · 2.02 KB
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
96
97
98
99
100
101
102
103
104
105
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
_ "embed"
)
//go:embed check.go.tmpl
var checkTmpl string
const (
configFile = "config.yml"
checksDir = "pkg/checks/"
)
func main() {
config, err := os.ReadFile(configFile)
if err != nil {
logrus.WithError(err).Fatal("failed to read config file")
}
var data struct {
Checks map[string]string `yaml:"checks"`
}
err = yaml.Unmarshal(config, &data)
if err != nil {
logrus.WithError(err).Fatal("failed to unmarshal config file")
}
err = filepath.Walk(checksDir, cleanChecksDir)
if err != nil {
logrus.WithError(err).Fatal("failed to clean checks directory")
}
tmpl, err := template.New("check.go.tmpl").Parse(checkTmpl)
if err != nil {
logrus.WithError(err).Fatal("failed to parse template")
}
for name, remote := range data.Checks {
var cleanRemote string
if strings.Contains(remote, "@") {
cleanRemote = strings.Split(remote, "@")[0]
} else {
cleanRemote = remote
}
out, err := os.Create(filepath.Join(checksDir, name+".go"))
if err != nil {
logrus.WithError(err).Fatalf("failed to create check file: \"%s.go\"", name)
}
err = tmpl.Execute(out, struct {
Name string
Remote string
CleanRemote string
}{
Name: name,
Remote: remote,
CleanRemote: cleanRemote,
})
if err != nil {
logrus.WithError(err).Fatal("failed to execute template")
}
}
for _, remote := range data.Checks {
cmd := exec.Command("go", "get", remote)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
logrus.WithError(err).Fatalf("failed to get remote: \"%s\"", remote)
}
}
err = exec.Command("go", "mod", "tidy").Run()
if err != nil {
logrus.WithError(err).Fatal("failed to tidy go modules")
}
}
func cleanChecksDir(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() != "main.go" && !info.IsDir() {
return os.Remove(path)
}
return nil
}