From c58d3d6fd0a4b9d9117e7e33f39353f63ea12216 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sat, 3 Dec 2022 19:49:57 +0500 Subject: [PATCH] Add "backup" command (#1) --- histd.py | 27 +++++++++++++++++++++++++-- readme.md | 7 +++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/histd.py b/histd.py index abfb3ca..9218b00 100755 --- a/histd.py +++ b/histd.py @@ -2,12 +2,19 @@ from datetime import date import os import subprocess +import sys def main(): base_dir = get_base_dir() today = date.today() - edit_note(base_dir, today) + + if len(sys.argv) == 1: + edit_note(base_dir, today) + elif sys.argv[1] == "backup": + backup(base_dir, today) + else: + print('Command not found') def get_base_dir() -> str: @@ -22,7 +29,7 @@ def get_base_dir() -> str: return base_dir -def edit_note(base_dir: str, note_date: date): +def edit_note(base_dir: str, note_date): """ Creates the required directories and opens a text editor so that the user can describe the day. @@ -46,5 +53,21 @@ def edit_note(base_dir: str, note_date: date): print("Your editor returned non-zero exit code") +def backup(base_dir: str, current_date): + """ + Creates an archive with all notes + """ + date_str = f'{current_date.year}-{current_date.month:02}-{current_date.day:02}' + archive_path = os.path.expanduser(f"~/histd-{date_str}.tar.xz") + cmd = ["tar", "cfJ", archive_path, "."] + try: + subprocess.run(cmd, check=True, cwd=base_dir) + print(f'Saved to {archive_path}') + except FileNotFoundError: + print("Error: I can't find tar program") + except subprocess.CalledProcessError: + print("Archiver returned non-zero exit code") + + if __name__ == '__main__': main() diff --git a/readme.md b/readme.md index a9c2d93..d8dc2d6 100644 --- a/readme.md +++ b/readme.md @@ -34,10 +34,9 @@ sudo curl -o /usr/local/bin/histd "https://raw.githubusercontent.com/ordinary-de To create a new note, you can simply type `histd` in the terminal. ## Backup -To create an archive of all notes, run the following commands: -```sh -cd ~/.local/share -tar -cJf histd.tar.xz histd +To create an archive of all notes, run the following command: +```bash +histd backup ``` ## Merge all notes