added ctl manuals
This commit is contained in:
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* 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 command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mstore/pkg/client"
|
||||
"mstore/pkg/descr"
|
||||
"mstore/pkg/terms"
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
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
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
func (util *AccountUtil) CreateAccountCmds() *cobra.Command {
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "accounts",
|
||||
Short: "Account operations",
|
||||
Aliases: []string{"account"},
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Username, "user", "U", util.commonAccountParams.Username, "Username")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "P", util.commonAccountParams.Password, "Password")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "X", defaultHostname, "Hostname")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "T", defaultTimeout, "Operation timeout")
|
||||
subCmd.PersistentFlags().BoolVarP(&util.commonAccountParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify")
|
||||
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||
|
||||
vi := viper.New()
|
||||
vi.SetEnvPrefix("mstore")
|
||||
vi.BindEnv("user")
|
||||
vi.BindEnv("pass")
|
||||
util.commonAccountParams.Username = vi.GetString("user")
|
||||
util.commonAccountParams.Password = vi.GetString("pass")
|
||||
|
||||
// CreateAccount
|
||||
var createAccountCmd = &cobra.Command{
|
||||
Use: "create [user:pass@]hostname[:port] username password",
|
||||
Short: "Create user account",
|
||||
Args: cobra.ExactArgs(3),
|
||||
Run: util.CreateAccount,
|
||||
}
|
||||
subCmd.AddCommand(createAccountCmd)
|
||||
|
||||
// GetAccount
|
||||
var getAccountCmd = &cobra.Command{
|
||||
Use: "get [user:pass@]hostname[:port] accountId|username",
|
||||
Short: "Get account info",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.GetAccount,
|
||||
}
|
||||
subCmd.AddCommand(getAccountCmd)
|
||||
|
||||
// UpdateAccount
|
||||
var updateAccountCmd = &cobra.Command{
|
||||
Use: "update [user:pass@]hostname[:port] username|accounId",
|
||||
Short: "Update account parameters",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.UpdateAccount,
|
||||
}
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "u", "", "New username")
|
||||
updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "p", "", "New password")
|
||||
updateAccountCmd.MarkFlagsOneRequired("newname", "newpass")
|
||||
subCmd.AddCommand(updateAccountCmd)
|
||||
|
||||
// DeleteAccount
|
||||
var deleteAccountCmd = &cobra.Command{
|
||||
Use: "delete [user:pass@]hostname[:port] username|accountId",
|
||||
Short: "Delete account",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.DeleteAccount,
|
||||
}
|
||||
subCmd.AddCommand(deleteAccountCmd)
|
||||
|
||||
// ListAccount
|
||||
var listAccountsCmd = &cobra.Command{
|
||||
Use: "list [user:pass@]hostname[:port]",
|
||||
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")
|
||||
subCmd.AddCommand(listAccountsCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
// CreateAccount
|
||||
type CreateAccountParams struct {
|
||||
NewUsername string
|
||||
NewPassword string
|
||||
}
|
||||
type CreateAccountResult struct {
|
||||
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)
|
||||
}
|
||||
|
||||
func (util *AccountUtil) createAccount(common *CommonAccountParams, params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||
var err error
|
||||
res := &CreateAccountResult{
|
||||
Grants: make(map[string]string, 0),
|
||||
}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
accountID, err := client.NewClient(common.SkipTLSVerify).CreateAccount(ctx, hostname, params.NewUsername, params.NewPassword)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
fullRights := []string{
|
||||
terms.RightWriteAccounts,
|
||||
terms.RightReadAccounts,
|
||||
terms.RightWriteFiles,
|
||||
terms.RightReadFiles,
|
||||
terms.RightWriteImages,
|
||||
terms.RightReadImages,
|
||||
}
|
||||
for _, right := range fullRights {
|
||||
id, err := client.NewClient(common.SkipTLSVerify).CreateGrantByAccountID(ctx, hostname, accountID, right, ".*")
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Grants[id] = right
|
||||
}
|
||||
res.AccountID = accountID
|
||||
return res, err
|
||||
}
|
||||
|
||||
// UpdateAccount
|
||||
type UpdateAccountParams struct {
|
||||
Timeout uint64
|
||||
AccountID string
|
||||
NewUsername string
|
||||
NewPassword string
|
||||
}
|
||||
type UpdateAccountResult struct {
|
||||
File *descr.File `yaml:"file,omitempty"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *UpdateAccountParams) (*UpdateAccountResult, error) {
|
||||
var err error
|
||||
res := &UpdateAccountResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
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(common.SkipTLSVerify).UpdateAccountByID(ctx, hostname, id, params.NewUsername, params.NewPassword)
|
||||
} else {
|
||||
err = client.NewClient(common.SkipTLSVerify).UpdateAccountByName(ctx, hostname, params.AccountID, params.NewUsername, params.NewPassword)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Get file
|
||||
type GetAccountParams struct {
|
||||
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)
|
||||
}
|
||||
|
||||
type GetAccountResult struct {
|
||||
Account *descr.AccountShort `yaml:"account,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAccountParams) (*GetAccountResult, error) {
|
||||
var err error
|
||||
res := &GetAccountResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
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(common.SkipTLSVerify).GetAccountByID(ctx, hostname, id)
|
||||
} else {
|
||||
opRes, err = client.NewClient(common.SkipTLSVerify).GetAccountByName(ctx, hostname, params.AccountID)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Account = opRes
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteAccount
|
||||
type DeleteAccountParams struct {
|
||||
AccountID string
|
||||
Timeout uint64
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *DeleteAccountParams) (*DeleteAccountResult, error) {
|
||||
var err error
|
||||
res := &DeleteAccountResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
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(common.SkipTLSVerify).DeleteAccountByID(ctx, hostname, id)
|
||||
} else {
|
||||
err = client.NewClient(common.SkipTLSVerify).DeleteAccountByName(ctx, hostname, params.AccountID)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListAccounts
|
||||
type ListAccountsParams struct {
|
||||
Timeout uint64
|
||||
Detail bool
|
||||
Regex string
|
||||
}
|
||||
|
||||
type Userinfo struct {
|
||||
Username string `yaml:"username,omitempty"`
|
||||
AccountID string `yaml:"accountId,omitempty"`
|
||||
Rights map[string]string `yaml:"rights,omitempty"`
|
||||
}
|
||||
|
||||
type ListAccountsResult struct {
|
||||
Accounts []descr.AccountShort `yaml:"accounts,omitempty"`
|
||||
Users []Userinfo `yaml:"users,omitempty"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListAccountsParams) (*ListAccountsResult, error) {
|
||||
var err error
|
||||
res := &ListAccountsResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
outRe, err := regexp.Compile(params.Regex)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
accounts, err := client.NewClient(common.SkipTLSVerify).ListAccounts(ctx, hostname)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
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.Users = make([]Userinfo, 0)
|
||||
for _, account := range outAccounts {
|
||||
userinfo := Userinfo{
|
||||
Username: account.Username,
|
||||
AccountID: account.ID,
|
||||
Rights: make(map[string]string, 0),
|
||||
}
|
||||
for _, grant := range account.Grants {
|
||||
userinfo.Rights[grant.ID] = grant.Right
|
||||
}
|
||||
res.Users = append(res.Users, userinfo)
|
||||
}
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,634 @@
|
||||
/*
|
||||
* 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 command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mstore/pkg/client"
|
||||
"mstore/pkg/descr"
|
||||
"mstore/pkg/terms"
|
||||
)
|
||||
|
||||
func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "files",
|
||||
Short: "File operations",
|
||||
Aliases: []string{"file"},
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Username, "user", "u", "", "Username")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Password, "pass", "p", "", "Password")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
subCmd.PersistentFlags().BoolVarP(&util.commonFileParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify")
|
||||
subCmd.MarkPersistentFlagRequired("host")
|
||||
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||
|
||||
vi := viper.New()
|
||||
vi.SetEnvPrefix("mstore")
|
||||
vi.BindEnv("user")
|
||||
vi.BindEnv("pass")
|
||||
util.commonFileParams.Username = vi.GetString("user")
|
||||
util.commonFileParams.Password = vi.GetString("pass")
|
||||
|
||||
// PutFile
|
||||
var putFileCmd = &cobra.Command{
|
||||
Use: "put filepath [user:pass@]hostname[:port]/collection/name",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Aliases: []string{"push"},
|
||||
Short: "Put file to storage",
|
||||
Run: util.PutFile,
|
||||
}
|
||||
subCmd.AddCommand(putFileCmd)
|
||||
|
||||
// GetFile
|
||||
var getFileCmd = &cobra.Command{
|
||||
Use: "get [user:pass@]hostname[:port]/collection/name filepath",
|
||||
Aliases: []string{"pull"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Get file from storage",
|
||||
Run: util.GetFile,
|
||||
}
|
||||
subCmd.AddCommand(getFileCmd)
|
||||
|
||||
// FileInfo
|
||||
var fileInfoCmd = &cobra.Command{
|
||||
Use: "info [user:pass@]hostname[:port]/collection/name",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Show file information",
|
||||
Run: util.FileInfo,
|
||||
}
|
||||
subCmd.AddCommand(fileInfoCmd)
|
||||
|
||||
// DeleteFile
|
||||
var deleteFileCmd = &cobra.Command{
|
||||
Use: "delete [user:pass@]hostname[:port]/collection/name",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Aliases: []string{"remove"},
|
||||
Short: "Delete file in storage",
|
||||
Run: util.DeleteFile,
|
||||
}
|
||||
subCmd.AddCommand(deleteFileCmd)
|
||||
|
||||
// ListFiles
|
||||
var listFilesCmd = &cobra.Command{
|
||||
Use: "list [user:pass@]hostname[:port]/catalog",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "List files in storage",
|
||||
Run: util.ListFiles,
|
||||
}
|
||||
listFilesCmd.Flags().BoolVarP(&util.listFilesParams.Detail, "detail", "D", false, "Show detail file information")
|
||||
listFilesCmd.Flags().BoolVarP(&util.listFilesParams.AsPrefix, "asprefix", "P", true, "Use path as collection path prefix")
|
||||
listFilesCmd.Flags().BoolVarP(&util.listFilesParams.AsRegexp, "asregex", "R", false, "Use path as collection path prefix")
|
||||
subCmd.AddCommand(listFilesCmd)
|
||||
|
||||
// ImportFiles
|
||||
var importFilesCmd = &cobra.Command{
|
||||
Use: "import directory [user:pass@]hostname[:port]/collection",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Send file tree to storage as is",
|
||||
Run: util.ImportFiles,
|
||||
}
|
||||
subCmd.AddCommand(importFilesCmd)
|
||||
|
||||
// ExportFiles
|
||||
var exportFilesCmd = &cobra.Command{
|
||||
Use: "export directory [user:pass@]hostname[:port]/collection dir",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Download file tree to storage as is",
|
||||
Run: util.ExportFiles,
|
||||
}
|
||||
exportFilesCmd.Flags().BoolVarP(&util.exportFilesParams.Detail, "detail", "D", false, "Show detail file information")
|
||||
exportFilesCmd.Flags().BoolVarP(&util.exportFilesParams.AsPrefix, "asprefix", "P", true, "Use path as collection path prefix")
|
||||
exportFilesCmd.Flags().BoolVarP(&util.exportFilesParams.AsRegexp, "asregex", "R", false, "Use path as collection path prefix")
|
||||
subCmd.AddCommand(exportFilesCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
func (util *FileUtil) CreateCollectionCmds() *cobra.Command {
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "collections",
|
||||
Short: "Colletion operations",
|
||||
Aliases: []string{"col", "cols", "dir", "dirs"},
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Username, "user", "u", "", "Username")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Password, "pass", "p", "", "Password")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
subCmd.MarkPersistentFlagRequired("host")
|
||||
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||
|
||||
// ListCollections
|
||||
var listCollectionsCmd = &cobra.Command{
|
||||
Use: "list [user:pass@]hostname[:port]/catalog",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "List collections in storage",
|
||||
Run: util.ListCollections,
|
||||
}
|
||||
listCollectionsCmd.Flags().BoolVarP(&util.listCollectionsParams.AsPrefix, "asprefix", "P", true, "Use path as collection path prefix")
|
||||
subCmd.AddCommand(listCollectionsCmd)
|
||||
|
||||
// DeleteCollection
|
||||
var deleteCollectionCmd = &cobra.Command{
|
||||
Use: "delete [user:pass@]hostname[:port]/catalog",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Delete all files in collection",
|
||||
Run: util.DeleteCollection,
|
||||
}
|
||||
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.Detail, "detail", "D", false, "Show detail file information")
|
||||
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.AsPrefix, "asprefix", "P", false, "Use path as collection path prefix")
|
||||
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.AsRegexp, "asregex", "R", false, "Use path as collection path prefix")
|
||||
deleteCollectionCmd.Flags().BoolVarP(&util.deleteCollectionParams.DryRun, "dryrun", "Y", false, "Simulate process, don't delete files")
|
||||
|
||||
subCmd.AddCommand(deleteCollectionCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
type FileUtil struct {
|
||||
fileInfoParams FileInfoParams
|
||||
putFileParams PutFileParams
|
||||
getFileParams GetFileParams
|
||||
deleteFileParams DeleteFileParams
|
||||
listFilesParams ListFilesParams
|
||||
importFilesParams ImportFilesParams
|
||||
exportFilesParams ExportFilesParams
|
||||
listCollectionsParams ListCollectionsParams
|
||||
deleteCollectionParams DeleteCollectionParams
|
||||
commonFileParams CommonFileParams
|
||||
}
|
||||
|
||||
type CommonFileParams struct {
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
// FileInfo
|
||||
type FileInfoParams struct {
|
||||
Filepath string
|
||||
}
|
||||
type FileInfoResult struct {
|
||||
File *descr.File `yaml:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) FileInfo(cmd *cobra.Command, args []string) {
|
||||
util.fileInfoParams.Filepath = args[0]
|
||||
res, err := util.fileInfo(&util.commonFileParams, &util.fileInfoParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) fileInfo(common *CommonFileParams, params *FileInfoParams) (*FileInfoResult, error) {
|
||||
var err error
|
||||
res := &FileInfoResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
exists, opres, err := client.NewClient(common.SkipTLSVerify).FileInfo(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !exists {
|
||||
err = fmt.Errorf("File %s not exists", params.Filepath)
|
||||
return res, err
|
||||
}
|
||||
res.File = opres
|
||||
return res, err
|
||||
}
|
||||
|
||||
// PutFile
|
||||
type PutFileParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
type PutFileResult struct{}
|
||||
|
||||
func (util *FileUtil) PutFile(cmd *cobra.Command, args []string) {
|
||||
util.putFileParams.Source = args[0]
|
||||
util.putFileParams.Dest = args[1]
|
||||
res, err := util.putFile(&util.commonFileParams, &util.putFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) putFile(common *CommonFileParams, params *PutFileParams) (*PutFileResult, error) {
|
||||
var err error
|
||||
res := &PutFileResult{}
|
||||
params.Dest, err = packUserinfo(params.Dest, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
err = client.NewClient(common.SkipTLSVerify).PutFile(ctx, params.Source, params.Dest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Get file
|
||||
type GetFileParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
|
||||
func (util *FileUtil) GetFile(cmd *cobra.Command, args []string) {
|
||||
util.getFileParams.Source = args[0]
|
||||
util.getFileParams.Dest = args[1]
|
||||
res, err := util.getFile(&util.commonFileParams, &util.getFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type GetFileResult struct{}
|
||||
|
||||
func (util *FileUtil) getFile(common *CommonFileParams, params *GetFileParams) (*GetFileResult, error) {
|
||||
var err error
|
||||
res := &GetFileResult{}
|
||||
params.Dest, err = packUserinfo(params.Source, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
_, err = client.NewClient(common.SkipTLSVerify).GetFile(ctx, params.Dest, params.Source)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteFile
|
||||
type DeleteFileParams struct {
|
||||
Filepath string
|
||||
}
|
||||
|
||||
type DeleteFileResult struct{}
|
||||
|
||||
func (util *FileUtil) DeleteFile(cmd *cobra.Command, args []string) {
|
||||
util.deleteFileParams.Filepath = args[0]
|
||||
res, err := util.deleteFile(&util.commonFileParams, &util.deleteFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
func (util *FileUtil) deleteFile(common *CommonFileParams, params *DeleteFileParams) (*DeleteFileResult, error) {
|
||||
var err error
|
||||
res := &DeleteFileResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
err = client.NewClient(common.SkipTLSVerify).DeleteFile(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListFiles
|
||||
type ListFilesParams struct {
|
||||
Filepath string
|
||||
Detail bool
|
||||
AsPrefix bool
|
||||
AsRegexp bool
|
||||
}
|
||||
|
||||
type ListFilesResult struct {
|
||||
Files []descr.File `yaml:"files,omitempty"`
|
||||
Filenames []string `yaml:"filenames,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ListFiles(cmd *cobra.Command, args []string) {
|
||||
util.listFilesParams.Filepath = args[0]
|
||||
res, err := util.listFiles(&util.commonFileParams, &util.listFilesParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
func (util *FileUtil) listFiles(common *CommonFileParams, params *ListFilesParams) (*ListFilesResult, error) {
|
||||
var err error
|
||||
res := &ListFilesResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if params.AsRegexp {
|
||||
params.AsPrefix = false
|
||||
}
|
||||
var pathUsage string
|
||||
switch {
|
||||
case params.AsRegexp:
|
||||
pathUsage = terms.AsRegexp
|
||||
case params.AsPrefix:
|
||||
pathUsage = terms.AsPrefix
|
||||
default:
|
||||
pathUsage = terms.AsFinePath
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
files, err := client.NewClient(common.SkipTLSVerify).ListFiles(ctx, params.Filepath, pathUsage)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if params.Detail {
|
||||
res.Files = files
|
||||
} else {
|
||||
res.Filenames = makeFilelistFromFiles(files)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ImportFiles
|
||||
type ImportFilesParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
Progress bool
|
||||
}
|
||||
type ImportFilesResult struct {
|
||||
Files []string `yaml:"files,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ImportFiles(cmd *cobra.Command, args []string) {
|
||||
util.importFilesParams.Source = args[0]
|
||||
util.importFilesParams.Dest = args[1]
|
||||
|
||||
res, err := util.importFiles(&util.commonFileParams, &util.importFilesParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) importFiles(common *CommonFileParams, params *ImportFilesParams) (*ImportFilesResult, error) {
|
||||
var err error
|
||||
res := &ImportFilesResult{
|
||||
Files: make([]string, 0),
|
||||
}
|
||||
params.Dest, err = packUserinfo(params.Dest, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
putErrors := make([]error, 0)
|
||||
walcFunc := func(walkPath string, infoItem fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if infoItem.Mode() == fs.ModeDevice {
|
||||
return nil
|
||||
}
|
||||
if infoItem.Mode() == fs.ModeNamedPipe {
|
||||
return nil
|
||||
}
|
||||
if infoItem.Mode() == fs.ModeCharDevice {
|
||||
return nil
|
||||
}
|
||||
if !infoItem.IsDir() {
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
relPath, _ := strings.CutPrefix(walkPath, params.Source)
|
||||
dest, _ := url.JoinPath(params.Dest, relPath)
|
||||
if err != nil {
|
||||
putErrors = append(putErrors, err)
|
||||
return nil
|
||||
}
|
||||
err = client.NewClient(common.SkipTLSVerify).PutFile(ctx, walkPath, dest)
|
||||
if err != nil {
|
||||
putErrors = append(putErrors, err)
|
||||
fmt.Printf("- %s: error: %v \n", walkPath, err)
|
||||
} else {
|
||||
res.Files = append(res.Files, walkPath)
|
||||
fmt.Printf("- %s: ok\n", walkPath)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, item := range putErrors {
|
||||
err = errors.Join(err, item)
|
||||
}
|
||||
err = filepath.Walk(params.Source, walcFunc)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ExportFiles
|
||||
type ExportFilesParams struct {
|
||||
Filepath string
|
||||
Detail bool
|
||||
AsPrefix bool
|
||||
AsRegexp bool
|
||||
Dest string
|
||||
}
|
||||
|
||||
type ExportFilesResult struct {
|
||||
Files []descr.File `yaml:"files,omitempty"`
|
||||
Filenames []string `yaml:"filenames,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ExportFiles(cmd *cobra.Command, args []string) {
|
||||
util.exportFilesParams.Filepath = args[0]
|
||||
util.exportFilesParams.Dest = args[1]
|
||||
res, err := util.exportFiles(&util.commonFileParams, &util.exportFilesParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
func (util *FileUtil) exportFiles(common *CommonFileParams, params *ExportFilesParams) (*ExportFilesResult, error) {
|
||||
var err error
|
||||
res := &ExportFilesResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if params.AsRegexp {
|
||||
params.AsPrefix = false
|
||||
}
|
||||
var pathUsage string
|
||||
switch {
|
||||
case params.AsRegexp:
|
||||
pathUsage = terms.AsRegexp
|
||||
case params.AsPrefix:
|
||||
pathUsage = terms.AsPrefix
|
||||
default:
|
||||
pathUsage = terms.AsFinePath
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
files, err := client.NewClient(common.SkipTLSVerify).ListFiles(ctx, params.Filepath, pathUsage)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
exportedFiles := make([]descr.File, 0)
|
||||
for _, file := range files {
|
||||
destdir := filepath.Join(params.Dest, file.Collection)
|
||||
err = os.MkdirAll(destdir, 0750)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
srcpath, err := makeFileURI(params.Filepath, file.Collection, file.Name)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
destpath := filepath.Join(params.Dest, file.Collection, file.Name)
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
_, err = client.NewClient(common.SkipTLSVerify).GetFile(ctx, srcpath, destpath)
|
||||
if err != nil {
|
||||
fmt.Printf("- %s: error %v\n", srcpath, err)
|
||||
//return res, err
|
||||
err = nil
|
||||
} else {
|
||||
fmt.Printf("- %s: ok\n", srcpath)
|
||||
exportedFiles = append(exportedFiles, file)
|
||||
}
|
||||
}
|
||||
if params.Detail {
|
||||
res.Files = exportedFiles
|
||||
} else {
|
||||
res.Filenames = makeFilelistFromFiles(exportedFiles)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func makeFileURI(hosturi, collection, name string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
uri, err := url.Parse(hosturi)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
uri.Path = path.Join(collection, name)
|
||||
res = uri.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListCollections
|
||||
type ListCollectionsParams struct {
|
||||
Path string
|
||||
AsPrefix bool
|
||||
AsRegexp bool
|
||||
}
|
||||
|
||||
type ListCollectionsResult struct {
|
||||
Collections []string `yaml:"collections,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ListCollections(cmd *cobra.Command, args []string) {
|
||||
util.listCollectionsParams.Path = args[0]
|
||||
res, err := util.listCollections(&util.commonFileParams, &util.listCollectionsParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) listCollections(common *CommonFileParams, params *ListCollectionsParams) (*ListCollectionsResult, error) {
|
||||
var err error
|
||||
res := &ListCollectionsResult{
|
||||
Collections: make([]string, 0),
|
||||
}
|
||||
params.Path, err = packUserinfo(params.Path, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if params.AsRegexp {
|
||||
params.AsPrefix = false
|
||||
}
|
||||
var pathUsage string
|
||||
switch {
|
||||
case params.AsRegexp:
|
||||
pathUsage = terms.AsRegexp
|
||||
case params.AsPrefix:
|
||||
pathUsage = terms.AsPrefix
|
||||
default:
|
||||
pathUsage = terms.AsFinePath
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
collecions, err := client.NewClient(common.SkipTLSVerify).ListCollections(ctx, params.Path, pathUsage)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Collections = collecions
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteCollection
|
||||
type DeleteCollectionParams struct {
|
||||
Path string
|
||||
Detail bool
|
||||
AsPrefix bool
|
||||
AsRegexp bool
|
||||
DryRun bool
|
||||
}
|
||||
|
||||
type DeleteCollectionResult struct {
|
||||
Files []descr.File `yaml:"files,omitempty"`
|
||||
Filenames []string `yaml:"filenames,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) DeleteCollection(cmd *cobra.Command, args []string) {
|
||||
util.deleteCollectionParams.Path = args[0]
|
||||
res, err := util.deleteCollection(&util.commonFileParams, &util.deleteCollectionParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) deleteCollection(common *CommonFileParams, params *DeleteCollectionParams) (*DeleteCollectionResult, error) {
|
||||
var err error
|
||||
res := &DeleteCollectionResult{
|
||||
Filenames: make([]string, 0),
|
||||
}
|
||||
params.Path, err = packUserinfo(params.Path, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
var pathUsage string
|
||||
switch {
|
||||
case params.AsPrefix:
|
||||
pathUsage = terms.AsPrefix
|
||||
default:
|
||||
pathUsage = terms.AsFinePath
|
||||
}
|
||||
files, err := client.NewClient(common.SkipTLSVerify).DeleteCollection(ctx, params.Path, pathUsage, params.DryRun)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if params.Detail {
|
||||
res.Files = files
|
||||
} else {
|
||||
res.Filenames = makeFilelistFromFiles(files)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func makeFilelistFromFiles(files []descr.File) []string {
|
||||
res := make([]string, 0, len(files))
|
||||
for _, file := range files {
|
||||
res = append(res, filepath.Join(file.Collection, file.Name))
|
||||
}
|
||||
slices.Sort(res)
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* 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 command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mstore/pkg/client"
|
||||
"mstore/pkg/descr"
|
||||
)
|
||||
|
||||
func (util *GrantUtil) CreateGrantCmds() *cobra.Command {
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "grants",
|
||||
Short: "Grant operations",
|
||||
Aliases: []string{"grant"},
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Username, "user", "u", "", "Username")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Password, "pass", "p", "", "Password")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonGrantParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
subCmd.PersistentFlags().BoolVarP(&util.commonGrantParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify")
|
||||
|
||||
vi := viper.New()
|
||||
vi.SetEnvPrefix("mstore")
|
||||
vi.BindEnv("user")
|
||||
vi.BindEnv("pass")
|
||||
util.commonGrantParams.Username = vi.GetString("user")
|
||||
util.commonGrantParams.Password = vi.GetString("pass")
|
||||
|
||||
// CreateGrant
|
||||
var createGrantCmd = &cobra.Command{
|
||||
Use: "create [user:pass@]hostname[:port] username|accountId rigth pattern",
|
||||
Short: "Create grant",
|
||||
Args: cobra.ExactArgs(4),
|
||||
Run: util.CreateGrant,
|
||||
}
|
||||
subCmd.AddCommand(createGrantCmd)
|
||||
|
||||
// GetGrant
|
||||
var getGrantCmd = &cobra.Command{
|
||||
Use: "get [user:pass@]hostname[:port] grantId",
|
||||
Short: "Get detail grant info",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.GetGrant,
|
||||
}
|
||||
subCmd.AddCommand(getGrantCmd)
|
||||
|
||||
// UpdateGrant
|
||||
var updateGrantCmd = &cobra.Command{
|
||||
Use: "update [user:pass@]hostname[:port] gruntId newPattern",
|
||||
Short: "Update grant parameters",
|
||||
Args: cobra.ExactArgs(3),
|
||||
Run: util.UpdateGrant,
|
||||
}
|
||||
subCmd.AddCommand(updateGrantCmd)
|
||||
|
||||
// DeleteGrant
|
||||
var deleteGrantCmd = &cobra.Command{
|
||||
Use: "delete [user:pass@]hostname[:port] gruntId ",
|
||||
Short: "Delete grant",
|
||||
Args: cobra.ExactArgs(2),
|
||||
|
||||
Run: util.DeleteGrant,
|
||||
}
|
||||
subCmd.AddCommand(deleteGrantCmd)
|
||||
|
||||
// ListGrants
|
||||
var listGrantsCmd = &cobra.Command{
|
||||
Use: "list [user:pass@]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")
|
||||
|
||||
subCmd.AddCommand(listGrantsCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
type GrantUtil struct {
|
||||
createGrantParams CreateGrantParams
|
||||
updateGrantParams UpdateGrantParams
|
||||
getGrantParams GetGrantParams
|
||||
deleteGrantParams DeleteGrantParams
|
||||
listGrantsParams ListGrantsParams
|
||||
commonGrantParams CommonGrantParams
|
||||
}
|
||||
|
||||
type CommonGrantParams struct {
|
||||
Username string
|
||||
Password string
|
||||
Hostname string
|
||||
Timeout uint64
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
// CreateGrant
|
||||
type CreateGrantParams struct {
|
||||
AccountID string
|
||||
Right string
|
||||
Pattern string
|
||||
}
|
||||
type CreateGrantResult struct {
|
||||
GrantID string `yaml:"grantId"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGrantParams) (*CreateGrantResult, error) {
|
||||
var err error
|
||||
res := &CreateGrantResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
re := regexp.MustCompile(uuidRegex)
|
||||
id := strings.ToLower(params.AccountID)
|
||||
var operRes string
|
||||
if re.MatchString(id) {
|
||||
operRes, err = client.NewClient(common.SkipTLSVerify).CreateGrantByAccountID(ctx, hostname, id, params.Right, params.Pattern)
|
||||
} else {
|
||||
operRes, err = client.NewClient(common.SkipTLSVerify).CreateGrantByUsername(ctx, hostname, params.AccountID, params.Right, params.Pattern)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.GrantID = operRes
|
||||
return res, err
|
||||
}
|
||||
|
||||
// UpdateGrant
|
||||
type UpdateGrantParams struct {
|
||||
GrantID 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)
|
||||
}
|
||||
|
||||
func (util *GrantUtil) updateGrant(common *CommonGrantParams, params *UpdateGrantParams) (*UpdateGrantResult, error) {
|
||||
var err error
|
||||
res := &UpdateGrantResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
id := strings.ToLower(params.GrantID)
|
||||
err = client.NewClient(common.SkipTLSVerify).UpdateGrant(ctx, hostname, id, params.Pattern)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// GetGrant
|
||||
type GetGrantParams struct {
|
||||
GrantID string
|
||||
}
|
||||
|
||||
type GetGrantResult struct {
|
||||
Grant *descr.Grant `yaml:"grant,omitempty"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (util *GrantUtil) getGrant(common *CommonGrantParams, params *GetGrantParams) (*GetGrantResult, error) {
|
||||
var err error
|
||||
res := &GetGrantResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
opRes := &descr.Grant{}
|
||||
|
||||
id := strings.ToLower(params.GrantID)
|
||||
opRes, err = client.NewClient(common.SkipTLSVerify).GetGrant(ctx, hostname, id)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Grant = opRes
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteGrant
|
||||
type DeleteGrantParams struct {
|
||||
GrantID string
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
func (util *GrantUtil) deleteGrant(common *CommonGrantParams, params *DeleteGrantParams) (*DeleteGrantResult, error) {
|
||||
var err error
|
||||
res := &DeleteGrantResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
id := strings.ToLower(params.GrantID)
|
||||
err = client.NewClient(common.SkipTLSVerify).DeleteGrant(ctx, hostname, id)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListGrants
|
||||
type ListGrantsParams struct {
|
||||
Detail bool
|
||||
AccountID string
|
||||
}
|
||||
|
||||
type ListGrantsResult struct {
|
||||
Grants []descr.Grant `yaml:"grants,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)
|
||||
}
|
||||
func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsParams) (*ListGrantsResult, error) {
|
||||
var err error
|
||||
res := &ListGrantsResult{}
|
||||
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
grants := make([]descr.Grant, 0)
|
||||
re := regexp.MustCompile(uuidRegex)
|
||||
id := strings.ToLower(params.AccountID)
|
||||
if re.MatchString(id) {
|
||||
grants, err = client.NewClient(common.SkipTLSVerify).ListGrantsByAccountID(ctx, hostname, id)
|
||||
} else {
|
||||
grants, err = client.NewClient(common.SkipTLSVerify).ListGrantsByUsername(ctx, hostname, params.AccountID)
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if params.Detail {
|
||||
res.Grants = grants
|
||||
} else {
|
||||
res.Rights = make(map[string]string, 0)
|
||||
for _, item := range grants {
|
||||
res.Rights[item.ID] = item.Right
|
||||
}
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* 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 command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mstore/pkg/client"
|
||||
)
|
||||
|
||||
func packUserinfo(resurseuri, username, password string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
if !strings.Contains(resurseuri, "://") {
|
||||
resurseuri = "https://" + resurseuri
|
||||
}
|
||||
uri, err := url.Parse(resurseuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
uri.Path = path.Clean(uri.Path)
|
||||
if username != "" && password != "" {
|
||||
uri.User = url.UserPassword(username, password)
|
||||
}
|
||||
res = uri.String()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (util *ImageUtil) CreateImageCmds() *cobra.Command {
|
||||
const defaultTimeout uint64 = 30 // Second
|
||||
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "images",
|
||||
Short: "Image operations",
|
||||
Aliases: []string{"image"},
|
||||
}
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonImageParams.Username, "user", "u", "", "Username")
|
||||
subCmd.PersistentFlags().StringVarP(&util.commonImageParams.Password, "pass", "p", "", "Password")
|
||||
subCmd.PersistentFlags().Uint64VarP(&util.commonImageParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
subCmd.PersistentFlags().BoolVarP(&util.commonImageParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify")
|
||||
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||
|
||||
vi := viper.New()
|
||||
vi.SetEnvPrefix("mstore")
|
||||
vi.BindEnv("user")
|
||||
vi.BindEnv("pass")
|
||||
util.commonImageParams.Username = vi.GetString("user")
|
||||
util.commonImageParams.Password = vi.GetString("pass")
|
||||
|
||||
// PushImage
|
||||
var pushImageCmd = &cobra.Command{
|
||||
Use: "push filename [user:pass@]hostname[:port]/path:tag",
|
||||
Short: "Push container image from local tar file into registry",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.PushImage,
|
||||
}
|
||||
subCmd.AddCommand(pushImageCmd)
|
||||
|
||||
// ImageInfo
|
||||
var imageInfoCmd = &cobra.Command{
|
||||
Use: "info [user:pass@]hostname[:port]/path:tag",
|
||||
Short: "Show container image info",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.ImageInfo,
|
||||
}
|
||||
subCmd.AddCommand(imageInfoCmd)
|
||||
|
||||
// PullImage
|
||||
var pullImageCmd = &cobra.Command{
|
||||
Use: "pull [user:pass@]hostname[:port]/path:tag filename",
|
||||
Short: "Pull container image from registry into local file",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.PullImage,
|
||||
}
|
||||
subCmd.AddCommand(pullImageCmd)
|
||||
|
||||
// DeleteImage
|
||||
var deleteImageCmd = &cobra.Command{
|
||||
Use: "delete [user:pass@]hostname[:port]/path:tag",
|
||||
Short: "Delete container image from registry",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.DeleteImage,
|
||||
}
|
||||
subCmd.AddCommand(deleteImageCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
type ImageUtil struct {
|
||||
imageInfoParams ImageInfoParams
|
||||
pullImageParams PullImageParams
|
||||
pushImageParams PushImageParams
|
||||
deleteImageParams DeleteImageParams
|
||||
commonImageParams CommonImageParams
|
||||
}
|
||||
|
||||
type CommonImageParams struct {
|
||||
Timeout uint64
|
||||
Username string
|
||||
Password string
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
// PushImage
|
||||
type PushImageParams struct {
|
||||
Imagepath string
|
||||
Filepath string
|
||||
}
|
||||
|
||||
type PushImageResult struct{}
|
||||
|
||||
func (util *ImageUtil) PushImage(cmd *cobra.Command, args []string) {
|
||||
util.pushImageParams.Filepath = args[0]
|
||||
util.pushImageParams.Imagepath = args[1]
|
||||
|
||||
res, err := util.pushImage(&util.commonImageParams, &util.pushImageParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *ImageUtil) pushImage(common *CommonImageParams, params *PushImageParams) (*PushImageResult, error) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
res := &PushImageResult{}
|
||||
|
||||
cli := client.NewClient(common.SkipTLSVerify)
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
err = cli.PushImage(ctx, params.Filepath, params.Imagepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ImageInfo
|
||||
type ImageInfoParams struct {
|
||||
Imagepath string
|
||||
}
|
||||
|
||||
type ImageInfoResult struct {
|
||||
ImageInfo *client.ImageDescr `yaml:"imageInfo"`
|
||||
}
|
||||
|
||||
func (util *ImageUtil) ImageInfo(cmd *cobra.Command, args []string) {
|
||||
util.imageInfoParams.Imagepath = args[0]
|
||||
res, err := util.imageInfo(&util.commonImageParams, &util.imageInfoParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *ImageUtil) imageInfo(common *CommonImageParams, params *ImageInfoParams) (*ImageInfoResult, error) {
|
||||
var err error
|
||||
res := &ImageInfoResult{}
|
||||
ctx := context.Background()
|
||||
|
||||
cli := client.NewClient(common.SkipTLSVerify)
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
|
||||
params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
opres, err := cli.ImageInfo(ctx, params.Imagepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.ImageInfo = opres
|
||||
return res, err
|
||||
}
|
||||
|
||||
// PullImage
|
||||
type PullImageParams struct {
|
||||
Imagepath string
|
||||
Filepath string
|
||||
}
|
||||
|
||||
type PullImageResult struct {
|
||||
Filepath string `yaml:"filepath"`
|
||||
Size int64 `yaml:"size"`
|
||||
}
|
||||
|
||||
func (util *ImageUtil) PullImage(cmd *cobra.Command, args []string) {
|
||||
util.pullImageParams.Imagepath = args[0]
|
||||
util.pullImageParams.Filepath = args[1]
|
||||
res, err := util.pullImage(&util.commonImageParams, &util.pullImageParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *ImageUtil) pullImage(common *CommonImageParams, params *PullImageParams) (*PullImageResult, error) {
|
||||
var err error
|
||||
|
||||
ctx := context.Background()
|
||||
res := &PullImageResult{}
|
||||
|
||||
cli := client.NewClient(common.SkipTLSVerify)
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
err = cli.PullImage(ctx, params.Imagepath, params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
filestat, err := os.Stat(params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Size = filestat.Size()
|
||||
res.Filepath = params.Filepath
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteImage
|
||||
type DeleteImageParams struct {
|
||||
Imagepath string
|
||||
}
|
||||
|
||||
type DeleteImageResult struct {
|
||||
}
|
||||
|
||||
func (util *ImageUtil) DeleteImage(cmd *cobra.Command, args []string) {
|
||||
util.deleteImageParams.Imagepath = args[0]
|
||||
res, err := util.deleteImage(&util.commonImageParams, &util.deleteImageParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *ImageUtil) deleteImage(common *CommonImageParams, params *DeleteImageParams) (*DeleteImageResult, error) {
|
||||
var err error
|
||||
res := &DeleteImageResult{}
|
||||
ctx := context.Background()
|
||||
|
||||
cli := client.NewClient(common.SkipTLSVerify)
|
||||
timeout := time.Duration(common.Timeout) * time.Second
|
||||
|
||||
params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
err = cli.DeleteImage(ctx, params.Imagepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
yaml "go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
type Util struct {
|
||||
FileUtil
|
||||
ImageUtil
|
||||
AccountUtil
|
||||
GrantUtil
|
||||
rootCmd *cobra.Command
|
||||
}
|
||||
|
||||
func NewUtil() *Util {
|
||||
return &Util{}
|
||||
}
|
||||
|
||||
func (util *Util) GetRooCmd() *cobra.Command {
|
||||
return util.rootCmd
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
rootCmd.AddCommand(util.CreateFileCmds())
|
||||
rootCmd.AddCommand(util.CreateCollectionCmds())
|
||||
rootCmd.AddCommand(util.CreateImageCmds())
|
||||
rootCmd.AddCommand(util.CreateAccountCmds())
|
||||
rootCmd.AddCommand(util.CreateGrantCmds())
|
||||
|
||||
util.rootCmd = rootCmd
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (util *Util) Exec(args []string) error {
|
||||
var err error
|
||||
util.rootCmd.SetArgs(args)
|
||||
err = util.rootCmd.Execute()
|
||||
return err
|
||||
}
|
||||
|
||||
func (util *Util) Hello(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("hello, world!")
|
||||
}
|
||||
|
||||
func printResponse(res any, err error) {
|
||||
type Response struct {
|
||||
Error bool `yaml:"error" yaml:"error"`
|
||||
Message string `yaml:"message,omitempty" yaml:"message,omitempty"`
|
||||
Result any `yaml:"result,omitempty" yaml:"result,omitempty"`
|
||||
}
|
||||
resp := Response{}
|
||||
if err != nil {
|
||||
resp.Error = true
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
resp.Result = res
|
||||
}
|
||||
respBytes, _ := yaml.Marshal(resp)
|
||||
fmt.Printf("---\n%s\n", string(respBytes))
|
||||
}
|
||||
Reference in New Issue
Block a user