Skip to content

Commit ae7a9ae

Browse files
authored
Merge 3d48155 into a4f2b66
2 parents a4f2b66 + 3d48155 commit ae7a9ae

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

tensorbay/cli/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def dataset(obj: Dict[str, str], tbrn: str, is_delete: bool, yes: bool) -> None:
168168
)
169169
@click.argument("tbrn", type=str)
170170
@click.option("-l", "--list", "is_list", is_flag=True, help="List the drafts.")
171+
@click.option("-e", "--edit", is_flag=True, help="Edit the draft's title and description.")
171172
@click.option(
172173
"-m",
173174
"--message",
@@ -188,6 +189,7 @@ def draft(
188189
obj: Dict[str, str],
189190
tbrn: str,
190191
is_list: bool,
192+
edit: bool,
191193
message: Tuple[str, ...],
192194
) -> None:
193195
"""List or create drafts.\f
@@ -196,12 +198,13 @@ def draft(
196198
obj: A dict contains config information.
197199
tbrn: The tbrn of the dataset.
198200
is_list: Whether to list the drafts.
201+
edit: Whether to edit the draft's title and description.
199202
message: The message of the draft.
200203
201204
""" # noqa: D301,D415
202205
from .draft import _implement_draft
203206

204-
_implement_draft(obj, tbrn, is_list, message)
207+
_implement_draft(obj, tbrn, is_list, edit, message)
205208

206209

207210
@command(

tensorbay/cli/draft.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
from .tbrn import TBRN, TBRNType
1616
from .utility import edit_input, error, get_dataset_client, get_gas
1717

18-
_DRAFT_HINT = """
18+
_DRAFT_HINT = """{}
19+
{}
1920
# Please enter the title for your draft.
2021
# Lines starting with '#' will be ignored.
2122
# And an empty draft title aborts the creation.
2223
"""
2324

2425

2526
def _implement_draft(
26-
obj: Dict[str, str], tbrn: str, is_list: bool, message: Tuple[str, ...]
27+
obj: Dict[str, str], tbrn: str, is_list: bool, edit: bool, message: Tuple[str, ...]
2728
) -> None:
2829
gas = get_gas(**obj)
2930
info = TBRN(tbrn=tbrn)
@@ -34,6 +35,8 @@ def _implement_draft(
3435

3536
if is_list:
3637
_list_drafts(dataset_client, info)
38+
elif edit:
39+
_edit_draft(dataset_client, info, message)
3740
else:
3841
_create_draft(dataset_client, info, message)
3942

@@ -83,7 +86,7 @@ def _echo_draft(
8386
try:
8487
branch = dataset_client.get_branch(branch_name)
8588
except ResourceNotExistError:
86-
error('The branch "{branch_name}" does not exist')
89+
error(f'The branch "{branch_name}" does not exist')
8790

8891
if branch.commit_id != ROOT_COMMIT_ID:
8992
click.echo(f"Branch: {branch_name}({branch.commit_id})")
@@ -95,3 +98,27 @@ def _echo_draft(
9598
click.echo(f"\n {title}\n")
9699
if description:
97100
click.echo(f" {description}\n")
101+
102+
103+
def _edit_draft(dataset_client: DatasetClientType, info: TBRN, message: Tuple[str, ...]) -> None:
104+
if not info.is_draft:
105+
error("Draft number is needed when editing draft")
106+
107+
if message:
108+
title, description = message[0], "\n".join(message[1:])
109+
else:
110+
draft = dataset_client.get_draft(draft_number=info.draft_number)
111+
title, description = edit_input(
112+
_DRAFT_HINT.format(
113+
draft.title, "\n" + draft.description + "\n" if draft.description else ""
114+
)
115+
)
116+
117+
if not title:
118+
error("Aborting creating draft due to empty draft title")
119+
120+
dataset_client.update_draft(
121+
draft_number=info.draft_number, title=title, description=description
122+
)
123+
click.echo(f"{info.get_tbrn()} update successful!")
124+
_echo_draft(dataset_client, title, description, dataset_client.status.branch_name)

tensorbay/cli/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def edit_input(hint: str) -> Tuple[str, str]:
152152
config_parser = read_config()
153153

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

158158

0 commit comments

Comments
 (0)