-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
122 lines (113 loc) · 5.48 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
package main
import (
"code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/plugin"
"code.cloudfoundry.org/cli/util/configv3"
"fmt"
"github.com/cloudfoundry/go-cfclient/v3/client"
"github.com/cloudfoundry/go-cfclient/v3/config"
"github/metskem/panzer-plugin/conf"
"github/metskem/panzer-plugin/event"
"github/metskem/panzer-plugin/version"
"os"
)
const (
ListAppsHelpText = "Lists basic information of apps in the current space"
ListRoutesHelpText = "Find the routes with their domain/org/space"
)
var (
ListAppsUsage = fmt.Sprintf("aa [-a appname-filter] [-q] [-u], use \"cf aa -help\" for full help message - Use the envvar CF_COLS to specify the output columns, available columns are (comma separated): %s", ValidColumns)
ListRoutesUsage = "lr [-t] <-r host-to-lookup>, use \"cf lr -help\" for full help message- Specify the host without the domain name, we will find all routes using this hostname, if option -t given we will also target the org/space"
)
// PanzerPlugin is the struct implementing the interface defined by the core CLI. It can be found at "code.cloudfoundry.org/cli/plugin/plugin.go"
type PanzerPlugin struct{}
// Run must be implemented by any plugin because it is part of the plugin interface defined by the core CLI.
//
// Run(....) is the entry point when the core CLI is invoking a command defined by a plugin.
// The first parameter, plugin.CliConnection, is a struct that can be used to invoke cli commands. The second parameter, args, is a slice of strings.
// args[0] will be the name of the command, and will be followed by any additional arguments a cli user typed in.
//
// Any error handling should be handled with the plugin itself (this means printing user facing errors).
// The CLI will exit 0 if the plugin exits 0 and will exit 1 should the plugin exits nonzero.
func (c *PanzerPlugin) Run(cliConnection plugin.CliConnection, args []string) {
preCheck(cliConnection)
cfHomeDir := os.Getenv("CF_HOME")
if cfHomeDir == "" {
cfHomeDir = os.Getenv("HOME")
}
if cfConfig, err := config.NewFromCFHomeDir(cfHomeDir); err != nil {
fmt.Printf("failed to create new config: %s", err)
os.Exit(1)
} else {
if conf.CfClient, err = client.New(cfConfig); err != nil {
fmt.Printf("failed to create new cf client: %s\n", err)
os.Exit(1)
}
}
switch args[0] {
case "aa":
checkTarget(cliConnection)
listApps(cliConnection)
case "lr":
listRoutes()
case "ev":
event.GetEvents(cliConnection)
}
}
// GetMetadata returns a PluginMetadata struct. The first field, Name, determines the name of the plugin which should generally be without spaces.
// If there are spaces in the name a user will need to properly quote the name during uninstall otherwise the name will be treated as separate arguments.
// The second value is a slice of Command structs. Our slice only contains one Command Struct, but could contain any number of them.
// The first field Name defines the command `cf basic-plugin-command` once installed into the CLI.
// The second field, HelpText, is used by the core CLI to display help information to the user in the core commands `cf help`, `cf`, or `cf -h`.
func (c *PanzerPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "panzer",
Version: plugin.VersionType{Major: version.GetMajorVersion(), Minor: version.GetMinorVersion(), Build: version.GetPatchVersion()},
MinCliVersion: plugin.VersionType{Major: 6, Minor: 7, Build: 0},
Commands: []plugin.Command{
{Name: "aa", HelpText: ListAppsHelpText, UsageDetails: plugin.Usage{Usage: ListAppsUsage}},
{Name: "lr", HelpText: ListRoutesHelpText, UsageDetails: plugin.Usage{Usage: ListRoutesUsage}},
{Name: "ev", HelpText: event.ListEventsHelpText, UsageDetails: plugin.Usage{Usage: event.ListEventsUsage}},
},
}
}
// checkTarget Checks if you currently have a targeted org and space.
func checkTarget(cliConnection plugin.CliConnection) {
hasOrg, err := cliConnection.HasOrganization()
if err != nil || !hasOrg {
fmt.Println(terminal.FailureColor("please target your org/space first"))
os.Exit(1)
}
org, _ := cliConnection.GetCurrentOrg()
conf.CurrentOrg = org
hasSpace, err := cliConnection.HasSpace()
if err != nil || !hasSpace {
fmt.Println(terminal.FailureColor("please target your space first"))
os.Exit(1)
}
space, _ := cliConnection.GetCurrentSpace()
conf.CurrentSpace = space
}
// preCheck Does all common validations, like being logged in.
func preCheck(cliConnection plugin.CliConnection) {
v3Config, _ := configv3.LoadConfig()
i18n.T = i18n.Init(v3Config)
loggedIn, err := cliConnection.IsLoggedIn()
if err != nil || !loggedIn {
fmt.Println(terminal.NotLoggedInText())
os.Exit(1)
}
conf.CurrentUser, _ = cliConnection.Username()
}
// Unlike most Go programs, the `Main()` function will not be used to run all the commands provided in your plugin.
// Main will be used to initialize the plugin process, as well as any dependencies you might require for your plugin.
func main() {
// Any initialization for your plugin can be handled here
//
// Note: to run the plugin.Start method, we pass in a pointer to the struct implementing the interface defined at "code.cloudfoundry.org/cli/plugin/plugin.go"
//
// Note: The plugin's main() method is invoked at install time to collect metadata. The plugin will exit 0 and the Run([]string) method will not be invoked.
plugin.Start(new(PanzerPlugin))
// Plugin code should be written in the Run([]string) method, ensuring the plugin environment is bootstrapped.
}