working commit

This commit is contained in:
Олег Бородин
2026-05-26 09:58:10 +02:00
parent 315f69b123
commit a53197e432
17 changed files with 1057 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package accountcmd
import (
"context"
"regexp"
"strings"
"time"
"github.com/spf13/cobra"
"mbase/pkg/accntcli"
"mbase/pkg/descr"
)
// ListGrants
type ListGrantsParams struct {
Detail bool
AccountID string
}
type ListGrantsResult struct {
Grants []descr.Grant `json:"grants,omitempty"`
Rights map[string]string `json:"rights,omitempty"`
}
func (util *GrantUtil) ListGrants(cmd *cobra.Command, args []string) {
util.commonGrantParams.Hostname = args[0]
util.listGrantsParams.AccountID = args[1]
res, err := util.listGrants(&util.commonGrantParams, &util.listGrantsParams)
printResponse(res, err)
}
func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsParams) (*ListGrantsResult, error) {
var err error
res := &ListGrantsResult{}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
ref, err := accntcli.ParseHostinfo(common.Hostname)
if err != nil {
return res, err
}
ref.SetUserinfo(common.Username, common.Password)
mw := accntcli.NewBasicAuthMiddleware(ref.Userinfo())
cli := accntcli.NewClient(nil, mw)
grants := make([]descr.Grant, 0)
re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
grants, err = cli.ListGrantsByAccountID(ctx, ref.Host(), id)
} else {
grants, err = cli.ListGrantsByUsername(ctx, ref.Host(), params.AccountID)
}
if err != nil {
return res, err
}
if params.Detail {
res.Grants = grants
} else {
res.Rights = make(map[string]string, 0)
for _, item := range grants {
res.Rights[item.ID] = item.Right
}
}
return res, err
}