40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
|
import os
|
||
|
from argparse import Namespace, _SubParsersAction
|
||
|
|
||
|
from mdrsclient.api import FileApi, FolderApi
|
||
|
from mdrsclient.commands.base import BaseCommand
|
||
|
from mdrsclient.exceptions import IllegalArgumentException
|
||
|
|
||
|
|
||
|
class RmCommand(BaseCommand):
|
||
|
@classmethod
|
||
|
def register(cls, parsers: _SubParsersAction) -> None:
|
||
|
command = cls()
|
||
|
rm_parser = parsers.add_parser("rm", help="remove the file or folder")
|
||
|
rm_parser.add_argument(
|
||
|
"-r", "--recursive", help="remove folders and their contents recursive", action="store_true"
|
||
|
)
|
||
|
rm_parser.add_argument("remote_path", help="remote file path (remote:/lab/path/file)")
|
||
|
rm_parser.set_defaults(func=command.rm)
|
||
|
|
||
|
def rm(self, args: Namespace) -> None:
|
||
|
(remote, laboratory_name, r_path) = self._parse_remote_host_with_path(args.remote_path)
|
||
|
r_path = r_path.rstrip("/")
|
||
|
r_dirname = os.path.dirname(r_path)
|
||
|
r_basename = os.path.basename(r_path)
|
||
|
connection = self._create_connection(remote)
|
||
|
laboratory = self._find_laboratory(connection, laboratory_name)
|
||
|
parent_folder = self._find_folder(connection, laboratory, r_dirname)
|
||
|
file = parent_folder.find_file(r_basename)
|
||
|
if file is not None:
|
||
|
file_api = FileApi(connection)
|
||
|
file_api.destroy(file)
|
||
|
else:
|
||
|
folder = parent_folder.find_sub_folder(r_basename)
|
||
|
if folder is None:
|
||
|
raise IllegalArgumentException(f"Cannot remove `{r_path}`: No such file or folder.")
|
||
|
if not args.recursive:
|
||
|
raise IllegalArgumentException(f"Cannot remove `{r_path}`: Is a folder.")
|
||
|
folder_api = FolderApi(connection)
|
||
|
folder_api.destroy(folder.id)
|