Skip to content

Commit

Permalink
🚸 Don't create --local-copy's root dir if it's missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ewen-lbh committed May 8, 2021
1 parent e926727 commit 129a0b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 10 additions & 4 deletions ideaseed/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,17 @@ def run(argv=None):
idea = gkeep.push_to_gkeep(**args) or Idea()

if args["local_copy"] and idea.body:
saved_to = ondisk.save(**args, idea=idea)
if saved_to:
print(f"[dim]Local copy saved to [blue]{saved_to}")
local_copy_dir = Path(args["local_copy"]).expanduser()
if not (local_copy_dir.exists() and local_copy_dir.is_dir()):
print(
f"[red]Given directory for --local-copy ([bold]{local_copy_dir}[/bold]) does not exist or is not a directory"
)
else:
print(f"[yellow]Did not save a local copy")
saved_to = ondisk.save(local_copy=local_copy_dir, idea=idea)
if saved_to:
print(f"[dim]Local copy saved to [blue]{saved_to}")
else:
print(f"[yellow]Did not save a local copy")


def flags_to_args(flags: dict[str, Any]) -> dict[str, Any]:
Expand Down
14 changes: 8 additions & 6 deletions ideaseed/ondisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _header_dict(self) -> dict:
}


def save(local_copy: str, idea: Idea, repo: str, **_) -> str:
def save(local_copy: Path, idea: Idea, repo: str, **_) -> str:
"""
Saves a local copy of the given Idea to the right path
Returns the path where the file was written.
Expand All @@ -69,7 +69,7 @@ def save(local_copy: str, idea: Idea, repo: str, **_) -> str:
root_dir=local_copy, repo_full=repo, title=idea.title, body=idea.body
)
if filepath.exists() and not answered_yes_to(
f"The local copy [bold blue]{filepath.relative_to(Path(local_copy).expanduser())}[/] already exists. Overwrite it?"
f"The local copy [bold blue]{filepath.relative_to(local_copy)}[/] already exists. Overwrite it?"
):
return ""

Expand All @@ -78,15 +78,17 @@ def save(local_copy: str, idea: Idea, repo: str, **_) -> str:
return filepath


def first_line(text: str) -> str:
return text.splitlines()[0]


def get_path(
root_dir: str, repo_full: Optional[str], title: Optional[str], body: str
root_dir: Path, repo_full: Optional[str], title: Optional[str], body: str
) -> Path:
repo_full = repo_full or ""
if "/" in repo_full:
user, repo = repo_full.split("/")
else:
user, repo = "", repo_full

return (
Path(root_dir).expanduser() / user / repo / (slugify(title or body.splitlines()[0]) + ".md")
)
return root_dir / user / repo / slugify(title or first_line(body)) + ".md"

0 comments on commit 129a0b4

Please sign in to comment.