working commit
This commit is contained in:
@@ -124,8 +124,8 @@ type CreateAccountParams struct {
|
||||
NewPassword string
|
||||
}
|
||||
type CreateAccountResult struct {
|
||||
AccountID string `json:"accountId"`
|
||||
GrantIDs []string `json:"grantsIds,omitempty"`
|
||||
AccountID string `yaml:"accountId"`
|
||||
GrantIDs []string `yaml:"grantsIds,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) {
|
||||
@@ -179,7 +179,7 @@ type UpdateAccountParams struct {
|
||||
NewPassword string
|
||||
}
|
||||
type UpdateAccountResult struct {
|
||||
File *descr.File `json:"file,omitempty"`
|
||||
File *descr.File `yaml:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||
@@ -224,7 +224,7 @@ func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
type GetAccountResult struct {
|
||||
Account *descr.AccountShort `json:"account,omitempty"`
|
||||
Account *descr.AccountShort `yaml:"account,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAccountParams) (*GetAccountResult, error) {
|
||||
@@ -300,13 +300,13 @@ type ListAccountsParams struct {
|
||||
}
|
||||
|
||||
type Userinfo struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
Rights []string `json:"rights,omitempty"`
|
||||
Username string `yaml:"username,omitempty"`
|
||||
Rights []string `yaml:"rights,omitempty"`
|
||||
}
|
||||
|
||||
type ListAccountsResult struct {
|
||||
Accounts []descr.AccountShort `json:"accounts,omitempty"`
|
||||
Users []Userinfo `json:"users,omitempty"`
|
||||
Accounts []descr.AccountShort `yaml:"accounts,omitempty"`
|
||||
Users []Userinfo `yaml:"users,omitempty"`
|
||||
}
|
||||
|
||||
func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) {
|
||||
|
||||
+110
-16
@@ -12,6 +12,9 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -41,8 +44,8 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Short: "Put file to storage",
|
||||
Run: util.PutFile,
|
||||
}
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Source, "src", "s", "", "Source path")
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Source, "src", "S", "", "Source path")
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "D", "", "Desctination path")
|
||||
putFileCmd.MarkFlagsRequiredTogether("src", "dest")
|
||||
putFileCmd.MarkFlagFilename("src")
|
||||
|
||||
@@ -55,8 +58,8 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Short: "Get file from storage",
|
||||
Run: util.GetFile,
|
||||
}
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Source, "src", "s", "", "Source path")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Source, "src", "S", "", "Source path")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Dest, "dest", "D", "", "Desctination path")
|
||||
getFileCmd.MarkFlagsRequiredTogether("src", "dest")
|
||||
|
||||
subCmd.AddCommand(getFileCmd)
|
||||
@@ -67,7 +70,7 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Short: "Show file information",
|
||||
Run: util.FileInfo,
|
||||
}
|
||||
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Filepath, "path", "d", "", "File path")
|
||||
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Filepath, "path", "P", "", "File path")
|
||||
fileInfoCmd.MarkFlagRequired("path")
|
||||
|
||||
subCmd.AddCommand(fileInfoCmd)
|
||||
@@ -79,7 +82,9 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Short: "Delete file in storage",
|
||||
Run: util.DeleteFile,
|
||||
}
|
||||
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Filepath, "path", "d", "", "File path")
|
||||
|
||||
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Filepath, "path", "P", "", "File path")
|
||||
|
||||
deleteFileCmd.MarkFlagRequired("path")
|
||||
subCmd.AddCommand(deleteFileCmd)
|
||||
|
||||
@@ -89,20 +94,34 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Short: "List file in storage",
|
||||
Run: util.ListFiles,
|
||||
}
|
||||
listFilesCmd.Flags().StringVarP(&util.listFilesParams.Filepath, "catalog", "c", "", "Catalog path")
|
||||
listFilesCmd.Flags().StringVarP(&util.listFilesParams.Filepath, "catalog", "C", "", "Catalog path")
|
||||
listFilesCmd.Flags().BoolVarP(&util.listFilesParams.Detail, "detail", "D", false, "Show detail file information")
|
||||
listFilesCmd.MarkFlagRequired("catalog")
|
||||
subCmd.AddCommand(listFilesCmd)
|
||||
|
||||
// ImportFiles
|
||||
var importFilesCmd = &cobra.Command{
|
||||
Use: "import",
|
||||
Short: "Send file tree to storage as is",
|
||||
Run: util.ImportFiles,
|
||||
}
|
||||
importFilesCmd.Flags().StringVarP(&util.importFilesParams.Source, "src", "S", "", "Source base path")
|
||||
importFilesCmd.Flags().StringVarP(&util.importFilesParams.Dest, "dest", "D", "", "Desctination base path")
|
||||
importFilesCmd.MarkFlagsRequiredTogether("src", "dest")
|
||||
|
||||
subCmd.AddCommand(importFilesCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
type FileUtil struct {
|
||||
fileInfoParams FileInfoParams
|
||||
putFileParams PutFileParams
|
||||
getFileParams GetFileParams
|
||||
deleteFileParams DeleteFileParams
|
||||
listFilesParams ListFilesParams
|
||||
commonFileParams CommonFileParams
|
||||
fileInfoParams FileInfoParams
|
||||
putFileParams PutFileParams
|
||||
getFileParams GetFileParams
|
||||
deleteFileParams DeleteFileParams
|
||||
listFilesParams ListFilesParams
|
||||
importFilesParams ImportFilesParams
|
||||
commonFileParams CommonFileParams
|
||||
}
|
||||
|
||||
type CommonFileParams struct {
|
||||
@@ -116,7 +135,7 @@ type FileInfoParams struct {
|
||||
Filepath string
|
||||
}
|
||||
type FileInfoResult struct {
|
||||
File *descr.File `json:"file,omitempty"`
|
||||
File *descr.File `yaml:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) FileInfo(cmd *cobra.Command, args []string) {
|
||||
@@ -232,10 +251,12 @@ func (util *FileUtil) deleteFile(common *CommonFileParams, params *DeleteFilePar
|
||||
// ListFiles
|
||||
type ListFilesParams struct {
|
||||
Filepath string
|
||||
Detail bool
|
||||
}
|
||||
|
||||
type ListFilesResult struct {
|
||||
Files []descr.File `json:"files"`
|
||||
Files []descr.File `yaml:"files,omitempty"`
|
||||
Filenames []string `yaml:"filenames,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ListFiles(cmd *cobra.Command, args []string) {
|
||||
@@ -255,6 +276,79 @@ func (util *FileUtil) listFiles(common *CommonFileParams, params *ListFilesParam
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Files = files
|
||||
|
||||
if params.Detail {
|
||||
res.Files = files
|
||||
} else {
|
||||
for _, file := range files {
|
||||
filename := filepath.Join("/", file.Collection, file.Name)
|
||||
res.Filenames = append(res.Filenames, filename)
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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)
|
||||
dest, _ := url.JoinPath(params.Dest, walkPath)
|
||||
if err != nil {
|
||||
putErrors = append(putErrors, err)
|
||||
return nil
|
||||
}
|
||||
err = client.NewClient().PutFile(ctx, walkPath, dest)
|
||||
if err != nil {
|
||||
putErrors = append(putErrors, err)
|
||||
fmt.Printf("- %s: error\n", walkPath)
|
||||
} else {
|
||||
res.Files = append(res.Files, walkPath)
|
||||
fmt.Printf("- %s: ok\n", walkPath)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err = filepath.Walk(params.Source, walcFunc)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ type CreateGrantParams struct {
|
||||
Pattern string
|
||||
}
|
||||
type CreateGrantResult struct {
|
||||
GrantID string `json:"grantId"`
|
||||
GrantID string `yaml:"grantId"`
|
||||
}
|
||||
|
||||
func (util *GrantUtil) CreateGrant(cmd *cobra.Command, args []string) {
|
||||
@@ -184,7 +184,7 @@ type GetGrantParams struct {
|
||||
}
|
||||
|
||||
type GetGrantResult struct {
|
||||
Grant *descr.Grant `json:"grant,omitempty"`
|
||||
Grant *descr.Grant `yaml:"grant,omitempty"`
|
||||
}
|
||||
|
||||
func (util *GrantUtil) GetGrant(cmd *cobra.Command, args []string) {
|
||||
@@ -247,8 +247,8 @@ type ListGrantsParams struct {
|
||||
}
|
||||
|
||||
type ListGrantsResult struct {
|
||||
Grants []descr.Grant `json:"grants,omitempty"`
|
||||
Rights []string `json:"rights,omitempty"`
|
||||
Grants []descr.Grant `yaml:"grants,omitempty"`
|
||||
Rights []string `yaml:"rights,omitempty"`
|
||||
}
|
||||
|
||||
func (util *GrantUtil) ListGrants(cmd *cobra.Command, args []string) {
|
||||
|
||||
@@ -155,7 +155,7 @@ type ImageInfoParams struct {
|
||||
}
|
||||
|
||||
type ImageInfoResult struct {
|
||||
ImageInfo *client.ImageDescr `json:"imageInfo"`
|
||||
ImageInfo *client.ImageDescr `yaml:"imageInfo"`
|
||||
}
|
||||
|
||||
func (util *ImageUtil) ImageInfo(cmd *cobra.Command, args []string) {
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/yaml"
|
||||
yaml "go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -76,9 +76,9 @@ func (util *Util) Hello(cmd *cobra.Command, args []string) {
|
||||
|
||||
func printResponse(res any, err error) {
|
||||
type Response struct {
|
||||
Error bool `json:"error" yaml:"error"`
|
||||
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
||||
Result any `json:"result,omitempty" yaml:"result,omitempty"`
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user