Skip to content

Commit

Permalink
feat(cli): support editing draft in "gas draft"
Browse files Browse the repository at this point in the history
PR Closed: #805
  • Loading branch information
yeyong.yu authored and yuyouyu32 committed Jul 8, 2021
1 parent a4f2b66 commit 70cfba4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion tensorbay/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def dataset(obj: Dict[str, str], tbrn: str, is_delete: bool, yes: bool) -> None:
)
@click.argument("tbrn", type=str)
@click.option("-l", "--list", "is_list", is_flag=True, help="List the drafts.")
@click.option("-e", "--edit", is_flag=True, help="Edit the draft's title and description.")
@click.option(
"-m",
"--message",
Expand All @@ -188,6 +189,7 @@ def draft(
obj: Dict[str, str],
tbrn: str,
is_list: bool,
edit: bool,
message: Tuple[str, ...],
) -> None:
"""List or create drafts.\f
Expand All @@ -196,12 +198,13 @@ def draft(
obj: A dict contains config information.
tbrn: The tbrn of the dataset.
is_list: Whether to list the drafts.
edit: Whether to edit the draft's title and description.
message: The message of the draft.
""" # noqa: D301,D415
from .draft import _implement_draft

_implement_draft(obj, tbrn, is_list, message)
_implement_draft(obj, tbrn, is_list, edit, message)


@command(
Expand Down
29 changes: 27 additions & 2 deletions tensorbay/cli/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def _implement_draft(
obj: Dict[str, str], tbrn: str, is_list: bool, message: Tuple[str, ...]
obj: Dict[str, str], tbrn: str, is_list: bool, edit: bool, message: Tuple[str, ...]
) -> None:
gas = get_gas(**obj)
info = TBRN(tbrn=tbrn)
Expand All @@ -34,6 +34,8 @@ def _implement_draft(

if is_list:
_list_drafts(dataset_client, info)
elif edit:
_edit_draft(dataset_client, info, message)
else:
_create_draft(dataset_client, info, message)

Expand Down Expand Up @@ -83,7 +85,7 @@ def _echo_draft(
try:
branch = dataset_client.get_branch(branch_name)
except ResourceNotExistError:
error('The branch "{branch_name}" does not exist')
error(f'The branch "{branch_name}" does not exist')

if branch.commit_id != ROOT_COMMIT_ID:
click.echo(f"Branch: {branch_name}({branch.commit_id})")
Expand All @@ -95,3 +97,26 @@ def _echo_draft(
click.echo(f"\n {title}\n")
if description:
click.echo(f" {description}\n")


def _edit_draft(dataset_client: DatasetClientType, info: TBRN, message: Tuple[str, ...]) -> None:
if not info.is_draft:
error("Draft number is required when editing draft")

if message:
title, description = message[0], "\n".join(message[1:])
else:
draft = dataset_client.get_draft()
hint = [draft.title]
original_description = draft.description
if original_description:
hint.extend(["", original_description])
hint.append(_DRAFT_HINT)
title, description = edit_input("\n".join(hint))

if not title:
error("Aborting updating draft due to empty draft title")

dataset_client.update_draft(title=title, description=description)
click.echo(f"{info.get_tbrn()} is updated successfully!")
_echo_draft(dataset_client, title, description, dataset_client.status.branch_name)
2 changes: 1 addition & 1 deletion tensorbay/cli/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def edit_input(hint: str) -> Tuple[str, str]:
config_parser = read_config()

editor = config_parser["config"].get("editor") if config_parser.has_section("config") else None
input_info = click.edit(hint, editor=editor)
input_info = click.edit(hint, editor=editor, require_save=False)
return _clean_up(input_info)


Expand Down

0 comments on commit 70cfba4

Please sign in to comment.