working commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package accountcmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"mstore/pkg/accntcli"
|
||||
"mstore/pkg/descr"
|
||||
)
|
||||
|
||||
// UpdateAccount
|
||||
type UpdateAccountParams struct {
|
||||
Timeout uint64
|
||||
AccountID string
|
||||
NewUsername string
|
||||
NewPassword string
|
||||
}
|
||||
type UpdateAccountResult struct {
|
||||
File *descr.File `yaml:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||
util.commonAccountParams.Hostname = args[0]
|
||||
util.updateAccountParams.AccountID = args[1]
|
||||
res, err := util.updateAccount(&util.commonAccountParams, &util.updateAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *UpdateAccountParams) (*UpdateAccountResult, error) {
|
||||
var err error
|
||||
res := &UpdateAccountResult{}
|
||||
|
||||
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)
|
||||
if re.MatchString(id) {
|
||||
err = cli.UpdateAccountByID(ctx, ref.Host(), id, params.NewUsername, params.NewPassword)
|
||||
} else {
|
||||
err = cli.UpdateAccountByName(ctx, ref.Host(), params.AccountID, params.NewUsername, params.NewPassword)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user