8ce9e09e69
Decouple CLI commands from internal helper logic and consolidate the core file transfer operations in the service layer to improve library portability. - Make MdrsClient subclass MdrsService to inherit resource resolution. - Remove all deprecated helper methods from BaseCommand. - Move core upload and download logic to a new transfer module. - Refactor all CLI commands to route actions through MdrsClient. - Eliminate circular imports between client and CLI command modules.
40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
from argparse import Namespace
|
|
from typing import Any
|
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
|
|
class UploadCommand(BaseCommand):
|
|
@classmethod
|
|
def register(cls, parsers: Any) -> None:
|
|
upload_parser = parsers.add_parser("upload", help="upload the file or directory")
|
|
upload_parser.add_argument(
|
|
"-r", "--recursive", help="upload directories and their contents recursive", action="store_true"
|
|
)
|
|
upload_parser.add_argument(
|
|
"-s",
|
|
"--skip-if-exists",
|
|
help="skip the upload if file is already uploaded and file size is the same",
|
|
action="store_true",
|
|
)
|
|
upload_parser.add_argument("local_path", help="local file path (/foo/bar/data.txt)")
|
|
upload_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
|
|
upload_parser.set_defaults(func=cls.func)
|
|
|
|
@classmethod
|
|
def func(cls, args: Namespace) -> None:
|
|
local_path = str(args.local_path)
|
|
remote_path = str(args.remote_path)
|
|
is_recursive = bool(args.recursive)
|
|
is_skip_if_exists = bool(args.skip_if_exists)
|
|
cls.upload(local_path, remote_path, is_recursive, is_skip_if_exists)
|
|
|
|
@classmethod
|
|
def upload(cls, local_path: str, remote_path: str, is_recursive: bool, is_skip_if_exists: bool) -> None:
|
|
remote = remote_path.split(":", 1)[0] if ":" in remote_path else ""
|
|
from mdrsclient.client import MdrsClient
|
|
|
|
client = MdrsClient.from_remote(remote)
|
|
client.upload(local_path, remote_path, is_recursive, is_skip_if_exists)
|
|
return
|