forked from pkallberg/cx
/
open.go
82 lines (68 loc) · 1.77 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
package main
import (
"fmt"
"os"
"github.com/cloud66/cli"
)
var cmdOpen = &Command{
Run: runOpen,
Build: buildBasicCommand,
Name: "open",
NeedsStack: true,
Short: "opens the web browser to visit the app served by the stack",
Long: `This opens the client web browser to visit the app servers by the stack. This could be the web page
specifically served by one server or the load balancer.
If no server is specified, the command opens the page served by the stack load balancer or first web server.
Alternatively you can specify the name or IP of the server. Partial names are accepted and are case insensitive.
Examples:
$ cx open
$ cx open lion
$ cx open -s mystack
$ cx open -s mystack lion
`,
}
func runOpen(c *cli.Context) {
stack := mustStack(c)
if len(c.Args()) > 1 {
//cmd.printUsage()
os.Exit(2)
}
var toOpen string
// are we connecting to a server?
if len(c.Args()) == 1 {
// get the server
serverName := c.Args()[0]
servers, err := client.Servers(stack.Uid)
if err != nil {
printFatal(err.Error())
}
server, err := findServer(servers, serverName)
if err != nil {
printFatal(err.Error())
}
if server == nil {
printFatal("Server '" + serverName + "' not found")
}
fmt.Printf("Server: %s\n", server.Name)
toOpen = "http://" + server.DnsRecord
} else {
// is the stack load balanced?
if stack.HasLoadBalancer {
toOpen = "http://" + stack.Fqdn
} else {
// use the first web server
servers, err := client.Servers(stack.Uid)
if err != nil {
printFatal(err.Error())
}
fmt.Printf("Server: %s\n", servers[0].Name)
toOpen = "http://" + servers[0].DnsRecord
}
}
// open server's fqdn
fmt.Printf("Opening %s\n", toOpen)
err := openURL(toOpen)
if err != nil {
printFatal(err.Error())
}
}