52ac2c2b97
improve function documentation
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package proxmoxaas_common_lib
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
// Converts input groupname object to string representation.
|
|
// Uses the format gid-realm.
|
|
func (g Groupname) ToString() string {
|
|
return fmt.Sprintf("%s-%s", g.GroupID, g.Realm)
|
|
}
|
|
|
|
// Converts input username object to string representation.
|
|
// Uses the format uid@realm.
|
|
func (u Username) ToString() string {
|
|
return fmt.Sprintf("%s-%s", u.UserID, u.Realm)
|
|
}
|
|
|
|
// Parses input groupname string to gid and realm.
|
|
// Assumes the format gid-realm or gid if realm is implicitly pve.
|
|
// Returns an error if the groupname format was not correct.
|
|
func ParseGroupname(groupname string) (Groupname, error) {
|
|
g := Groupname{}
|
|
// <groupid>-<realm>
|
|
m1 := regexp.MustCompilePOSIX("([[:alnum:]]+)-([[:alnum:]]+)")
|
|
// <groupid> (implied pve realm)
|
|
m2 := regexp.MustCompilePOSIX("([[:alnum:]]+)")
|
|
x1 := m1.FindStringSubmatch(groupname)
|
|
x2 := m2.FindStringSubmatch(groupname)
|
|
if len(x1) == 3 {
|
|
g.GroupID = x1[1]
|
|
g.Realm = x1[2]
|
|
return g, nil
|
|
|
|
} else if len(x2) == 2 {
|
|
g.GroupID = groupname
|
|
g.Realm = "pve"
|
|
return g, nil
|
|
} else {
|
|
return g, fmt.Errorf("groupid did not follow the format <groupid> or <groupid>-<realm>")
|
|
}
|
|
}
|
|
|
|
// Parses input username string to uid and realm.
|
|
// Assumes the format uid@realm.
|
|
// Returns an error if the username format was not correct.
|
|
func ParseUsername(username string) (Username, error) {
|
|
u := Username{}
|
|
m := regexp.MustCompilePOSIX("([[:alnum:]]+)@([[:alnum:]]+)")
|
|
x := m.FindStringSubmatch(username)
|
|
if len(x) == 3 {
|
|
u.UserID = x[1]
|
|
u.Realm = x[2]
|
|
return u, nil
|
|
} else {
|
|
return u, fmt.Errorf("userid did not follow the format <userid>@<realm>")
|
|
}
|
|
}
|