working commit
This commit is contained in:
@@ -8,6 +8,7 @@ sbin_PROGRAMS = mstored
|
|||||||
mstorectl_SOURCES = \
|
mstorectl_SOURCES = \
|
||||||
cmd/mstorectl/filecmd.go \
|
cmd/mstorectl/filecmd.go \
|
||||||
cmd/mstorectl/imagecmd.go \
|
cmd/mstorectl/imagecmd.go \
|
||||||
|
cmd/mstorectl/accountcmd.go \
|
||||||
cmd/mstorectl/main.go
|
cmd/mstorectl/main.go
|
||||||
|
|
||||||
mstored_SOURCES = \
|
mstored_SOURCES = \
|
||||||
|
|||||||
+3
-2
@@ -163,8 +163,8 @@ am__define_uniq_tagged_files = \
|
|||||||
done | $(am__uniquify_input)`
|
done | $(am__uniquify_input)`
|
||||||
AM_RECURSIVE_TARGETS = cscope
|
AM_RECURSIVE_TARGETS = cscope
|
||||||
am__DIST_COMMON = $(srcdir)/Makefile.in \
|
am__DIST_COMMON = $(srcdir)/Makefile.in \
|
||||||
$(top_srcdir)/app/config/variant.go.in config.guess config.sub \
|
$(top_srcdir)/app/config/variant.go.in README.md config.guess \
|
||||||
install-sh missing
|
config.sub install-sh missing
|
||||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||||
distdir = $(PACKAGE)-$(VERSION)
|
distdir = $(PACKAGE)-$(VERSION)
|
||||||
top_distdir = $(distdir)
|
top_distdir = $(distdir)
|
||||||
@@ -289,6 +289,7 @@ GOFLAGS = -v
|
|||||||
mstorectl_SOURCES = \
|
mstorectl_SOURCES = \
|
||||||
cmd/mstorectl/filecmd.go \
|
cmd/mstorectl/filecmd.go \
|
||||||
cmd/mstorectl/imagecmd.go \
|
cmd/mstorectl/imagecmd.go \
|
||||||
|
cmd/mstorectl/accountcmd.go \
|
||||||
cmd/mstorectl/main.go
|
cmd/mstorectl/main.go
|
||||||
|
|
||||||
mstored_SOURCES = \
|
mstored_SOURCES = \
|
||||||
|
|||||||
@@ -0,0 +1,286 @@
|
|||||||
|
/*
|
||||||
|
* 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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"mstore/app/descr"
|
||||||
|
"mstore/pkg/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
|
||||||
|
var subCmd = &cobra.Command{
|
||||||
|
Use: "account",
|
||||||
|
Short: "Account operation",
|
||||||
|
}
|
||||||
|
const defaultTimeout uint64 = 10
|
||||||
|
// CreateAccount
|
||||||
|
var createAccountCmd = &cobra.Command{
|
||||||
|
Use: "create",
|
||||||
|
Short: "Create user account",
|
||||||
|
Run: util.CreateAccount,
|
||||||
|
}
|
||||||
|
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().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")
|
||||||
|
|
||||||
|
subCmd.AddCommand(createAccountCmd)
|
||||||
|
|
||||||
|
// GetAccount
|
||||||
|
var getAccountCmd = &cobra.Command{
|
||||||
|
Use: "get",
|
||||||
|
Short: "Get account info",
|
||||||
|
Run: util.GetAccount,
|
||||||
|
}
|
||||||
|
getAccountCmd.Flags().StringVarP(&util.getAccountParams.Hostname, "host", "x", "", "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")
|
||||||
|
|
||||||
|
subCmd.AddCommand(getAccountCmd)
|
||||||
|
|
||||||
|
// UpdateAccount
|
||||||
|
var updateAccountCmd = &cobra.Command{
|
||||||
|
Use: "info",
|
||||||
|
Short: "Show file information",
|
||||||
|
Run: util.UpdateAccount,
|
||||||
|
}
|
||||||
|
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.Username, "username", "u", "", "Username")
|
||||||
|
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.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID")
|
||||||
|
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newuser", "U", "", "New username")
|
||||||
|
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "pass", "P", "", "New password")
|
||||||
|
|
||||||
|
subCmd.AddCommand(updateAccountCmd)
|
||||||
|
|
||||||
|
// DeleteAccount
|
||||||
|
var deleteAccountCmd = &cobra.Command{
|
||||||
|
Use: "delete",
|
||||||
|
Short: "Delete file in storage",
|
||||||
|
Run: util.DeleteAccount,
|
||||||
|
}
|
||||||
|
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().Uint64VarP(&util.deleteAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||||
|
|
||||||
|
deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID")
|
||||||
|
|
||||||
|
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
|
||||||
|
var listAccountsCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "list user accounts",
|
||||||
|
Run: util.ListAccounts,
|
||||||
|
}
|
||||||
|
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().Uint64VarP(&util.listAccountsParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||||
|
|
||||||
|
subCmd.AddCommand(listAccountsCmd)
|
||||||
|
return subCmd
|
||||||
|
}
|
||||||
|
|
||||||
|
type AccountUtil struct {
|
||||||
|
createAccountParams CreateAccountParams
|
||||||
|
updateAccountParams UpdateAccountParams
|
||||||
|
getAccountParams GetAccountParams
|
||||||
|
deleteAccountParams DeleteAccountParams
|
||||||
|
listAccountsParams ListAccountsParams
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAccount
|
||||||
|
type CreateAccountParams struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Hostname string
|
||||||
|
NewUsername string
|
||||||
|
NewPassword string
|
||||||
|
Timeout uint64
|
||||||
|
}
|
||||||
|
type CreateAccountResult struct {
|
||||||
|
AccountID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
|
||||||
|
res, err := util.createAccount(&util.createAccountParams)
|
||||||
|
printResponse(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) createAccount(params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &CreateAccountResult{}
|
||||||
|
params.Hostname, err = packUserinfo(params.Hostname, params.NewUsername, params.NewPassword)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
timeout := time.Duration(params.Timeout) * time.Second
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||||
|
accountID, err := client.NewClient().CreateAccount(ctx, params.Hostname, params.NewUsername, params.NewPassword)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
res.AccountID = accountID
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateAccount
|
||||||
|
type UpdateAccountParams struct {
|
||||||
|
Hostname string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Timeout uint64
|
||||||
|
AccountID string
|
||||||
|
NewUsername string
|
||||||
|
NewPassword string
|
||||||
|
}
|
||||||
|
type UpdateAccountResult struct {
|
||||||
|
File *descr.File `json:"file,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||||
|
res, err := util.updateAccount(&util.updateAccountParams)
|
||||||
|
printResponse(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) updateAccount(params *UpdateAccountParams) (*UpdateAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &UpdateAccountResult{}
|
||||||
|
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file
|
||||||
|
type GetAccountParams struct {
|
||||||
|
Hostname string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Timeout uint64
|
||||||
|
AccountID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
|
||||||
|
res, err := util.getAccount(&util.getAccountParams)
|
||||||
|
printResponse(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAccountResult struct{}
|
||||||
|
|
||||||
|
func (util *AccountUtil) getAccount(params *GetAccountParams) (*GetAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &GetAccountResult{}
|
||||||
|
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
timeout := time.Duration(params.Timeout) * time.Second
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||||
|
_, err = client.NewClient().GetAccountByID(ctx, params.Hostname, params.AccountID)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAccount
|
||||||
|
type DeleteAccountParams struct {
|
||||||
|
Hostname string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
AccountID string
|
||||||
|
Timeout uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteAccountResult struct{}
|
||||||
|
|
||||||
|
func (util *AccountUtil) DeleteAccount(cmd *cobra.Command, args []string) {
|
||||||
|
res, err := util.deleteAccount(&util.deleteAccountParams)
|
||||||
|
printResponse(res, err)
|
||||||
|
}
|
||||||
|
func (util *AccountUtil) deleteAccount(params *DeleteAccountParams) (*DeleteAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &DeleteAccountResult{}
|
||||||
|
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
timeout := time.Duration(params.Timeout) * time.Second
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||||
|
err = client.NewClient().DeleteAccountByID(ctx, params.Hostname, params.AccountID)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAccounts
|
||||||
|
type ListAccountsParams struct {
|
||||||
|
Hostname string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Timeout uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListAccountsResult struct {
|
||||||
|
Accounts []descr.AccountShort `json:"accounts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
|
||||||
|
res, err := util.listAccounts(&util.listAccountsParams)
|
||||||
|
printResponse(res, err)
|
||||||
|
}
|
||||||
|
func (util *AccountUtil) listAccounts(params *ListAccountsParams) (*ListAccountsResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &ListAccountsResult{}
|
||||||
|
params.Hostname, err = packUserinfo(params.Hostname, params.Username, params.Password)
|
||||||
|
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
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
@@ -34,6 +34,7 @@ func main() {
|
|||||||
type Util struct {
|
type Util struct {
|
||||||
FileUtil
|
FileUtil
|
||||||
ImageUtil
|
ImageUtil
|
||||||
|
AccountUtil
|
||||||
rootCmd cobra.Command
|
rootCmd cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +54,8 @@ func (util *Util) Build() error {
|
|||||||
rootCmd.AddCommand(util.CreateFileCmds())
|
rootCmd.AddCommand(util.CreateFileCmds())
|
||||||
rootCmd.AddCommand(util.CreateFilesCmds())
|
rootCmd.AddCommand(util.CreateFilesCmds())
|
||||||
rootCmd.AddCommand(util.CreateImageCmds())
|
rootCmd.AddCommand(util.CreateImageCmds())
|
||||||
|
rootCmd.AddCommand(util.CreateAccountCmds())
|
||||||
|
rootCmd.AddCommand(util.CreateAccountsCmds())
|
||||||
|
|
||||||
util.rootCmd = rootCmd
|
util.rootCmd = rootCmd
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username strin
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, newUsername, newPassword string) error {
|
func (cli *Client) UpdateAccountByID(ctx context.Context, hosturi, id, newUsername, newPassword string) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
apipath, err := setApiPath(hosturi, "/v3/api/account/update")
|
apipath, err := setApiPath(hosturi, "/v3/api/account/update")
|
||||||
@@ -126,7 +126,7 @@ func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, new
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
operParams := operator.UpdateAccountParams{
|
operParams := operator.UpdateAccountParams{
|
||||||
Username: username,
|
//Username: username,
|
||||||
AccountID: id,
|
AccountID: id,
|
||||||
NewUsername: newUsername,
|
NewUsername: newUsername,
|
||||||
NewPassword: newPassword,
|
NewPassword: newPassword,
|
||||||
|
|||||||
@@ -12,9 +12,6 @@ package client
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
//"math/rand"
|
|
||||||
//"os"
|
|
||||||
//"path/filepath"
|
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|||||||
Reference in New Issue
Block a user