summaryrefslogtreecommitdiff
path: root/internal/run.go
blob: a4fbf68a78af3937408f0db486f71438eb694810 (plain)
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 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 := newState(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(subject, body, conf); err != nil {
		notifyError(conf, err)
	}
}

func persistReport(subject, 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(fmt.Sprintf("%s\n\n", subject)); err != nil {
		return err
	}
	if _, err = f.WriteString(body); err != nil {
		return err
	}
	return os.Rename(tmpFile, reportFile)
}