-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathopen.go
More file actions
144 lines (121 loc) · 4.23 KB
/
Copy pathopen.go
File metadata and controls
144 lines (121 loc) · 4.23 KB
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"github.com/Jeffail/gabs/v2"
)
// openResource opens the Scalr dashboard URL for the given resource type and ID/name.
//
// URL patterns:
// account: https://{hostname}/v2/a/{account-id}/
// environment: https://{hostname}/v2/e/{env-id}/workspaces/
// workspace: https://{hostname}/v2/e/{env-id}/workspaces/{workspace-id}/
// run: https://{hostname}/v2/e/{env-id}/workspaces/{workspace-id}/runs/{run-id}/
func openResource(resourceType string, identifier string) {
var dashURL string
switch resourceType {
case "account", "acc":
accountID := ScalrAccount
if identifier != "" {
accountID = resolveNameToID("account", identifier)
}
if accountID == "" {
fmt.Fprintln(os.Stderr, "Error: No account specified. Use -account env var or pass an account ID.")
os.Exit(ExitError)
}
dashURL = fmt.Sprintf("https://%s/v2/a/%s/", ScalrHostname, accountID)
case "environment", "env":
if identifier == "" {
fmt.Fprintln(os.Stderr, "Error: Environment name or ID required.")
fmt.Fprintln(os.Stderr, "Usage: scalr open environment <name-or-id>")
os.Exit(ExitError)
}
envID := resolveNameToID("environment", identifier)
dashURL = fmt.Sprintf("https://%s/v2/e/%s/workspaces/", ScalrHostname, envID)
case "workspace", "ws":
if identifier == "" {
fmt.Fprintln(os.Stderr, "Error: Workspace name or ID required.")
fmt.Fprintln(os.Stderr, "Usage: scalr open workspace <name-or-id>")
os.Exit(ExitError)
}
wsID := resolveNameToID("workspace", identifier)
envID := fetchRelationshipID("/workspaces/"+wsID, "environment")
dashURL = fmt.Sprintf("https://%s/v2/e/%s/workspaces/%s/", ScalrHostname, envID, wsID)
case "run":
if identifier == "" {
fmt.Fprintln(os.Stderr, "Error: Run ID required.")
fmt.Fprintln(os.Stderr, "Usage: scalr open run <run-id>")
os.Exit(ExitError)
}
runID := identifier
wsID := fetchRelationshipID("/runs/"+runID, "workspace")
envID := fetchRelationshipID("/workspaces/"+wsID, "environment")
dashURL = fmt.Sprintf("https://%s/v2/e/%s/workspaces/%s/runs/%s/", ScalrHostname, envID, wsID, runID)
default:
fmt.Fprintf(os.Stderr, "Error: Unknown resource type '%s'.\n", resourceType)
fmt.Fprintln(os.Stderr, "Supported: account, environment, workspace, run")
os.Exit(ExitError)
}
fmt.Fprintln(os.Stderr, dashURL)
openBrowser(dashURL)
}
// fetchRelationshipID fetches a resource by API path and extracts a relationship ID.
// For example, fetchRelationshipID("/workspaces/ws-xxx", "environment") returns "env-yyy".
func fetchRelationshipID(apiPath string, relationshipName string) string {
apiURL := "https://" + ScalrHostname + BasePath + apiPath
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(ExitError)
}
setScalrHeaders(req)
res, err := scalrHTTPClient.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Request failed: %s\n", err)
os.Exit(ExitTransientError)
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(ExitError)
}
if res.StatusCode >= 300 {
showError(resBody, res.StatusCode)
}
response, err := gabs.ParseJSON(resBody)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Invalid API response\n")
os.Exit(ExitError)
}
// Extract relationship ID: data.relationships.{name}.data.id
relID, ok := response.Path("data.relationships." + relationshipName + ".data.id").Data().(string)
if !ok || relID == "" {
fmt.Fprintf(os.Stderr, "Error: Could not find %s relationship for %s\n", relationshipName, apiPath)
os.Exit(ExitError)
}
return relID
}
// openBrowser opens a URL in the user's default browser.
func openBrowser(url string) {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", url)
case "linux":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
default:
fmt.Fprintln(os.Stderr, "Cannot open browser on this platform. URL printed above.")
return
}
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Error opening browser: %s\n", err)
fmt.Fprintln(os.Stderr, "Open the URL above manually.")
}
}