290 lines
8.4 KiB
Go
290 lines
8.4 KiB
Go
/*
|
|
* 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"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"mstore/app/descr"
|
|
"mstore/pkg/client"
|
|
)
|
|
|
|
func (util *FileUtil) CreateFileCmds() *cobra.Command {
|
|
var subCmd = &cobra.Command{
|
|
Use: "file",
|
|
Short: "File operation",
|
|
}
|
|
const defaultTimeout uint64 = 10
|
|
// PutFile
|
|
var putFileCmd = &cobra.Command{
|
|
Use: "put",
|
|
Short: "Put file to storage",
|
|
Run: util.PutFile,
|
|
}
|
|
putFileCmd.Flags().StringVarP(&util.putFileParams.Username, "user", "u", "", "Username")
|
|
putFileCmd.Flags().StringVarP(&util.putFileParams.Password, "pass", "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")
|
|
putFileCmd.MarkFlagsRequiredTogether("src", "dest")
|
|
putFileCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
putFileCmd.MarkFlagFilename("src")
|
|
|
|
subCmd.AddCommand(putFileCmd)
|
|
|
|
// GetFile
|
|
var getFileCmd = &cobra.Command{
|
|
Use: "get",
|
|
Short: "Get file from storage",
|
|
Run: util.GetFile,
|
|
}
|
|
getFileCmd.Flags().StringVarP(&util.getFileParams.Username, "user", "u", "", "Username")
|
|
getFileCmd.Flags().StringVarP(&util.getFileParams.Password, "pass", "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")
|
|
getFileCmd.MarkFlagsRequiredTogether("src", "dest")
|
|
getFileCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
|
|
subCmd.AddCommand(getFileCmd)
|
|
|
|
// FileInfo
|
|
var fileInfoCmd = &cobra.Command{
|
|
Use: "info",
|
|
Short: "Show file information",
|
|
Run: util.FileInfo,
|
|
}
|
|
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Username, "user", "u", "", "Username")
|
|
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Password, "pass", "p", "", "Password")
|
|
fileInfoCmd.Flags().StringVarP(&util.fileInfoParams.Filepath, "path", "d", "", "File path")
|
|
fileInfoCmd.Flags().Uint64VarP(&util.fileInfoParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
|
fileInfoCmd.MarkFlagRequired("path")
|
|
fileInfoCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
|
|
subCmd.AddCommand(fileInfoCmd)
|
|
|
|
// DeleteFile
|
|
var deleteFileCmd = &cobra.Command{
|
|
Use: "delete",
|
|
Short: "Delete file in storage",
|
|
Run: util.DeleteFile,
|
|
}
|
|
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Username, "user", "u", "", "Username")
|
|
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Password, "pass", "p", "", "Password")
|
|
deleteFileCmd.Flags().StringVarP(&util.deleteFileParams.Filepath, "path", "d", "", "File path")
|
|
deleteFileCmd.Flags().Uint64VarP(&util.deleteFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
|
deleteFileCmd.MarkFlagRequired("path")
|
|
deleteFileCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
|
|
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")
|
|
listFilesCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
|
|
subCmd.AddCommand(listFilesCmd)
|
|
|
|
return subCmd
|
|
}
|
|
|
|
type FileUtil struct {
|
|
fileInfoParams FileInfoParams
|
|
putFileParams PutFileParams
|
|
getFileParams GetFileParams
|
|
deleteFileParams DeleteFileParams
|
|
listFilesParams ListFilesParams
|
|
}
|
|
|
|
// FileInfo
|
|
type FileInfoParams struct {
|
|
Filepath string
|
|
Username string
|
|
Password string
|
|
Timeout uint64
|
|
}
|
|
type FileInfoResult struct {
|
|
File *descr.File `json:"file,omitempty"`
|
|
}
|
|
|
|
func (util *FileUtil) FileInfo(cmd *cobra.Command, args []string) {
|
|
res, err := util.fileInfo(&util.fileInfoParams)
|
|
printResponse(res, err)
|
|
}
|
|
|
|
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
|
|
type GetFileParams struct {
|
|
Source string
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|