working commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user