45 lines
949 B
Go
45 lines
949 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.maurice.fr/thomas/mailout/pkg/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var (
|
|
uuidRegex = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
|
)
|
|
|
|
func splitUser(user string) (string, string, error) {
|
|
splt := strings.Split(user, "@")
|
|
if len(splt) != 2 {
|
|
return "", "", fmt.Errorf("invalid username: %s", user)
|
|
}
|
|
|
|
if len(splt[0]) == 0 || len(splt[1]) == 0 {
|
|
return "", "", fmt.Errorf("invalid username: %s", user)
|
|
}
|
|
|
|
return splt[0], splt[1], nil
|
|
}
|
|
|
|
func buildUserQuery(arg string) (*models.User, error) {
|
|
var userQuery models.User
|
|
if uuidRegex.Match([]byte(arg)) {
|
|
id, err := uuid.Parse(arg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
userQuery.ID = id
|
|
} else {
|
|
username, domain, err := splitUser(arg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
userQuery = models.User{Domain: domain, Username: username}
|
|
}
|
|
return &userQuery, nil
|
|
}
|