From 119b2ad43ba08775c18914fd5a95c866ce639003 Mon Sep 17 00:00:00 2001 From: Joshua Anderson Date: Fri, 1 Jul 2016 15:14:09 -0700 Subject: [PATCH] feat(cmd): add more advanced handling of app urls --- cmd/apps.go | 44 +++++++++++++++++++++++++++++++++++++++++--- cmd/apps_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/cmd/apps.go b/cmd/apps.go index 23dd6ea1..44d2ef76 100644 --- a/cmd/apps.go +++ b/cmd/apps.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "os" "strings" @@ -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" @@ -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) @@ -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 } @@ -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, ".") +} diff --git a/cmd/apps_test.go b/cmd/apps_test.go index 57187895..47f13777 100644 --- a/cmd/apps_test.go +++ b/cmd/apps_test.go @@ -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) + } + } +}