Skip to content

Commit

Permalink
bug(cmd): add more advanced handling of app urls (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson authored Jul 25, 2016
1 parent 2b35225 commit 75c43e7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
50 changes: 46 additions & 4 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 +105,20 @@ func AppInfo(appID string) error {
return err
}

url, err := appURL(s, appID)
if err != nil {
return err
}

if url == "" {
url = fmt.Sprintf(noDomainAssignedMsg, appID)
}

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,15 @@ func AppOpen(appID string) error {
return err
}

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

u := app.URL
if u == "" {
return fmt.Errorf(noDomainAssignedMsg, appID)
}

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

return nil
}

const noDomainAssignedMsg = "No domain assigned to %s"

// 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 "", nil
}

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 75c43e7

Please sign in to comment.