Skip to content
Snippets Groups Projects
Commit ca30b6bb authored by Jasper Clemens Gräflich's avatar Jasper Clemens Gräflich
Browse files

Add overwrite protection and --force flag

Also switch to argparse for correct parsing of arguments and help
parent b5aa1ebe
No related branches found
No related tags found
No related merge requests found
Pipeline #86562 passed
...@@ -5,10 +5,13 @@ Usage: The script can be used with or without arguments. ...@@ -5,10 +5,13 @@ Usage: The script can be used with or without arguments.
./journal_entry.py New entry for today ./journal_entry.py New entry for today
./journal_entry.py "YYYY-MM-DD" New entry for specified date ./journal_entry.py "YYYY-MM-DD" New entry for specified date
If the supplied date already has an entry, the program will be aborted,except if supplied with the "--force" flag.
Note that a new entry always points to the newest previously created Note that a new entry always points to the newest previously created
entry. entry. Inserting an entry anywhere but at the end is not supported.
""" """
import argparse
import glob import glob
import os import os
import re import re
...@@ -21,24 +24,46 @@ def main(): ...@@ -21,24 +24,46 @@ def main():
reponame = os.path.basename(os.getcwd()) reponame = os.path.basename(os.getcwd())
wikifolder = "../" + reponame + ".wiki" wikifolder = "../" + reponame + ".wiki"
if sys.argv[1:]: parser = argparse.ArgumentParser(
new_date = datetime.strptime(sys.argv[1], "%Y-%m-%d").date() description="Add a new journal entry to the wiki.\n\nNote that a new entry always points to the newest previously created entry. Inserting an entry anywhere but at the end is not supported."
else: )
new_date = date.today() parser.add_argument(
"date",
help='create entry for specified date, in "YYYY-MM-DD" format, default being the current date',
nargs="?",
default=f"{date.today()}",
)
parser.add_argument(
"-f",
"--force",
help="force overwrite if entry already exists",
action="store_true",
)
args = parser.parse_args()
# Parse the date argument
new_date = datetime.strptime(args.date, "%Y-%m-%d").date()
os.chdir(wikifolder) os.chdir(wikifolder)
os.system("git pull") os.system("git pull")
update(new_date) update(new_date, args.force)
os.system("git add .") os.system("git add .")
os.system(f"git commit -m 'Create journal entry for {new_date}'") os.system(f"git commit -m 'Create journal entry for {new_date}'")
os.system("git push") os.system("git push")
def update(new_date): def update(new_date, forced):
"""Update previous entry, MoC, and _sidebar, and create new entry.""" """Update previous entry, MoC, and _sidebar, and create new entry."""
# Read previous entry # Read previous entry
previous = glob.glob("./journal/2*")[-1] previous = glob.glob("./journal/2*")[-1]
# Check if the file for `new_date` already exists
if previous == f"./journal/{new_date}.md" and not forced:
sys.exit(
f"Error: file journal/{new_date}.md already exists. Use `--force` to overwrite"
)
with open(previous, "r") as file: with open(previous, "r") as file:
lines = file.readlines() lines = file.readlines()
lines[2] = re.sub(r"next", r"[next](" + f"journal/{new_date}" + ")", lines[2]) lines[2] = re.sub(r"next", r"[next](" + f"journal/{new_date}" + ")", lines[2])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment