working commit

This commit is contained in:
2026-02-15 11:54:32 +02:00
parent c7d041f416
commit af44293195
10 changed files with 274 additions and 339 deletions
+96 -96
View File
@@ -26,29 +26,46 @@ const (
defaultHostname = "localhost:1025"
)
type AccountUtil struct {
createAccountParams CreateAccountParams
updateAccountParams UpdateAccountParams
getAccountParams GetAccountParams
deleteAccountParams DeleteAccountParams
listAccountsParams ListAccountsParams
commonAccountParams CommonAccountParams
}
type CommonAccountParams struct {
Username string
Password string
Hostname string
Timeout uint64
}
func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
var subCmd = &cobra.Command{
Use: "account",
Short: "Account operation",
Use: "accounts",
Short: "Account operation",
Aliases: []string{"account"},
}
const defaultTimeout uint64 = 10
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Username, "user", "u", "", "Username")
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "p", "", "Password")
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
subCmd.MarkPersistentFlagRequired("host")
subCmd.MarkFlagsRequiredTogether("user", "pass")
// CreateAccount
var createAccountCmd = &cobra.Command{
Use: "create",
Short: "Create user account",
Run: util.CreateAccount,
}
createAccountCmd.Flags().StringVarP(&util.createAccountParams.Username, "user", "u", "", "Username")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.Password, "pass", "p", "", "Password")
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")
createAccountCmd.MarkFlagsRequiredTogether("user", "pass")
subCmd.AddCommand(createAccountCmd)
// GetAccount
@@ -57,38 +74,23 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
Short: "Get account info",
Run: util.GetAccount,
}
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Username, "user", "u", "", "Username")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Password, "pass", "p", "", "Password")
getAccountCmd.Flags().Uint64VarP(&util.getAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID or name")
getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "name", "n", "", "Account ID or name")
getAccountCmd.MarkFlagRequired("host")
getAccountCmd.MarkFlagsOneRequired("id", "name")
getAccountCmd.MarkFlagsRequiredTogether("user", "pass")
subCmd.AddCommand(getAccountCmd)
// UpdateAccount
var updateAccountCmd = &cobra.Command{
Use: "info",
Use: "update",
Short: "Update account parameters",
Run: util.UpdateAccount,
}
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Username, "user", "u", "", "Username")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Password, "pass", "p", "", "Password")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Hostname, "host", "x", "", "File path")
updateAccountCmd.Flags().Uint64VarP(&util.updateAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID or username")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "N", "", "New username")
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "P", "", "New password")
updateAccountCmd.MarkFlagRequired("host")
updateAccountCmd.MarkFlagsOneRequired("id", "name")
updateAccountCmd.MarkFlagsOneRequired("newname", "newpass")
subCmd.AddCommand(updateAccountCmd)
// DeleteAccount
@@ -97,85 +99,71 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
Short: "Delete account",
Run: util.DeleteAccount,
}
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Username, "user", "u", "", "Username")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Password, "pass", "p", "", "Password")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.Hostname, "host", "x", defaultHostname, "Hostname")
deleteAccountCmd.Flags().Uint64VarP(&util.deleteAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID")
deleteAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username")
deleteAccountCmd.MarkFlagRequired("host")
deleteAccountCmd.MarkFlagsOneRequired("id", "name")
deleteAccountCmd.MarkFlagsRequiredTogether("user", "pass")
subCmd.AddCommand(deleteAccountCmd)
return subCmd
}
func (util *AccountUtil) CreateAccountsCmds() *cobra.Command {
var subCmd = &cobra.Command{
Use: "accounts",
Short: "Accounts operation",
}
const defaultTimeout uint64 = 10
// ListAccounts
// ListAccount
var listAccountsCmd = &cobra.Command{
Use: "list",
Short: "list accounts",
Run: util.ListAccounts,
}
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Username, "user", "u", "", "Username")
listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Password, "pass", "p", "", "Password")
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
}
type AccountUtil struct {
createAccountParams CreateAccountParams
updateAccountParams UpdateAccountParams
getAccountParams GetAccountParams
deleteAccountParams DeleteAccountParams
listAccountsParams ListAccountsParams
return subCmd
}
// CreateAccount
type CreateAccountParams struct {
Username string
Password string
Hostname string
NewUsername string
NewPassword string
Timeout uint64
}
type CreateAccountResult struct {
AccountID string
AccountID string `json:"accountId"`
GrantIDs []string `json:"grantsIds,omitempty"`
}
func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
res, err := util.createAccount(&util.createAccountParams)
res, err := util.createAccount(&util.commonAccountParams, &util.createAccountParams)
printResponse(res, err)
}
func (util *AccountUtil) createAccount(params *CreateAccountParams) (*CreateAccountResult, error) {
func (util *AccountUtil) createAccount(common *CommonAccountParams, params *CreateAccountParams) (*CreateAccountResult, error) {
var err error
res := &CreateAccountResult{}
params.Hostname, err = packUserinfo(params.Hostname, params.NewUsername, params.NewPassword)
res := &CreateAccountResult{
GrantIDs: make([]string, 0),
}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
accountID, err := client.NewClient().CreateAccount(ctx, params.Hostname, params.NewUsername, params.NewPassword)
accountID, err := client.NewClient().CreateAccount(ctx, hostname, params.NewUsername, params.NewPassword)
if err != nil {
return res, err
}
fullRights := []string{
descr.RightWriteAccounts,
descr.RightReadAccounts,
descr.RightWriteFiles,
descr.RightReadFiles,
descr.RightWriteImages,
descr.RightReadImages,
}
for _, right := range fullRights {
id, err := client.NewClient().CreateGrant(ctx, hostname, accountID, right, ".*")
if err != nil {
return res, err
}
res.GrantIDs = append(res.GrantIDs, id)
}
res.AccountID = accountID
return res, err
}
@@ -195,25 +183,25 @@ type UpdateAccountResult struct {
}
func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) {
res, err := util.updateAccount(&util.updateAccountParams)
res, err := util.updateAccount(&util.commonAccountParams, &util.updateAccountParams)
printResponse(res, err)
}
func (util *AccountUtil) updateAccount(params *UpdateAccountParams) (*UpdateAccountResult, error) {
func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *UpdateAccountParams) (*UpdateAccountResult, error) {
var err error
res := &UpdateAccountResult{}
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
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)
err = client.NewClient().UpdateAccountByID(ctx, hostname, id, params.NewUsername, params.NewPassword)
} else {
err = client.NewClient().UpdateAccountByName(ctx, params.Hostname, params.AccountID, params.NewUsername, params.NewPassword)
err = client.NewClient().UpdateAccountByName(ctx, hostname, params.AccountID, params.NewUsername, params.NewPassword)
}
if err != nil {
return res, err
@@ -231,7 +219,7 @@ type GetAccountParams struct {
}
func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
res, err := util.getAccount(&util.getAccountParams)
res, err := util.getAccount(&util.commonAccountParams, &util.getAccountParams)
printResponse(res, err)
}
@@ -239,23 +227,23 @@ type GetAccountResult struct {
Account *descr.AccountShort `json:"account,omitempty"`
}
func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult, error) {
func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAccountParams) (*GetAccountResult, error) {
var err error
res := &GetAccountResult{}
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
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)
opRes, err = client.NewClient().GetAccountByID(ctx, hostname, id)
} else {
opRes, err = client.NewClient().GetAccountByName(ctx, params.Hostname, params.AccountID)
opRes, err = client.NewClient().GetAccountByName(ctx, hostname, params.AccountID)
}
if err != nil {
return res, err
@@ -276,24 +264,24 @@ type DeleteAccountParams struct {
type DeleteAccountResult struct{}
func (util *AccountUtil) DeleteAccount(cmd *cobra.Command, args []string) {
res, err := util.deleteAccount(&util.deleteAccountParams)
res, err := util.deleteAccount(&util.commonAccountParams, &util.deleteAccountParams)
printResponse(res, err)
}
func (util *AccountUtil) deleteAccount(params *DeleteAccountParams) (*DeleteAccountResult, error) {
func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *DeleteAccountParams) (*DeleteAccountResult, error) {
var err error
res := &DeleteAccountResult{}
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
err = client.NewClient().DeleteAccountByID(ctx, params.Hostname, id)
err = client.NewClient().DeleteAccountByID(ctx, hostname, id)
} else {
err = client.NewClient().DeleteAccountByName(ctx, params.Hostname, params.AccountID)
err = client.NewClient().DeleteAccountByName(ctx, hostname, params.AccountID)
}
if err != nil {
return res, err
@@ -311,19 +299,24 @@ type ListAccountsParams struct {
Regex string
}
type Userinfo struct {
Username string `json:"username,omitempty"`
Rights []string `json:"rights,omitempty"`
}
type ListAccountsResult struct {
Accounts []descr.AccountShort `json:"accounts,omitempty"`
Usernames []string `json:"names,omitempty"`
Accounts []descr.AccountShort `json:"accounts,omitempty"`
Users []Userinfo `json:"users,omitempty"`
}
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
res, err := util.listAccounts(&util.listAccountsParams)
res, err := util.listAccounts(&util.commonAccountParams, &util.listAccountsParams)
printResponse(res, err)
}
func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccountsResult, error) {
func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListAccountsParams) (*ListAccountsResult, error) {
var err error
res := &ListAccountsResult{}
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
@@ -332,9 +325,9 @@ func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccounts
return res, err
}
timeout := time.Duration(params.Timeout) * time.Second
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
accounts, err := client.NewClient().ListAccounts(ctx, params.Hostname)
accounts, err := client.NewClient().ListAccounts(ctx, hostname)
if err != nil {
return res, err
}
@@ -351,9 +344,16 @@ func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccounts
if params.Detail {
res.Accounts = outAccounts
} else {
res.Usernames = make([]string, 0)
for _, item := range outAccounts {
res.Usernames = append(res.Usernames, item.Username)
res.Users = make([]Userinfo, 0)
for _, account := range outAccounts {
userinfo := Userinfo{
Username: account.Username,
Rights: make([]string, 0),
}
for _, grant := range account.Grants {
userinfo.Rights = append(userinfo.Rights, grant.Right)
}
res.Users = append(res.Users, userinfo)
}
}
return res, err