-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
74 lines (61 loc) · 3.2 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
package main
import (
"os"
log "github.com/sirupsen/logrus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
func initLogging(isDebugEnabled bool) {
log.SetOutput(os.Stdout)
if isDebugEnabled {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
}
func main() {
// flags
configFile := kingpin.Flag("config.file", "Azure configuration file").Default("sample-azure.yml").String()
isDebugEnabled := kingpin.Flag("debug", "Debug flag").Default("false").Bool()
// get command
getCommand := kingpin.Command("get", "Perform GET <url> against Azure Resource Manager API")
getCommandUrl := getCommand.Arg("url", "The <url>").Required().String()
// summary command
summaryCommand := kingpin.Command("resources", "Print out the Azure resources that exist on this subscription")
summaryCommandMaxContinuation := summaryCommand.Flag("maxcontinuation", "The max number of continuations to follow when calling ARM API. Default to 10.").Default("10").Int()
// grafana command
grafanaCommand := kingpin.Command("grafana", "Generate Grafana dashboard JSON files for given Azure resource type.")
grafanaCommandTitle := grafanaCommand.Flag("title", "This will be used as prefix in the dashboard title").Required().String()
grafanaCommandDataSourceName := grafanaCommand.Flag("datasource", "The Azure Monitor data source name on Grafana").Required().String()
grafanaCommandResourceType := grafanaCommand.Flag("resourcetype", "The Azure Resource Manager (ARM) resource type").Required().String()
grafanaCommandSubResourceType := grafanaCommand.Flag("subresourcetype", "The sub-resource type underneath the ARM resource type.").Default("").String()
grafanaCommandSubResourceName := grafanaCommand.Flag("subresourcename", "The resource name of the sub-resource type underneath the ARM resource type.").Default("").String()
grafanaCommandKind := grafanaCommand.Flag("kind", "The kind property on the Azure Resource Manager (ARM) resource type. This is optional.").Default("").String()
grafanaCommandMaxDashboardResources := grafanaCommand.Flag("maxdashboardresource", "The max number of Azure resources to include in each dashboard. Default to 10.").Default("10").Int()
grafanaCommandMaxContinuation := grafanaCommand.Flag("maxcontinuation", "The max number of continuations to follow when calling ARM API. Default to 10.").Default("10").Int()
command := kingpin.Parse()
// initialize logging after parsing flags
initLogging(*isDebugEnabled)
config := &Config{}
err := config.loadConfig(*configFile)
if err != nil {
log.Error(err)
os.Exit(1)
}
environment := getCurrentEnvironment(config.Credentials.Environment)
processor := NewCommandProcessor(config, environment)
// process commands
switch command {
case "get":
processor.processGetCommand(*getCommandUrl)
break
case "resources":
processor.processSummarizeCommand(*summaryCommandMaxContinuation)
break
case "grafana":
processor.processGrafanaCommand(*grafanaCommandTitle, *grafanaCommandDataSourceName, *grafanaCommandMaxContinuation, *grafanaCommandMaxDashboardResources, *grafanaCommandResourceType, *grafanaCommandKind, *grafanaCommandSubResourceType, *grafanaCommandSubResourceName)
break
default:
log.Errorf("Unknown command: %s\n", command)
break
}
}