-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
83 lines (76 loc) · 2.04 KB
/
init.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
package main
import (
"flag"
"github.com/charmbracelet/log"
"os"
"strconv"
)
var (
username string
password string
url string
directory string
help bool
restore bool
alerting bool
dashboards bool
datasources bool
)
func init() {
flag.StringVar(&username, "u", "", "Username for Grafana Instance")
flag.StringVar(&password, "p", "", "Password for Grafana Instance")
flag.StringVar(&url, "url", "", "Baseurl for your Grafana Instance.")
flag.StringVar(&directory, "directory", "", "Directory where Output/Input is stored")
flag.BoolVar(&help, "h", false, "The Help")
flag.BoolVar(&restore, "r", false, "Restore of provided backup Directory")
flag.BoolVar(&alerting, "alerting", false, "Export or Restore of the Alerting Objects including Alert Rules, Contact Point, Notification Policies and Notification Templates")
flag.BoolVar(&dashboards, "dashboard", true, "Export or Restore of the Dashboards, Folders and Library Panels. Default is always true")
flag.BoolVar(&datasources, "datasources", true, "Export or Restore of the Datasources. Default is always true")
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
if username == "" {
username = os.Getenv("username")
}
if password == "" {
password = os.Getenv("password")
}
if url == "" {
url = os.Getenv("url")
}
if directory == "" {
directory = os.Getenv("directory")
}
if !alerting {
alerting = getEnvBool("alerting")
}
if len(os.Getenv("dashboard")) > 0 {
dashboards = getEnvBool("dashboard")
}
if len(os.Getenv("datasources")) > 0 {
datasources = getEnvBool("datasources")
}
if !restore {
restore = getEnvBool("restore")
}
info, err := os.Stat(directory)
if os.IsNotExist(err) {
log.Fatal("No directory provided. \n Please create target folder")
}
if !info.IsDir() {
log.Fatal("Path is not a directory.")
}
}
func getEnvBool(key string) bool {
value := os.Getenv(key)
if value == "" {
return false
}
binary, err := strconv.ParseBool(value)
if err != nil {
log.Fatal("Not able to parse Boolean ", key)
}
return binary
}