working commit

This commit is contained in:
2026-02-18 16:28:02 +02:00
parent 8739f2feb7
commit e1ca9b1b4c
8 changed files with 183 additions and 117 deletions
+35 -52
View File
@@ -16,7 +16,6 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"mstore/app/descr"
"mstore/pkg/client"
@@ -55,75 +54,59 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
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",
Use: "create host username password",
Short: "Create user account",
Args: cobra.ExactArgs(3),
Run: util.CreateAccount,
}
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username")
createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password")
createAccountCmd.MarkFlagsRequiredTogether("newuser", "newpass")
subCmd.AddCommand(createAccountCmd)
// GetAccount
var getAccountCmd = &cobra.Command{
Use: "get",
Use: "get hostname accountId|username",
Short: "Get account info",
Args: cobra.ExactArgs(2),
Run: util.GetAccount,
}
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.MarkFlagsOneRequired("id", "name")
subCmd.AddCommand(getAccountCmd)
// UpdateAccount
var updateAccountCmd = &cobra.Command{
Use: "update",
Use: "update hostname username|accounId",
Short: "Update account parameters",
Args: cobra.ExactArgs(2),
Run: util.UpdateAccount,
}
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.MarkFlagsOneRequired("id", "name")
updateAccountCmd.MarkFlagsOneRequired("newname", "newpass")
subCmd.AddCommand(updateAccountCmd)
// DeleteAccount
var deleteAccountCmd = &cobra.Command{
Use: "delete",
Use: "delete hostname username|accountId",
Short: "Delete account",
Args: cobra.ExactArgs(2),
Run: util.DeleteAccount,
}
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID")
deleteAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username")
deleteAccountCmd.MarkFlagsOneRequired("id", "name")
subCmd.AddCommand(deleteAccountCmd)
// ListAccount
var listAccountsCmd = &cobra.Command{
Use: "list",
Use: "list hostname",
Short: "list accounts",
Args: cobra.ExactArgs(1),
Run: util.ListAccounts,
}
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)
viper.BindPFlags(subCmd.Flags())
viper.SetEnvPrefix("MSTORE")
// Bind environment variables
viper.BindEnv("user")
viper.BindEnv("pass")
viper.BindEnv("host")
return subCmd
}
@@ -133,11 +116,14 @@ type CreateAccountParams struct {
NewPassword string
}
type CreateAccountResult struct {
AccountID string `yaml:"accountId"`
GrantIDs []string `yaml:"grantsIds,omitempty"`
AccountID string `yaml:"accountId"`
Grants map[string]string `yaml:"grantsIds,omitempty"`
}
func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
util.commonAccountParams.Hostname = args[0]
util.createAccountParams.NewUsername = args[1]
util.createAccountParams.NewPassword = args[2]
res, err := util.createAccount(&util.commonAccountParams, &util.createAccountParams)
printResponse(res, err)
}
@@ -145,7 +131,7 @@ func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
func (util *AccountUtil) createAccount(common *CommonAccountParams, params *CreateAccountParams) (*CreateAccountResult, error) {
var err error
res := &CreateAccountResult{
GrantIDs: make([]string, 0),
Grants: make(map[string]string, 0),
}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
@@ -167,11 +153,11 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea
descr.RightReadImages,
}
for _, right := range fullRights {
id, err := client.NewClient().CreateGrant(ctx, hostname, accountID, right, ".*")
id, err := client.NewClient().CreateGrantByAccountID(ctx, hostname, accountID, right, ".*")
if err != nil {
return res, err
}
res.GrantIDs = append(res.GrantIDs, id)
res.Grants[id] = right
}
res.AccountID = accountID
return res, err
@@ -179,9 +165,6 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea
// UpdateAccount
type UpdateAccountParams struct {
Hostname string
Username string
Password string
Timeout uint64
AccountID string
NewUsername string
@@ -192,6 +175,8 @@ type UpdateAccountResult struct {
}
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)
}
@@ -220,14 +205,13 @@ func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *Upda
// Get file
type GetAccountParams struct {
Hostname string
Username string
Password string
Timeout uint64
AccountID string
}
func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
util.commonAccountParams.Hostname = args[0]
util.getAccountParams.AccountID = args[1]
res, err := util.getAccount(&util.commonAccountParams, &util.getAccountParams)
printResponse(res, err)
}
@@ -263,9 +247,6 @@ func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAcco
// DeleteAccount
type DeleteAccountParams struct {
Hostname string
Username string
Password string
AccountID string
Timeout uint64
}
@@ -273,6 +254,8 @@ type DeleteAccountParams struct {
type DeleteAccountResult struct{}
func (util *AccountUtil) DeleteAccount(cmd *cobra.Command, args []string) {
util.commonAccountParams.Hostname = args[0]
util.deleteAccountParams.AccountID = args[1]
res, err := util.deleteAccount(&util.commonAccountParams, &util.deleteAccountParams)
printResponse(res, err)
}
@@ -300,17 +283,15 @@ func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *Dele
// ListAccounts
type ListAccountsParams struct {
Hostname string
Username string
Password string
Timeout uint64
Detail bool
Regex string
Timeout uint64
Detail bool
Regex string
}
type Userinfo struct {
Username string `yaml:"username,omitempty"`
Rights []string `yaml:"rights,omitempty"`
Username string `yaml:"username,omitempty"`
AccountID string `yaml:"accountId,omitempty"`
Rights map[string]string `yaml:"rights,omitempty"`
}
type ListAccountsResult struct {
@@ -319,6 +300,7 @@ type ListAccountsResult struct {
}
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
util.commonAccountParams.Hostname = args[0]
res, err := util.listAccounts(&util.commonAccountParams, &util.listAccountsParams)
printResponse(res, err)
}
@@ -356,11 +338,12 @@ func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListA
res.Users = make([]Userinfo, 0)
for _, account := range outAccounts {
userinfo := Userinfo{
Username: account.Username,
Rights: make([]string, 0),
Username: account.Username,
AccountID: account.ID,
Rights: make(map[string]string, 0),
}
for _, grant := range account.Grants {
userinfo.Rights = append(userinfo.Rights, grant.Right)
userinfo.Rights[grant.ID] = grant.Right
}
res.Users = append(res.Users, userinfo)
}
+39 -35
View File
@@ -31,69 +31,53 @@ func (util *GrantUtil) CreateGrantCmds() *cobra.Command {
subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Username, "user", "u", "", "Username")
subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Password, "pass", "p", "", "Password")
subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Hostname, "host", "x", defaultHostname, "Hostname")
subCmd.PersistentFlags().Uint64VarP(&util.commonGrantParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
subCmd.MarkPersistentFlagRequired("host")
subCmd.MarkFlagsRequiredTogether("user", "pass")
// CreateGrant
var createGrantCmd = &cobra.Command{
Use: "create",
Use: "create hostname[:port] username|accountId rigth pattern",
Short: "Create grant",
Args: cobra.ExactArgs(4),
Run: util.CreateGrant,
}
createGrantCmd.Flags().StringVarP(&util.createGrantParams.Right, "newuser", "U", "", "New account username")
createGrantCmd.Flags().StringVarP(&util.createGrantParams.Pattern, "newpass", "P", "", "New account password")
createGrantCmd.MarkFlagsRequiredTogether("newuser", "newpass")
subCmd.AddCommand(createGrantCmd)
// GetGrant
var getGrantCmd = &cobra.Command{
Use: "get",
Use: "get hostname[:port] grantId",
Short: "Get detail grant info",
Args: cobra.ExactArgs(2),
Run: util.GetGrant,
}
getGrantCmd.Flags().StringVarP(&util.getGrantParams.GrantID, "id", "I", "", "Grant ID or name")
getGrantCmd.Flags().StringVarP(&util.getGrantParams.GrantID, "name", "n", "", "Grant ID or name")
getGrantCmd.MarkFlagsOneRequired("id", "name")
subCmd.AddCommand(getGrantCmd)
// UpdateGrant
var updateGrantCmd = &cobra.Command{
Use: "update",
Use: "update hostname[:port] gruntId newPattern",
Short: "Update grant parameters",
Run: util.UpdateGrant,
Args: cobra.ExactArgs(3),
Run: util.UpdateGrant,
}
updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.GrantID, "id", "I", "", "Grant ID or username")
updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.GrantID, "name", "n", "", "Grant ID or username")
updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.Right, "rigth", "U", "", "Rigth")
updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.Pattern, "pattern", "P", "", "New pattern")
updateGrantCmd.MarkFlagsOneRequired("id", "name")
updateGrantCmd.MarkFlagRequired("rigth")
updateGrantCmd.MarkFlagRequired("pattern")
subCmd.AddCommand(updateGrantCmd)
// DeleteGrant
var deleteGrantCmd = &cobra.Command{
Use: "delete",
Use: "delete hostname[:port] gruntId ",
Short: "Delete grant",
Run: util.DeleteGrant,
Args: cobra.ExactArgs(2),
Run: util.DeleteGrant,
}
deleteGrantCmd.Flags().StringVarP(&util.deleteGrantParams.GrantID, "id", "I", "", "Grant ID")
deleteGrantCmd.MarkFlagRequired("id")
subCmd.AddCommand(deleteGrantCmd)
// ListGrants
var listGrantsCmd = &cobra.Command{
Use: "list",
Use: "list hostname[:port] accountId|username",
Short: "list user grants",
Args: cobra.ExactArgs(2),
Run: util.ListGrants,
}
listGrantsCmd.Flags().BoolVarP(&util.listGrantsParams.Detail, "detail", "d", false, "Show detail information")
listGrantsCmd.Flags().StringVarP(&util.listGrantsParams.AccountID, "id", "I", "", "User account ID")
listGrantsCmd.Flags().StringVarP(&util.listGrantsParams.AccountID, "name", "n", "", "User account ID")
listGrantsCmd.MarkFlagRequired("host")
listGrantsCmd.MarkFlagsOneRequired("id", "name")
subCmd.AddCommand(listGrantsCmd)
@@ -127,6 +111,10 @@ type CreateGrantResult struct {
}
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)
}
@@ -140,23 +128,32 @@ func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGran
}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
accountID, err := client.NewClient().CreateGrant(ctx, hostname, params.AccountID, params.Right, params.Pattern)
re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
var operRes string
if re.MatchString(id) {
operRes, err = client.NewClient().CreateGrantByAccountID(ctx, hostname, params.AccountID, params.Right, params.Pattern)
} else {
operRes, err = client.NewClient().CreateGrantByUsername(ctx, hostname, params.AccountID, params.Right, params.Pattern)
}
if err != nil {
return res, err
}
res.GrantID = accountID
res.GrantID = operRes
return res, err
}
// UpdateGrant
type UpdateGrantParams struct {
GrantID string
Right string
Pattern string
}
type UpdateGrantResult struct{}
func (util *GrantUtil) UpdateGrant(cmd *cobra.Command, args []string) {
util.commonGrantParams.Hostname = args[0]
util.updateGrantParams.GrantID = args[1]
util.updateGrantParams.Pattern = args[2]
res, err := util.updateGrant(&util.commonGrantParams, &util.updateGrantParams)
printResponse(res, err)
}
@@ -188,6 +185,9 @@ type GetGrantResult struct {
}
func (util *GrantUtil) GetGrant(cmd *cobra.Command, args []string) {
util.commonGrantParams.Hostname = args[0]
util.getGrantParams.GrantID = args[1]
res, err := util.getGrant(&util.commonGrantParams, &util.getGrantParams)
printResponse(res, err)
}
@@ -220,6 +220,8 @@ type DeleteGrantParams struct {
type DeleteGrantResult struct{}
func (util *GrantUtil) DeleteGrant(cmd *cobra.Command, args []string) {
util.commonGrantParams.Hostname = args[0]
util.deleteGrantParams.GrantID = args[1]
res, err := util.deleteGrant(&util.commonGrantParams, &util.deleteGrantParams)
printResponse(res, err)
}
@@ -248,10 +250,12 @@ type ListGrantsParams struct {
type ListGrantsResult struct {
Grants []descr.Grant `yaml:"grants,omitempty"`
Rights []string `yaml:"rights,omitempty"`
Rights map[string]string `yaml:"rights,omitempty"`
}
func (util *GrantUtil) ListGrants(cmd *cobra.Command, args []string) {
util.commonGrantParams.Hostname = args[0]
util.listGrantsParams.AccountID = args[1]
res, err := util.listGrants(&util.commonGrantParams, &util.listGrantsParams)
printResponse(res, err)
}
@@ -278,9 +282,9 @@ func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsP
if params.Detail {
res.Grants = grants
} else {
res.Rights = make([]string, 0)
res.Rights = make(map[string]string, 0)
for _, item := range grants {
res.Rights = append(res.Rights, item.Right)
res.Rights[item.ID] = item.Right
}
}
return res, err
+3 -3
View File
@@ -47,9 +47,9 @@ func (util *Util) Build() error {
var err error
execName := filepath.Base(os.Args[0])
rootCmd := cobra.Command{
Use: execName,
Short: "\nA brief description the command",
SilenceUsage: true,
Use: execName,
Short: "\nA brief description the command",
//SilenceUsage: true,
}
rootCmd.CompletionOptions.DisableDefaultCmd = true