This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
/
main.go
105 lines (85 loc) · 2.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
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 (
"encoding/json"
"flag"
"fmt"
"os"
"text/tabwriter"
"time"
"github.com/pivotal-cloudops/bosh-stats/deployments"
)
func printHeader(w *tabwriter.Writer) {
fmt.Fprintln(w, "Deployment", "\t", "Count")
fmt.Fprintln(w, "--------------------", "\t", "--------------------")
}
func friendlyCalendarMonth(calendarMonth *string) string {
parsedMonth, err := time.Parse("2006/01", *calendarMonth)
friendlyCalendarMonth := parsedMonth.Format("Jan 2006")
if err != nil {
friendlyCalendarMonth = *calendarMonth
}
return friendlyCalendarMonth
}
func printJSON(numberByDeployment map[string]int) {
jsonOutput, err := json.Marshal(numberByDeployment)
fmt.Println(string(jsonOutput[:]))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func printResults(numberByDeployment map[string]int, calendarMonth *string) {
totalDeploys := 0
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug)
printHeader(w)
for k, v := range numberByDeployment {
totalDeploys += v
fmt.Fprintln(w, k, "\t", v, "deploys")
}
fmt.Println()
fmt.Fprintln(w, "--------------------", "\t", "--------------------")
fmt.Fprintln(w, friendlyCalendarMonth(calendarMonth), "\t", totalDeploys, "total deploys")
w.Flush()
}
func main() {
var outputJson bool
directorURL := flag.String("directorUrl", "", "bosh director URL")
uaaURL := flag.String("uaaUrl", "", "UAA URL")
uaaClientID := flag.String("uaaClientId", "", "UAA Client ID")
uaaClientSecret := flag.String("uaaClientSecret", "", "UAA Client Secret")
caCert := flag.String("caCert", "", "CA Cert")
calendarMonth := flag.String("calendarMonth", "", "Calendar month/year YYYY/MM")
repaveUser := flag.String("repaveUser", "", "The username to filter out as the 'repave' user")
deployment := flag.String("deployment", "", "The deployment to filter out")
releaseName := flag.String("release", "", "The release to filter for the deploy date")
releaseVersion := flag.String("version", "", "The version to filter for the deploy date")
flag.BoolVar(&outputJson, "json", false, "print JSON to standard out (output is a table by default)")
flag.Parse()
deployCounter := deployments.DeployCounter{
DirectorURL: *directorURL,
UaaURL: *uaaURL,
UaaClientID: *uaaClientID,
UaaClientSecret: *uaaClientSecret,
CaCert: *caCert,
}
if *releaseName == "" {
numberByDeployment := make(map[string]int)
err := deployCounter.SuccessfulDeploys(*calendarMonth, 200, *repaveUser, &numberByDeployment, *deployment)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if outputJson {
printJSON(numberByDeployment)
} else {
printResults(numberByDeployment, calendarMonth)
}
} else {
date, err := deployCounter.DeployDate(*releaseName, *releaseVersion, 200)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(date)
}
}