mirror of
https://github.com/dnote/dnote
synced 2026-03-14 22:45:50 +01:00
Dynamic hostname (#454)
* Display dynamic hostname for login * Document change
This commit is contained in:
parent
4b700f5e66
commit
ebe360d7ff
3 changed files with 107 additions and 1 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
56
pkg/cli/cmd/login/login_test.go
Normal file
56
pkg/cli/cmd/login/login_test.go
Normal 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")
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue