-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
33 lines (26 loc) · 878 Bytes
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import datetime
import os
import click
from werkzeug.utils import secure_filename
POST_TEMPLATE = """---
title: {title}
date: {date}
tags:
-
description:
---
"""
@click.command()
@click.option("--title", prompt="Enter the title of the new post", help="Title of the new post")
def create_new_post(title):
date = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=-5))).isoformat()
filename = secure_filename(title).replace("_", "-").lower() + ".md"
filepath = os.path.join("posts", filename)
if os.path.exists(filepath):
click.echo("A post with the same title already exists. Please choose a different title.")
return
with open(filepath, "w") as file:
file.write(POST_TEMPLATE.format(title=title, date=date))
click.echo(f"New post created at {filepath}")
if __name__ == "__main__":
create_new_post()