-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
156 lines (130 loc) · 4.7 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"strconv"
"code.cloudfoundry.org/cli/plugin"
flags "github.com/jessevdk/go-flags"
"code.cloudfoundry.org/app-autoscaler-cli-plugin/commands"
"code.cloudfoundry.org/app-autoscaler-cli-plugin/ui"
)
type AutoScaler struct{}
var BuildMajorVersion string
var BuildMinorVersion string
var BuildPatchVersion string
var BuildPrerelease string
var BuildMeta string
var BuildDate string
var BuildVcsUrl string
var BuildVcsId string
var BuildVcsIdDate string
func (as *AutoScaler) GetMetadata() plugin.PluginMetadata {
version := getVersion()
return plugin.PluginMetadata{
Name: "AutoScaler",
Version: version,
Commands: []plugin.Command{
{
Name: "autoscaling-api",
Alias: "asa",
HelpText: "Set or view AutoScaler service API endpoint",
UsageDetails: plugin.Usage{
Usage: `cf autoscaling-api [URL] [--unset] [--skip-ssl-validation]
OPTIONS:
--unset Unset the api endpoint,
--skip-ssl-validation Skip verification of the api endpoint. Not recommended! Inherit "cf" --skip-ssl-validation setting by default`,
},
},
{
Name: "autoscaling-policy",
Alias: "asp",
HelpText: "Retrieve the scaling policy of an application",
UsageDetails: plugin.Usage{
Usage: `cf autoscaling-policy APP_NAME [--output PATH_TO_FILE]
OPTIONS:
--output Dump the policy to a file in JSON format`,
},
},
{
Name: "attach-autoscaling-policy",
Alias: "aasp",
HelpText: "Attach a scaling policy to an application",
UsageDetails: plugin.Usage{
Usage: `cf attach-autoscaling-policy APP_NAME PATH_TO_FILE`,
},
},
{
Name: "detach-autoscaling-policy",
Alias: "dasp",
HelpText: "Detach the scaling policy from an application",
UsageDetails: plugin.Usage{
Usage: `cf detach-as-policy APP_NAME`,
},
},
{
Name: "autoscaling-metrics",
Alias: "asm",
HelpText: "Retrieve the metrics of an application",
UsageDetails: plugin.Usage{
Usage: `cf autoscaling-metrics APP_NAME METRIC_NAME [--start START_TIME] [--end END_TIME] [--asc] [--output PATH_TO_FILE]
METRIC_NAME:
memoryused, memoryutil, responsetime, throughput, cpu or custom metric names.
OPTIONS:
--start Start time of metrics collected with format "yyyy-MM-ddTHH:mm:ss+/-HH:mm" or "yyyy-MM-ddTHH:mm:ssZ", default to very beginning if not specified.
--end End time of the metrics collected with format "yyyy-MM-ddTHH:mm:ss+/-HH:mm" or "yyyy-MM-ddTHH:mm:ssZ", default to current time if not speficied.
--asc Display in ascending order, default to descending order if not specified.
--output Dump the metrics to a file in table format.
`,
},
},
{
Name: "autoscaling-history",
Alias: "ash",
HelpText: "Retrieve the scaling history of an application",
UsageDetails: plugin.Usage{
Usage: `cf autoscaling-history APP_NAME [--start START_TIME] [--end END_TIME] [--asc] [--output PATH_TO_FILE]
OPTIONS:
--start Start time of the scaling history with format "yyyy-MM-ddTHH:mm:ss+/-HH:mm" or "yyyy-MM-ddTHH:mm:ssZ", default to very beginning if not specified.
--end End time of the scaling history with format "yyyy-MM-ddTHH:mm:ss+/-HH:mm" or "yyyy-MM-ddTHH:mm:ssZ", default to current time if not speficied.
--asc Display in ascending order, default to descending order if not specified.
--output Dump the scaling history to a file in table format.
`,
},
},
},
}
}
func getVersion() plugin.VersionType {
// We set a default version 0.0.0, and then we try to parse the version from the build flags
// errors are ignored, as we will use the default version in case of errors
version := plugin.VersionType{Major: 0, Minor: 0, Build: 0}
version.Major, _ = strconv.Atoi(BuildMajorVersion)
version.Minor, _ = strconv.Atoi(BuildMinorVersion)
version.Build, _ = strconv.Atoi(BuildPatchVersion)
return version
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
version := getVersion()
fmt.Printf("Upstream Version: %d.%d.%d\n", version.Major, version.Minor, version.Build)
fmt.Println("Build Prerelease: ", BuildPrerelease)
fmt.Println("Build Version: ", BuildMeta)
fmt.Println("Build Date: ", BuildDate)
fmt.Println("VCS Url:", BuildVcsUrl)
fmt.Println("VCS Identifier: ", BuildVcsId)
fmt.Println("VCS Identifier Date: ", BuildVcsIdDate)
}
plugin.Start(new(AutoScaler))
}
func (as *AutoScaler) Run(cliConnection plugin.CliConnection, args []string) {
commands.AutoScaler.CLIConnection = cliConnection
parser := flags.NewParser(&commands.AutoScaler, flags.HelpFlag|flags.PassDoubleDash)
parser.NamespaceDelimiter = "-"
_, err := parser.ParseArgs(args)
if err != nil {
ui.SayFailed()
ui.SayMessage("Error: %s", err.Error())
os.Exit(1)
}
}