-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (99 loc) · 2.19 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"sync"
logger "github.com/sirupsen/logrus"
"github.com/superwomany/murafana/services"
"github.com/superwomany/murafana/utils"
)
func main() {
mode := flag.String("mode", "", "what task you want to run")
uid := flag.String("uid", "", "download specific dashboard uid to yaml file")
jfp := flag.String("file-path", "", "dashboard json file path")
dn := flag.String("name", "", "dashboard name used for import")
flag.Parse()
c := services.New()
switch *mode {
case "download-dashboard":
if len(*uid) > 0 {
err := c.GetDashboardByUID(*uid)
if err != nil {
logger.Error(err)
}
return
} else {
logger.Error("uid is required")
}
case "download-list":
err := DownloadDashboardList()
if err != nil {
logger.Error(err)
}
case "download-dashboards":
err := DownloadAllDashboards()
if err != nil {
logger.Error(err)
}
case "import-dashboard":
if len(*jfp) == 0 {
logger.Error("dashboard json file path is required")
}
d, err := os.Open(*jfp)
if err != nil {
logger.Error("read file data failed")
}
defer d.Close()
db, err := ioutil.ReadAll(d)
if err != nil {
logger.Error("read bytes failed")
}
err = c.ImportDashboardFromJson(*dn, db)
if err != nil {
logger.Error("import failed")
}
default:
logger.Error("unknown mode")
}
}
func DownloadAllDashboards() error {
c := services.New()
uids, err := c.ListDashboards()
if err != nil {
logger.Error(err)
return err
}
var pwg sync.WaitGroup
pwg.Add(len(uids))
errChan := make(chan error, len(uids))
logger.Info(uids, len(uids))
for _, v := range uids {
go func(c services.ServiceIntf, uid string) {
defer pwg.Done()
err := c.GetDashboardByUID(uid)
if err != nil {
nerr := fmt.Errorf("uid: %v, error: %v", uid, err)
errChan <- nerr
return
}
}(c, v)
}
pwg.Wait()
close(errChan)
if err := utils.ParseErrorsFromChannel(errChan); err != nil {
logger.Errorf("we have errors %v, but it is ok to continue.", err)
// return err
}
return nil
}
func DownloadDashboardList() error {
c := services.New()
_, err := c.ListDashboards("enabled")
if err != nil {
logger.Error(err)
return err
}
return nil
}