-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (95 loc) · 3.07 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
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
106
107
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/sivukhin/cuemon/lib"
"github.com/sivukhin/cuemon/lib/auth"
)
var (
bootstrap = flag.NewFlagSet("bootstrap", flag.ExitOnError)
bootstrapInput = bootstrap.String("input", "", "input file with Grafana dashboard JSON (stdin if not provided)")
bootstrapDir = bootstrap.String("dir", "", "target directory where cuemon will be initialized")
bootstrapOverwrite = bootstrap.Bool("overwrite", false, "enable unsafe mode which can overwrite files")
export = flag.NewFlagSet("export", flag.ExitOnError)
push = flag.NewFlagSet("push", flag.ExitOnError)
pushMessage = push.String("message", "", "message describing dashboard updates")
pushGrafana = push.String("grafana", "", "url to Grafana instance")
pushPlayground = push.String("playground", "", "playground dashboard name which will be updated instead of original dashboard id")
)
func printUsage() {
fmt.Println("cuemon:")
bootstrap.Usage()
export.Usage()
push.Usage()
}
func multilineErr(err error, ident string) string {
result := err.Error()
lines := strings.Split(result, "\n")
if len(lines) == 1 {
return lines[0]
}
return strings.Join(append([]string{""}, lines...), "\n"+ident)
}
const (
errIdent = " "
authorizationSubject = "GRAFANA"
// cuemonModule must be in sync with CUE package name, which is used in lib/cue/ definitions
cuemonModule = "cuemon"
)
func run(args []string) error {
var tags []string
tagSet := func(s string) error {
tags = append(tags, s)
return nil
}
push.Func("t", "tags for cue export", tagSet)
export.Func("t", "tags for cue export", tagSet)
switch args[0] {
case "export":
if err := export.Parse(args[1:]); err != nil {
return fmt.Errorf("export error: %v", multilineErr(err, errIdent))
}
result, err := lib.Export(export.Args(), tags)
if err != nil {
return fmt.Errorf("export error: %v", multilineErr(err, errIdent))
}
fmt.Printf("%v", result)
case "bootstrap":
if err := bootstrap.Parse(args[1:]); err != nil {
return fmt.Errorf("bootstrap error: %v", multilineErr(err, errIdent))
}
if err := lib.Bootstrap(*bootstrapInput, cuemonModule, *bootstrapDir, *bootstrapOverwrite); err != nil {
return fmt.Errorf("bootstrap error: %v", multilineErr(err, errIdent))
}
case "push":
if err := push.Parse(args[1:]); err != nil {
return fmt.Errorf("push error: %v", multilineErr(err, errIdent))
}
authorization, err := auth.AnalyzeSubjectAuthorization(os.Environ())
if err != nil {
return fmt.Errorf("failed to analyze authorization methods: %w", err)
}
if err := lib.Push(strings.TrimRight(*pushGrafana, "/"), authorization[authorizationSubject], *pushMessage, *pushPlayground, tags, push.Args()); err != nil {
return fmt.Errorf("push error: %v", multilineErr(err, errIdent))
}
case "help":
printUsage()
default:
return fmt.Errorf("unknown command: %v", args[0])
}
return nil
}
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(2)
}
err := run(os.Args[1:])
if err != nil {
fmt.Printf("%v\n", err)
printUsage()
os.Exit(2)
}
}