forked from jeaniejung/Wildcard_Plugin
/
wildcard_plugin.go
161 lines (142 loc) · 4.78 KB
/
wildcard_plugin.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
//helpful link for match: https://golang.org/src/path/filepath/match_test.go
//GetApps()'s apps-summary is WAY too limited!!! They REALLY need to improve it >:(
//
package main
import (
"errors"
"fmt" //standard
"os"
//"reflect" //used to see type of object
"strconv"
"github.com/guidowb/cf-go-client/panic" //panics
"strings"
"github.com/cloudfoundry/cli/plugin/models"
//"github.com/cloudfoundry/cli/cf/api"
//"github.com/cloudfoundry/cli/cf/formatters"
"github.com/cloudfoundry/cli/plugin" //standard//https://github.com/cloudfoundry/cli/blob/8c310da376377c53f001d916708c056ce1558959/plugin/plugin.go
"path/filepath" //for matches//https://golang.org/pkg/path/filepath/
"github.com/cloudfoundry/cli/cf/terminal" //for table || https://github.com/cloudfoundry/cli/blob/4a108fd21d6633b250f6d9f46e870967cae96ac0/cf/terminal/table.go
//for implementing T
"github.com/cloudfoundry/cli/cf/trace"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/i18n/detection"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/configuration/config_helpers"
//for adding onto table
"github.com/cloudfoundry/cli/cf/formatters"
//"github.com/cloudfoundry/cli/cf/ui_helpers"
)
//Wildcard is this plugin
type Wildcard struct {
ui terminal.UI
matchedApps []plugin_models.ApplicationSummary
}
//GetMetadata returns metatada
func (cmd *Wildcard) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "wildcard",
Version: plugin.VersionType{ //leavealone
Major: 0,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{ //****** array of command structures
{
Name: "wildcard-apps",
Alias: "wc-a",
HelpText: "List all apps in the target space matching the wildcard",
UsageDetails: plugin.Usage{
Usage: "cf wildcard-apps APP_NAME_WITH_WILDCARD",
},
},
{
Name: "wildcard-delete",
Alias: "wc-d",
HelpText: "Delete apps in the target space matching the wildcard",
UsageDetails: plugin.Usage{
Usage: "cf wildcard-delete APP_NAME_WITH_WILDCARD",
},
},
},
}
}
func main() {
plugin.Start(new(Wildcard))
//plugin.Start(newWildcard())
}
func (cmd *Wildcard) usage(args []string) error {
badArgs := 3 != len(args)
if badArgs {
return errors.New("Usage: cf wildcard-apps\n\tcf wildcard-apps APP_NAME_WITH_WILDCARD")
}
return nil
}
//Run runs the plugin
//called everytime user executes the command
func (cmd *Wildcard) Run(cliConnection plugin.CliConnection, args []string) {
//fmt.Println(formatters.ToMegabytes("d"))
if args[0] == "wildcard-apps" { //checking is very imp.
cmd.WildcardCommandApps(cliConnection, args)
} else if args[0] == "wildcard-delete" {
cmd.WildcardCommandDelete(cliConnection, args)
}
}
//WildcardCommand creates a new instance of this plugin
//this is the actual implementation
//one method per command
func InitializeCliDependencies() {
errorHandler := func(err error) {
if err != nil {
fmt.Sprintf("Config error: %s", err)
}
}
cc_config := core_config.NewRepositoryFromFilepath(config_helpers.DefaultFilePath(), errorHandler)
T = Init(cc_config, &detection.JibberJabberDetector{})
if os.Getenv("CF_TRACE") != "" {
trace.Logger = trace.NewLogger(os.Getenv("CF_TRACE"))
} else {
trace.Logger = trace.NewLogger(cc_config.Trace())
}
}
func (cmd *Wildcard) getMatchedApps(cliConnection plugin.CliConnection, args []string) []plugin_models.ApplicationSummary {
pattern := args[1]
output, _ := cliConnection.GetApps()
for i := 0; i < (len(output)); i++ {
ok, _ := filepath.Match(pattern, output[i].Name)
if ok {
cmd.matchedApps = append(cmd.matchedApps, output[i])
}
}
return cmd.matchedApps
}
func (cmd *Wildcard) WildcardCommandApps(cliConnection plugin.CliConnection, args []string) {
InitializeCliDependencies()
defer panic.HandlePanics()
cmd.getMatchedApps(cliConnection, args)
cmd.ui = terminal.NewUI(os.Stdin, terminal.NewTeePrinter())
table := terminal.NewTable(cmd.ui, []string{T("name"), T("requested state"), T("instances"), T("memory"), T("disk"), T("urls")})
for _, app := range cmd.matchedApps {
var urls []string
for _, route := range app.Routes {
if route.Host == "" {
urls = append(urls, route.Domain.Name)
}
urls = append(urls, fmt.Sprintf("%s.%s", route.Host, route.Domain.Name))
}
table.Add(
app.Name,
app.State,
strconv.Itoa(app.RunningInstances),
formatters.ByteSize(app.Memory*formatters.MEGABYTE),
formatters.ByteSize(app.DiskQuota*formatters.MEGABYTE),
strings.Join(urls, ", "),
)
}
table.Print()
}
func (cmd *Wildcard) WildcardCommandDelete(cliConnection plugin.CliConnection, args []string) {
cmd.WildcardCommandApps(cliConnection, args)
for _, app := range cmd.matchedApps {
cliConnection.CliCommandWithoutTerminalOutput("delete", app.Name, "-f")
}
}