working commit
This commit is contained in:
+73
-10
@@ -11,6 +11,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -19,6 +21,11 @@ import (
|
||||
"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 {
|
||||
var subCmd = &cobra.Command{
|
||||
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.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().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username")
|
||||
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password")
|
||||
createAccountCmd.MarkFlagRequired("host")
|
||||
createAccountCmd.MarkFlagsRequiredTogether("newuser", "newpass")
|
||||
|
||||
subCmd.AddCommand(createAccountCmd)
|
||||
|
||||
@@ -47,11 +56,12 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
|
||||
Short: "Get account info",
|
||||
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.Password, "password", "p", "", "Password")
|
||||
getAccountCmd.Flags().Uint64VarP(&util.getAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID")
|
||||
getAccountCmd.MarkFlagRequired("host")
|
||||
|
||||
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.Hostname, "host", "x", "", "File path")
|
||||
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.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.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.MarkFlagRequired("host")
|
||||
|
||||
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.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().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)
|
||||
return subCmd
|
||||
@@ -182,7 +197,13 @@ func (util *AccountUtil) updateAccount(params *UpdateAccountParams) (*UpdateAcco
|
||||
}
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
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 {
|
||||
return res, err
|
||||
}
|
||||
@@ -203,7 +224,9 @@ func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type GetAccountResult struct{}
|
||||
type GetAccountResult struct {
|
||||
Account *descr.AccountShort `json:"account,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult, error) {
|
||||
var err error
|
||||
@@ -214,10 +237,19 @@ func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult
|
||||
}
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
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 {
|
||||
return res, err
|
||||
}
|
||||
res.Account = opRes
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -245,7 +277,13 @@ func (util *AccountUtil) deleteAccount(params *DeleteAccountParams) (*DeleteAcco
|
||||
}
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
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 {
|
||||
return res, err
|
||||
}
|
||||
@@ -258,10 +296,13 @@ type ListAccountsParams struct {
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
Detail bool
|
||||
Regex string
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -275,12 +316,34 @@ func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccounts
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
outRe, err := regexp.Compile(params.Regex)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
accounts, err := client.NewClient().ListAccounts(ctx, params.Hostname)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user