Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Validation Domain for login command #271

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
)

func FormatCreatedTime(timestamp string) (string, error) {
Expand Down Expand Up @@ -39,6 +40,42 @@ func FormatUrl(url string) string {
return url
}

// ValidateDomain checks if the given domain string is non-empty, properly formatted, and a valid domain.
func FormatToValidDomain(input string) (string, error) {
parts := strings.Split(input, ".")
if len(parts) != 3 {
return "", fmt.Errorf("invalid server address input, must be in the format: subdomain.example.tld")
}

for _, part := range parts {
if !isValidLabel(part) {
return "", fmt.Errorf("invalid domain label: %s", part)
}
}

return strings.Join(parts, "."), nil
}

// isValidLabel checks if a domain label is valid according to DNS rules
func isValidLabel(label string) bool {
if len(label) == 0 || len(label) > 63 {
return false
}
trimedLabel := strings.TrimSpace(label)
if len(trimedLabel) != len(label) {
return false
}
for _, ch := range label {
if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != '-' {
return false
}
}
if label[0] == '-' || label[len(label)-1] == '-' {
return false
}
return true
}

func FormatSize(size int64) string {
mbSize := float64(size) / (1024 * 1024)
return fmt.Sprintf("%.2fMiB", mbSize)
Expand Down
8 changes: 4 additions & 4 deletions pkg/views/login/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package login
import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -32,10 +31,11 @@ func CreateView(loginView *LoginView) {
if strings.TrimSpace(str) == "" {
return errors.New("server cannot be empty or only spaces")
}
formattedUrl := utils.FormatUrl(str)
if _, err := url.ParseRequestURI(formattedUrl); err != nil {
return errors.New("please enter the correct server format")
_, err := utils.FormatToValidDomain(str)
if err != nil {
return err
}

return nil
}),
huh.NewInput().
Expand Down