working commit

This commit is contained in:
2026-02-14 11:36:02 +02:00
parent 801606b956
commit 7790b70372
2 changed files with 113 additions and 17 deletions
+73 -10
View File
@@ -11,6 +11,8 @@ package main
import ( import (
"context" "context"
"regexp"
"strings"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -19,6 +21,11 @@ import (
"mstore/pkg/client" "mstore/pkg/client"
) )
const (
uuidRegex = `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
defaultHostname = "localhost:1025"
)
func (util *AccountUtil) CreateAccountCmds() *cobra.Command { func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
var subCmd = &cobra.Command{ var subCmd = &cobra.Command{
Use: "account", Use: "account",
@@ -33,11 +40,13 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
} }
createAccountCmd.Flags().StringVarP(&util.createAccountParams.Username, "username", "u", "", "Username") createAccountCmd.Flags().StringVarP(&util.createAccountParams.Username, "username", "u", "", "Username")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.Password, "password", "p", "", "Password") createAccountCmd.Flags().StringVarP(&util.createAccountParams.Password, "password", "p", "", "Password")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.Hostname, "host", "x", "", "Hostname") createAccountCmd.Flags().StringVarP(&util.createAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
createAccountCmd.Flags().Uint64VarP(&util.createAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") createAccountCmd.Flags().Uint64VarP(&util.createAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username") createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password") createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password")
createAccountCmd.MarkFlagRequired("host")
createAccountCmd.MarkFlagsRequiredTogether("newuser", "newpass")
subCmd.AddCommand(createAccountCmd) subCmd.AddCommand(createAccountCmd)
@@ -47,11 +56,12 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
Short: "Get account info", Short: "Get account info",
Run: util.GetAccount, Run: util.GetAccount,
} }
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Hostname, "host", "x", "", "Hostname") getAccountCmd.Flags().StringVarP(&util.getAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Username, "username", "u", "", "Username") getAccountCmd.Flags().StringVarP(&util.getAccountParams.Username, "username", "u", "", "Username")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Password, "password", "p", "", "Password") getAccountCmd.Flags().StringVarP(&util.getAccountParams.Password, "password", "p", "", "Password")
getAccountCmd.Flags().Uint64VarP(&util.getAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") getAccountCmd.Flags().Uint64VarP(&util.getAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID") getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID")
getAccountCmd.MarkFlagRequired("host")
subCmd.AddCommand(getAccountCmd) subCmd.AddCommand(getAccountCmd)
@@ -65,6 +75,7 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Password, "password", "p", "", "Password") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Password, "password", "p", "", "Password")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Hostname, "host", "x", "", "File path") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Hostname, "host", "x", "", "File path")
updateAccountCmd.Flags().Uint64VarP(&util.updateAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") updateAccountCmd.Flags().Uint64VarP(&util.updateAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
updateAccountCmd.MarkFlagRequired("host")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newuser", "U", "", "New username") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newuser", "U", "", "New username")
@@ -80,8 +91,9 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
} }
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Username, "username", "u", "", "Username") deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Username, "username", "u", "", "Username")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Password, "password", "p", "", "Password") deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Password, "password", "p", "", "Password")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Hostname, "host", "x", "", "Hostname") deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
deleteAccountCmd.Flags().Uint64VarP(&util.deleteAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") deleteAccountCmd.Flags().Uint64VarP(&util.deleteAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
deleteAccountCmd.MarkFlagRequired("host")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID") deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID")
@@ -104,8 +116,11 @@ func (util *AccountUtil) CreateAccountsCmds() *cobra.Command {
} }
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Username, "username", "u", "", "Username") listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Username, "username", "u", "", "Username")
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Password, "password", "p", "", "Password") listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Password, "password", "p", "", "Password")
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Hostname, "host", "x", "", "Hostname") listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Hostname, "host", "x", defaultHostname, "Hostname")
listAccountsCmd.Flags().Uint64VarP(&util.listAccountsParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") listAccountsCmd.Flags().Uint64VarP(&util.listAccountsParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
listAccountsCmd.Flags().BoolVarP(&util.listAccountsParams.Detail, "detail", "d", false, "Show detail information")
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Regex, "regex", "r", "", "Output regexp for usernames")
listAccountsCmd.MarkFlagRequired("host")
subCmd.AddCommand(listAccountsCmd) subCmd.AddCommand(listAccountsCmd)
return subCmd return subCmd
@@ -182,7 +197,13 @@ func (util *AccountUtil) updateAccount(params *UpdateAccountParams) (*UpdateAcco
} }
timeout := time.Duration(params.Timeout) * time.Second timeout := time.Duration(params.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, _ := context.WithTimeout(context.Background(), timeout)
err = client.NewClient().UpdateAccountByID(ctx, params.Hostname, params.AccountID, params.NewUsername, params.NewPassword) re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
err = client.NewClient().UpdateAccountByID(ctx, params.Hostname, id, params.NewUsername, params.NewPassword)
} else {
err = client.NewClient().UpdateAccountByName(ctx, params.Hostname, params.AccountID, params.NewUsername, params.NewPassword)
}
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -203,7 +224,9 @@ func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
printResponse(res, err) printResponse(res, err)
} }
type GetAccountResult struct{} type GetAccountResult struct {
Account *descr.AccountShort `json:"account,omitempty"`
}
func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult, error) { func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult, error) {
var err error var err error
@@ -214,10 +237,19 @@ func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult
} }
timeout := time.Duration(params.Timeout) * time.Second timeout := time.Duration(params.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, _ := context.WithTimeout(context.Background(), timeout)
_, err = client.NewClient().GetAccountByID(ctx, params.Hostname, params.AccountID) opRes := &descr.AccountShort{}
re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
opRes, err = client.NewClient().GetAccountByID(ctx, params.Hostname, id)
} else {
opRes, err = client.NewClient().GetAccountByName(ctx, params.Hostname, params.AccountID)
}
if err != nil { if err != nil {
return res, err return res, err
} }
res.Account = opRes
return res, err return res, err
} }
@@ -245,7 +277,13 @@ func (util *AccountUtil) deleteAccount(params *DeleteAccountParams) (*DeleteAcco
} }
timeout := time.Duration(params.Timeout) * time.Second timeout := time.Duration(params.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, _ := context.WithTimeout(context.Background(), timeout)
err = client.NewClient().DeleteAccountByID(ctx, params.Hostname, params.AccountID) re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
err = client.NewClient().DeleteAccountByID(ctx, params.Hostname, id)
} else {
err = client.NewClient().DeleteAccountByName(ctx, params.Hostname, params.AccountID)
}
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -258,10 +296,13 @@ type ListAccountsParams struct {
Username string Username string
Password string Password string
Timeout uint64 Timeout uint64
Detail bool
Regex string
} }
type ListAccountsResult struct { type ListAccountsResult struct {
Accounts []descr.AccountShort `json:"accounts"` Accounts []descr.AccountShort `json:"accounts,omitempty"`
Usernames []string `json:"names,omitempty"`
} }
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) { func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
@@ -275,12 +316,34 @@ func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccounts
if err != nil { if err != nil {
return res, err return res, err
} }
outRe, err := regexp.Compile(params.Regex)
if err != nil {
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second timeout := time.Duration(params.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, _ := context.WithTimeout(context.Background(), timeout)
accounts, err := client.NewClient().ListAccounts(ctx, params.Hostname) accounts, err := client.NewClient().ListAccounts(ctx, params.Hostname)
if err != nil { if err != nil {
return res, err return res, err
} }
res.Accounts = accounts outAccounts := make([]descr.AccountShort, 0)
if params.Regex != "" {
for _, item := range accounts {
if outRe.MatchString(item.Username) {
outAccounts = append(outAccounts, item)
}
}
} else {
outAccounts = accounts
}
if params.Detail {
res.Accounts = outAccounts
} else {
res.Usernames = make([]string, 0)
for _, item := range outAccounts {
res.Usernames = append(res.Usernames, item.Username)
}
}
return res, err return res, err
} }
+40 -7
View File
@@ -87,35 +87,37 @@ func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (*des
return res, err return res, err
} }
func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username string) error { func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username string) (*descr.AccountShort, error) {
var err error var err error
res := &descr.AccountShort{}
apipath, err := setApiPath(hosturi, "/v3/api/account/get") apipath, err := setApiPath(hosturi, "/v3/api/account/get")
if err != nil { if err != nil {
return err return res, err
} }
operParams := operator.GetAccountParams{ operParams := operator.GetAccountParams{
Username: username, Username: username,
} }
paramsJson, err := json.Marshal(operParams) paramsJson, err := json.Marshal(operParams)
if err != nil { if err != nil {
return err return res, err
} }
respBytes, err := doHTTPCall(ctx, apipath, paramsJson) respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
if err != nil { if err != nil {
return err return res, err
} }
operRes := handler.NewResponse[operator.GetAccountResult]() operRes := handler.NewResponse[operator.GetAccountResult]()
err = json.Unmarshal(respBytes, operRes) err = json.Unmarshal(respBytes, operRes)
if err != nil { if err != nil {
return err return res, err
} }
if operRes.Error { if operRes.Error {
err = fmt.Errorf("%s", operRes.Message) err = fmt.Errorf("%s", operRes.Message)
return err return res, err
} }
return err res = operRes.Result.Account
return res, err
} }
func (cli *Client) UpdateAccountByID(ctx context.Context, hosturi, id, newUsername, newPassword string) error { func (cli *Client) UpdateAccountByID(ctx context.Context, hosturi, id, newUsername, newPassword string) error {
@@ -151,6 +153,37 @@ func (cli *Client) UpdateAccountByID(ctx context.Context, hosturi, id, newUserna
return err return err
} }
func (cli *Client) UpdateAccountByName(ctx context.Context, hosturi, username, newUsername, newPassword string) error {
var err error
apipath, err := setApiPath(hosturi, "/v3/api/account/update")
if err != nil {
return err
}
operParams := operator.UpdateAccountParams{
Username: username,
NewUsername: newUsername,
NewPassword: newPassword,
}
paramsJson, err := json.Marshal(operParams)
if err != nil {
return err
}
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
if err != nil {
return err
}
operRes := handler.NewResponse[operator.UpdateAccountResult]()
err = json.Unmarshal(respBytes, operRes)
if err != nil {
return err
}
if operRes.Error {
err = fmt.Errorf("%s", operRes.Message)
return err
}
return err
}
func (cli *Client) DeleteAccountByName(ctx context.Context, hosturi, username string) error { func (cli *Client) DeleteAccountByName(ctx context.Context, hosturi, username string) error {
var err error var err error