Dynamic hostname (#454)

* Display dynamic hostname for login

* Document change
This commit is contained in:
Sung Won Cho 2020-04-25 16:56:33 +10:00 committed by GitHub
commit ebe360d7ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 1 deletions

View file

@ -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 {

View file

@ -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")
})
}
}