Skip to content

Commit

Permalink
feat(cmd): add more advanced handling of app urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Anderson committed Jul 1, 2016
1 parent 59d8690 commit 119b2ad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
44 changes: 41 additions & 3 deletions cmd/apps.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/deis/controller-sdk-go/api"
"github.com/deis/controller-sdk-go/apps"
"github.com/deis/controller-sdk-go/config"
"github.com/deis/controller-sdk-go/domains"
"github.com/deis/workflow-cli/pkg/git"
"github.com/deis/workflow-cli/pkg/webbrowser"
"github.com/deis/workflow-cli/settings"
Expand Down Expand Up @@ -104,11 +106,19 @@ func AppInfo(appID string) error {
return err
}

url, err := appURL(s, appID)
if checkAPICompatibility(s.Client, err) != nil {
if err != errNoDomain {
return err
}
url = errNoDomain.Error()
}

fmt.Printf("=== %s Application\n", app.ID)
fmt.Println("updated: ", app.Updated)
fmt.Println("uuid: ", app.UUID)
fmt.Println("created: ", app.Created)
fmt.Println("url: ", app.URL)
fmt.Println("url: ", url)
fmt.Println("owner: ", app.Owner)
fmt.Println("id: ", app.ID)

Expand Down Expand Up @@ -137,12 +147,11 @@ func AppOpen(appID string) error {
return err
}

app, err := apps.Get(s.Client, appID)
u, err := appURL(s, appID)
if checkAPICompatibility(s.Client, err) != nil {
return err
}

u := app.URL
if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
u = "http://" + u
}
Expand Down Expand Up @@ -276,3 +285,32 @@ func AppTransfer(appID, username string) error {

return nil
}

var errNoDomain = errors.New("No domain assigned to app")

// appURL grabs the first domain an app has and returns this.
func appURL(s *settings.Settings, appID string) (string, error) {
domains, _, err := domains.List(s.Client, appID, 1)
if checkAPICompatibility(s.Client, err) != nil {
return "", err
}

if len(domains) == 0 {
return "", errNoDomain
}

return expandURL(s.Client.ControllerURL.Host, domains[0].Domain), nil
}

// expandURL expands an app url if neccessary.
func expandURL(host, u string) string {
if strings.Contains(u, ".") {
// If domain is a full url.
return u
}

// If domain is a subdomain, look up the controller url and replace the subdomain.
parts := strings.Split(host, ".")
parts[0] = u
return strings.Join(parts, ".")
}
26 changes: 26 additions & 0 deletions cmd/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@ func TestPrintLogLinesBadLine(t *testing.T) {
t.Fatal(err)
}
}

type expandURLCases struct {
Input string
Expected string
}

func TestExpandUrl(t *testing.T) {
checks := []expandURLCases{
expandURLCases{
Input: "test.com",
Expected: "test.com",
},
expandURLCases{
Input: "test",
Expected: "test.foo.com",
},
}

for _, check := range checks {
out := expandURL("deis.foo.com", check.Input)

if out != check.Expected {
t.Errorf("Expected %s, Got %s", check.Expected, out)
}
}
}

0 comments on commit 119b2ad

Please sign in to comment.