-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopen.go
188 lines (168 loc) · 4.57 KB
/
open.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"code.cloudfoundry.org/cli/plugin"
"github.com/skratchdot/open-golang/open"
)
type serviceInstanceResponse struct {
Entity serviceInstance `json:"entity"`
}
type serviceInstance struct {
DashboardURL string `json:"dashboard_url"`
}
var Version = ""
func main() {
if len(os.Args) > 1 {
if os.Args[1] == "-v" || os.Args[1] == "--version" {
if Version == "" {
fmt.Printf("cf-plugin-open (development)\n")
} else {
fmt.Printf("cf-plugin-open v%s\n", Version)
}
os.Exit(0)
}
}
plugin.Start(&OpenPlugin{})
}
// OpenPlugin empty struct for plugin
type OpenPlugin struct{}
// Run of seeder plugin
func (plugin OpenPlugin) Run(cliConnection plugin.CliConnection, args []string) {
err := checkArgs(cliConnection, args)
if err != nil {
os.Exit(1)
}
if args[0] == "open" {
plugin.runAppOpen(cliConnection, args)
} else if args[0] == "service-open" {
plugin.runServiceOpen(cliConnection, args)
}
}
// GetMetadata of plugin
func (OpenPlugin) GetMetadata() plugin.PluginMetadata {
version := plugin.VersionType{Major: 0, Minor: 0, Build: 0}
versionRE := regexp.MustCompile("([0-9]+).([0-9]+).([0-9]+)")
versionParts := versionRE.FindStringSubmatch(Version)
if len(versionParts) == 4 {
var part int64
part, _ = strconv.ParseInt(versionParts[1], 10, 32)
version.Major = int(part)
part, _ = strconv.ParseInt(versionParts[2], 10, 32)
version.Minor = int(part)
part, _ = strconv.ParseInt(versionParts[3], 10, 32)
version.Build = int(part)
}
return plugin.PluginMetadata{
Name: "open",
Version: version,
Commands: []plugin.Command{
{
Name: "open",
HelpText: "open app url in browser",
UsageDetails: plugin.Usage{
Usage: "open <appname>",
},
},
{
Name: "service-open",
HelpText: "open service instance dashboard in browser",
UsageDetails: plugin.Usage{
Usage: "service-open <servicename>",
},
},
},
}
}
func (plugin OpenPlugin) runAppOpen(cliConnection plugin.CliConnection, args []string) {
output, err := cliConnection.CliCommandWithoutTerminalOutput("app", args[1])
if err != nil {
fmt.Fprintln(os.Stdout, "error: app does not exist")
os.Exit(1)
}
url, err := getUrlFromOutput(output)
if err != nil {
fmt.Fprintln(os.Stdout, "error: ", err)
os.Exit(1)
}
open.Run(multiRoutesMenu(os.Stdin, url))
}
func getUrlFromOutput(output []string) ([]string, error) {
urls := []string{}
for _, line := range output {
splitLine := strings.Split(strings.TrimSpace(line), " ")
if splitLine[0] == "urls:" {
if len(splitLine) > 1 {
for p := 1; p < len(splitLine); p++ {
url := "https://" + strings.Trim(splitLine[p], ",")
url = strings.TrimSpace(url)
urls = append(urls, url)
}
} else if len(splitLine) == 1 {
return []string{""}, errors.New("App has no route")
}
}
}
return urls, nil
}
func multiRoutesMenu(input io.Reader, urls []string) string {
if len(urls) == 1 {
return urls[0]
} else {
var choice int
fmt.Println("Multiple routes detected. Please choose one: ")
for u := 0; u < len(urls); u++ {
fmt.Printf("%d - %s\n", u+1, urls[u])
}
fmt.Print("Enter route to open: ")
fmt.Fscanf(input, "%d", &choice)
for !(choice >= 1 && choice <= len(urls)) {
fmt.Printf("Please enter valid number(1 to %d): ", len(urls))
fmt.Fscanf(input, "%d", &choice)
}
return urls[choice-1]
}
}
func (plugin OpenPlugin) runServiceOpen(cliConnection plugin.CliConnection, args []string) {
output, err := cliConnection.CliCommandWithoutTerminalOutput("service", args[1], "--guid")
if err != nil {
fmt.Fprintln(os.Stdout, "error: service does not exist")
os.Exit(1)
}
serviceInstanceGUID := strings.TrimSpace(output[0])
output, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v2/service_instances/%s", serviceInstanceGUID))
if err != nil {
fmt.Fprintln(os.Stdout, "error: service does not exist")
os.Exit(1)
}
jsonStr := ""
for _, line := range output {
jsonStr += line + "\n"
}
response := serviceInstanceResponse{}
json.Unmarshal([]byte(jsonStr), &response)
url := response.Entity.DashboardURL
if url == "" {
fmt.Println("No dashboard available")
} else {
open.Run(url)
}
}
func checkArgs(cliConnection plugin.CliConnection, args []string) error {
if len(args) < 2 {
if args[0] == "open" {
cliConnection.CliCommand(args[0], "-h")
return errors.New("Appname is needed")
} else if args[0] == "service-open" {
cliConnection.CliCommand(args[0], "-h")
return errors.New("Appname is needed")
}
}
return nil
}