working commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
package accountcmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"mbase/pkg/accntcli"
|
||||
)
|
||||
|
||||
// CreateGrant
|
||||
type CreateGrantParams struct {
|
||||
AccountID string
|
||||
Right string
|
||||
Pattern string
|
||||
}
|
||||
type CreateGrantResult struct {
|
||||
GrantID string `json:"grantId"`
|
||||
}
|
||||
|
||||
func (util *GrantUtil) CreateGrant(cmd *cobra.Command, args []string) {
|
||||
util.commonGrantParams.Hostname = args[0]
|
||||
util.createGrantParams.AccountID = args[1]
|
||||
util.createGrantParams.Right = args[2]
|
||||
util.createGrantParams.Pattern = args[3]
|
||||
res, err := util.createGrant(&util.commonGrantParams, &util.createGrantParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGrantParams) (*CreateGrantResult, error) {
|
||||
var err error
|
||||
res := &CreateGrantResult{}
|
||||
|
||||
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)
|
||||
|
||||
re := regexp.MustCompile(uuidRegex)
|
||||
id := strings.ToLower(params.AccountID)
|
||||
var opres string
|
||||
if re.MatchString(id) {
|
||||
opres, err = cli.CreateGrantByAccountID(ctx, ref.Host(), id, params.Right, params.Pattern)
|
||||
} else {
|
||||
opres, err = cli.CreateGrantByUsername(ctx, ref.Host(), params.AccountID, params.Right, params.Pattern)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.GrantID = opres
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user