From ebe360d7ff84ad16ddf4f48261f5f9bee4b74450 Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Sat, 25 Apr 2020 16:56:33 +1000 Subject: [PATCH] Dynamic hostname (#454) * Display dynamic hostname for login * Document change --- CHANGELOG.md | 10 ++++++ pkg/cli/cmd/login/login.go | 42 ++++++++++++++++++++++++- pkg/cli/cmd/login/login_test.go | 56 +++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 pkg/cli/cmd/login/login_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a97e671..0399d2c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,6 +164,16 @@ The following log documents the history of the server project. The following log documentes the history of the CLI project +### Unreleased + +#### Fixed + +- Fix upgrade URL (#453) + +#### Changed + +- Display hostname of the self-hosted instance while logging in + ### 0.11.0 - 2020-02-05 #### Added diff --git a/pkg/cli/cmd/login/login.go b/pkg/cli/cmd/login/login.go index 85e2838c..bc68657a 100644 --- a/pkg/cli/cmd/login/login.go +++ b/pkg/cli/cmd/login/login.go @@ -19,6 +19,8 @@ package login import ( + "fmt" + "net/url" "strconv" "github.com/dnote/dnote/pkg/cli/client" @@ -110,9 +112,47 @@ func getPassword() (string, error) { return password, nil } +func getBaseURL(rawURL string) (string, error) { + u, err := url.Parse(rawURL) + if err != nil { + return "", errors.Wrap(err, "parsing url") + } + + if u.Scheme == "" || u.Host == "" { + return "", nil + } + + return fmt.Sprintf("%s://%s", u.Scheme, u.Host), nil +} + +func getServerDisplayURL(ctx context.DnoteCtx) string { + if ctx.APIEndpoint == "https://api.getdnote.com" { + return "https://www.getdnote.com" + } + + baseURL, err := getBaseURL(ctx.APIEndpoint) + if err != nil { + return "" + } + + return baseURL +} + +func getGreeting(ctx context.DnoteCtx) string { + base := "Welcome to Dnote Pro" + + serverURL := getServerDisplayURL(ctx) + if serverURL == "" { + return fmt.Sprintf("%s\n", base) + } + + return fmt.Sprintf("%s (%s)\n", base, serverURL) +} + func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { - log.Plain("Welcome to Dnote Pro (https://www.getdnote.com).\n") + greeting := getGreeting(ctx) + log.Plain(greeting) email, err := getUsername() if err != nil { diff --git a/pkg/cli/cmd/login/login_test.go b/pkg/cli/cmd/login/login_test.go new file mode 100644 index 00000000..d48202ce --- /dev/null +++ b/pkg/cli/cmd/login/login_test.go @@ -0,0 +1,56 @@ +package login + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" +) + +func TestGetServerDisplayURL(t *testing.T) { + testCases := []struct { + apiEndpoint string + expected string + }{ + { + apiEndpoint: "https://api.getdnote.com", + expected: "https://www.getdnote.com", + }, + { + apiEndpoint: "https://dnote.mydomain.com/api", + expected: "https://dnote.mydomain.com", + }, + { + apiEndpoint: "https://mysubdomain.mydomain.com/dnote/api", + expected: "https://mysubdomain.mydomain.com", + }, + { + apiEndpoint: "https://dnote.mysubdomain.mydomain.com/api", + expected: "https://dnote.mysubdomain.mydomain.com", + }, + { + apiEndpoint: "some-string", + expected: "", + }, + { + apiEndpoint: "", + expected: "", + }, + { + apiEndpoint: "https://", + expected: "", + }, + { + apiEndpoint: "https://abc", + expected: "https://abc", + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("for input %s", tc.apiEndpoint), func(t *testing.T) { + got := getServerDisplayURL(context.DnoteCtx{APIEndpoint: tc.apiEndpoint}) + assert.Equal(t, got, tc.expected, "result mismatch") + }) + } +}