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
|
package internal
import (
"context"
"fmt"
"log"
"os"
)
func Run(ctx context.Context, configFile string, renotify, force bool) {
conf, err := newConfig(configFile)
if err != nil {
log.Fatal(err)
}
if err := conf.sanityCheck(); err != nil {
notifyError(conf, err)
}
state, err := readState(conf)
if err != nil {
notifyError(conf, err)
}
state = runChecks(ctx, state, conf)
if err := state.persist(); err != nil {
notifyError(conf, err)
}
subject, body, doNotify := state.report(renotify, force)
if doNotify {
if err := notify(conf, subject, body); err != nil {
log.Println("error:", err)
return
}
}
if err := persistReport(body, conf); err != nil {
notifyError(conf, err)
}
}
func persistReport(body string, conf config) error {
reportFile := fmt.Sprintf("%s/report.txt", conf.StateDir)
tmpFile := fmt.Sprintf("%s.tmp", reportFile)
f, err := os.Create(tmpFile)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(body); err != nil {
return err
}
return os.Rename(tmpFile, reportFile)
}
|