cli/api/queries_user.go
beforetech 563c2f9e18 refactor: use a more straightforward return value
Signed-off-by: beforetech <mail@before.tech>
2025-02-23 22:59:33 +08:00

45 lines
1 KiB
Go

package api
type Organization struct {
Login string
}
func CurrentLoginName(client *Client, hostname string) (string, error) {
var query struct {
Viewer struct {
Login string
}
}
err := client.Query(hostname, "UserCurrent", &query, nil)
return query.Viewer.Login, err
}
func CurrentLoginNameAndOrgs(client *Client, hostname string) (string, []string, error) {
var query struct {
Viewer struct {
Login string
Organizations struct {
Nodes []Organization
} `graphql:"organizations(first: 100)"`
}
}
err := client.Query(hostname, "UserCurrent", &query, nil)
if err != nil {
return "", nil, err
}
orgNames := []string{}
for _, org := range query.Viewer.Organizations.Nodes {
orgNames = append(orgNames, org.Login)
}
return query.Viewer.Login, orgNames, nil
}
func CurrentUserID(client *Client, hostname string) (string, error) {
var query struct {
Viewer struct {
ID string
}
}
err := client.Query(hostname, "UserCurrent", &query, nil)
return query.Viewer.ID, err
}