Skip to content

Commit 75c43e7

Browse files
bug(cmd): add more advanced handling of app urls (#110)
1 parent 2b35225 commit 75c43e7

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

cmd/apps.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/deis/controller-sdk-go/api"
1212
"github.com/deis/controller-sdk-go/apps"
1313
"github.com/deis/controller-sdk-go/config"
14+
"github.com/deis/controller-sdk-go/domains"
1415
"github.com/deis/workflow-cli/pkg/git"
1516
"github.com/deis/workflow-cli/pkg/webbrowser"
1617
"github.com/deis/workflow-cli/settings"
@@ -104,11 +105,20 @@ func AppInfo(appID string) error {
104105
return err
105106
}
106107

108+
url, err := appURL(s, appID)
109+
if err != nil {
110+
return err
111+
}
112+
113+
if url == "" {
114+
url = fmt.Sprintf(noDomainAssignedMsg, appID)
115+
}
116+
107117
fmt.Printf("=== %s Application\n", app.ID)
108118
fmt.Println("updated: ", app.Updated)
109119
fmt.Println("uuid: ", app.UUID)
110120
fmt.Println("created: ", app.Created)
111-
fmt.Println("url: ", app.URL)
121+
fmt.Println("url: ", url)
112122
fmt.Println("owner: ", app.Owner)
113123
fmt.Println("id: ", app.ID)
114124

@@ -137,12 +147,15 @@ func AppOpen(appID string) error {
137147
return err
138148
}
139149

140-
app, err := apps.Get(s.Client, appID)
141-
if checkAPICompatibility(s.Client, err) != nil {
150+
u, err := appURL(s, appID)
151+
if err != nil {
142152
return err
143153
}
144154

145-
u := app.URL
155+
if u == "" {
156+
return fmt.Errorf(noDomainAssignedMsg, appID)
157+
}
158+
146159
if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
147160
u = "http://" + u
148161
}
@@ -276,3 +289,32 @@ func AppTransfer(appID, username string) error {
276289

277290
return nil
278291
}
292+
293+
const noDomainAssignedMsg = "No domain assigned to %s"
294+
295+
// appURL grabs the first domain an app has and returns this.
296+
func appURL(s *settings.Settings, appID string) (string, error) {
297+
domains, _, err := domains.List(s.Client, appID, 1)
298+
if checkAPICompatibility(s.Client, err) != nil {
299+
return "", err
300+
}
301+
302+
if len(domains) == 0 {
303+
return "", nil
304+
}
305+
306+
return expandURL(s.Client.ControllerURL.Host, domains[0].Domain), nil
307+
}
308+
309+
// expandURL expands an app url if neccessary.
310+
func expandURL(host, u string) string {
311+
if strings.Contains(u, ".") {
312+
// If domain is a full url.
313+
return u
314+
}
315+
316+
// If domain is a subdomain, look up the controller url and replace the subdomain.
317+
parts := strings.Split(host, ".")
318+
parts[0] = u
319+
return strings.Join(parts, ".")
320+
}

cmd/apps_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,29 @@ func TestPrintLogLinesBadLine(t *testing.T) {
1616
t.Fatal(err)
1717
}
1818
}
19+
20+
type expandURLCases struct {
21+
Input string
22+
Expected string
23+
}
24+
25+
func TestExpandUrl(t *testing.T) {
26+
checks := []expandURLCases{
27+
expandURLCases{
28+
Input: "test.com",
29+
Expected: "test.com",
30+
},
31+
expandURLCases{
32+
Input: "test",
33+
Expected: "test.foo.com",
34+
},
35+
}
36+
37+
for _, check := range checks {
38+
out := expandURL("deis.foo.com", check.Input)
39+
40+
if out != check.Expected {
41+
t.Errorf("Expected %s, Got %s", check.Expected, out)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)