working commit
This commit is contained in:
+174
-16
@@ -10,7 +10,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/client"
|
||||
)
|
||||
|
||||
func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
@@ -18,6 +25,7 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
Use: "file",
|
||||
Short: "File operation",
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
// PutFile
|
||||
var putFileCmd = &cobra.Command{
|
||||
Use: "put",
|
||||
@@ -28,6 +36,7 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Password, "password", "p", "", "Password")
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Source, "src", "s", "", "Source path")
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
putFileCmd.Flags().Uint64VarP(&util.putFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
|
||||
subCmd.AddCommand(putFileCmd)
|
||||
|
||||
@@ -41,22 +50,24 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Password, "password", "p", "", "Password")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Source, "src", "s", "", "Source path")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
getFileCmd.Flags().Uint64VarP(&util.getFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
|
||||
subCmd.AddCommand(getFileCmd)
|
||||
|
||||
// FileExists
|
||||
var fileExistsCmd = &cobra.Command{
|
||||
Use: "exist",
|
||||
Short: "Check file into storage",
|
||||
Run: util.FileExists,
|
||||
// FileInfo
|
||||
var fileInfoCmd = &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Show file information",
|
||||
Run: util.FileInfo,
|
||||
}
|
||||
fileExistsCmd.Flags().StringVarP(&util.fileExistsParams.Username, "username", "u", "", "Username")
|
||||
fileExistsCmd.Flags().StringVarP(&util.fileExistsParams.Password, "password", "p", "", "Password")
|
||||
fileExistsCmd.Flags().StringVarP(&util.fileExistsParams.Filepath, "path", "d", "", "File path")
|
||||
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Username, "username", "u", "", "Username")
|
||||
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Password, "password", "p", "", "Password")
|
||||
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Filepath, "path", "d", "", "File path")
|
||||
fileInfoCmd.Flags().Uint64VarP(&util.fileInfoParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
|
||||
subCmd.AddCommand(fileExistsCmd)
|
||||
subCmd.AddCommand(fileInfoCmd)
|
||||
|
||||
// FileExists
|
||||
// DeleteFile
|
||||
var deleteFileCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete file in storage",
|
||||
@@ -65,38 +76,111 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
||||
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Username, "username", "u", "", "Username")
|
||||
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Password, "password", "p", "", "Password")
|
||||
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Filepath, "path", "d", "", "File path")
|
||||
deleteFileCmd.Flags().Uint64VarP(&util.deleteFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
|
||||
subCmd.AddCommand(deleteFileCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
func (util *FileUtil) CreateFilesCmds() *cobra.Command {
|
||||
var subCmd = &cobra.Command{
|
||||
Use: "files",
|
||||
Short: "Files operation",
|
||||
}
|
||||
const defaultTimeout uint64 = 10
|
||||
|
||||
// ListFiles
|
||||
var listFilesCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List file in storage",
|
||||
Run: util.ListFiles,
|
||||
}
|
||||
listFilesCmd.Flags().StringVarP(&util.listFilesParams.Username, "username", "u", "", "Username")
|
||||
listFilesCmd.Flags().StringVarP(&util.listFilesParams.Password, "password", "p", "", "Password")
|
||||
listFilesCmd.Flags().StringVarP(&util.listFilesParams.Filepath, "catalog", "c", "", "Catalog path")
|
||||
listFilesCmd.Flags().Uint64VarP(&util.listFilesParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
||||
listFilesCmd.MarkFlagRequired("catalog")
|
||||
|
||||
subCmd.AddCommand(listFilesCmd)
|
||||
|
||||
return subCmd
|
||||
}
|
||||
|
||||
type FileUtil struct {
|
||||
fileExistsParams FileExistsParams
|
||||
fileInfoParams FileInfoParams
|
||||
putFileParams PutFileParams
|
||||
getFileParams GetFileParams
|
||||
deleteFileParams DeleteFileParams
|
||||
listFilesParams ListFilesParams
|
||||
}
|
||||
|
||||
// File exists
|
||||
type FileExistsParams struct {
|
||||
// FileInfo
|
||||
type FileInfoParams struct {
|
||||
Filepath string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
}
|
||||
type FileInfoResult struct {
|
||||
File *descr.File `json:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) FileExists(cmd *cobra.Command, args []string) {
|
||||
func (util *FileUtil) FileInfo(cmd *cobra.Command, args []string) {
|
||||
res, err := util.fileInfo(&util.fileInfoParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
// Put file
|
||||
func (util *FileUtil) fileInfo(params *FileInfoParams) (*FileInfoResult, error) {
|
||||
var err error
|
||||
res := &FileInfoResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, params.Username, params.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
exists, opres, err := client.NewClient().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
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
}
|
||||
type PutFileResult struct{}
|
||||
|
||||
func (util *FileUtil) PutFile(cmd *cobra.Command, args []string) {
|
||||
res, err := util.putFile(&util.putFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (util *FileUtil) putFile(params *PutFileParams) (*PutFileResult, error) {
|
||||
var err error
|
||||
res := &PutFileResult{}
|
||||
params.Dest, err = packUserinfo(params.Dest, 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().PutFile(ctx, params.Source, params.Dest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Get file
|
||||
@@ -105,17 +189,91 @@ type GetFileParams struct {
|
||||
Dest string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
}
|
||||
|
||||
func (util *FileUtil) GetFile(cmd *cobra.Command, args []string) {
|
||||
res, err := util.getFile(&util.getFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
// Delete file
|
||||
type GetFileResult struct{}
|
||||
|
||||
func (util *FileUtil) getFile(params *GetFileParams) (*GetFileResult, error) {
|
||||
var err error
|
||||
res := &GetFileResult{}
|
||||
params.Dest, err = packUserinfo(params.Source, 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().GetFile(ctx, params.Dest, params.Source)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// DeleteFile
|
||||
type DeleteFileParams struct {
|
||||
Filepath string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
}
|
||||
|
||||
type DeleteFileResult struct{}
|
||||
|
||||
func (util *FileUtil) DeleteFile(cmd *cobra.Command, args []string) {
|
||||
res, err := util.deleteFile(&util.deleteFileParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
func (util *FileUtil) deleteFile(params *DeleteFileParams) (*DeleteFileResult, error) {
|
||||
var err error
|
||||
res := &DeleteFileResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, 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().DeleteFile(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ListFiles
|
||||
type ListFilesParams struct {
|
||||
Filepath string
|
||||
Username string
|
||||
Password string
|
||||
Timeout uint64
|
||||
}
|
||||
|
||||
type ListFilesResult struct {
|
||||
Files []descr.File `json:"files"`
|
||||
}
|
||||
|
||||
func (util *FileUtil) ListFiles(cmd *cobra.Command, args []string) {
|
||||
res, err := util.listFiles(&util.listFilesParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
func (util *FileUtil) listFiles(params *ListFilesParams) (*ListFilesResult, error) {
|
||||
var err error
|
||||
res := &ListFilesResult{}
|
||||
params.Filepath, err = packUserinfo(params.Filepath, params.Username, params.Password)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
timeout := time.Duration(params.Timeout) * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), timeout)
|
||||
files, err := client.NewClient().ListFiles(ctx, params.Filepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Files = files
|
||||
return res, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user