-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.83 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
markdown "github.com/MichaelMure/go-term-markdown"
"github.com/eggfoobar/promdiff/pkg/config"
"github.com/eggfoobar/promdiff/pkg/prom"
"github.com/eggfoobar/promdiff/pkg/templates"
)
var (
configFile string
outputFile string
outputFormat string
templateFile string
lineWidth int
leftPadding int
)
func init() {
flag.StringVar(&configFile, "c", "prom.yaml", "config file to use")
flag.StringVar(&outputFormat, "o", "markdown", "output format (ex. markdown, jira, text)")
flag.StringVar(&outputFile, "f", "", "output file to save data (default: stdout)")
flag.StringVar(&templateFile, "t", "", "custom template file to use")
flag.IntVar(&lineWidth, "w", 90, "line width for markdown print out (only used when printing to markdown stdout)")
flag.IntVar(&leftPadding, "lp", 0, "left hand padding markdown print out (only used when printing to markdown stdout)")
flag.Parse()
}
func main() {
conf, err := config.NewConfig(configFile)
if err != nil {
panic(err)
}
results, err := prom.FetchData(conf)
if err != nil {
panic(fmt.Errorf("failed to get data for target (%s)", err))
}
var output templates.OutputTemplate
if templateFile != "" {
output, err = templates.ParseWithTemplateFile(templateFile, results)
} else {
output, err = templates.Parse(outputFormat, results)
}
if err != nil {
panic(err)
}
printout(output, outputFile)
}
func printout(output templates.OutputTemplate, outputFile string) error {
if outputFile != "" {
err := os.WriteFile(outputFile, []byte(output.ParsedTemplate), os.ModePerm)
if err != nil {
return err
}
return nil
}
printOut := output.ParsedTemplate
if output.Type == templates.Markdown {
result := markdown.Render(output.ParsedTemplate, lineWidth, leftPadding)
printOut = string(result)
}
fmt.Println(printOut)
return nil
}