Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell hooks #190

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Equally, the only thing flatnotes caches is the search index and that's incremen
* Light/dark themes.
* Multiple authentication options (none, read-only, username/password, 2FA).
* Restful API.
* Hook on create/update/delete.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend a new section to provide a demo like you did in the PR.

Alternative suggestion for git, sample. 100% Untested :-) I used this technique in some of my (shell) scripts I removed the git add -A to also handle deletes and possible renames (rename detection can be a little hit and miss)

FLATNOTES_HOOK_CREATE="git add -u :/ ; git add . && git commit -m 'note %s' && git push"
FLATNOTES_HOOK_UPDATE="$FLATNOTES_HOOK_CREATE"
FLATNOTES_HOOK_DELETE="$FLATNOTES_HOOK_CREATE"


See [the wiki](https://github.com/dullage/flatnotes/wiki) for more details.

Expand Down Expand Up @@ -118,3 +119,32 @@ A special thanks to 2 fantastic open-source projects that make flatnotes possibl

* [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html) - A fast, pure Python search engine library.
* [TOAST UI Editor](https://ui.toast.com/tui-editor) - A GFM Markdown and WYSIWYG editor for the browser.

## Contributing

Requirements:

- python3, node, npm, pipenv
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks for doc'ing this. I was thinking about experimenting with this so this will help!


### Local run


1. Install deps:

pipenv install
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly dumb question; Should this include --ignore-pipfile?

RUN pipenv install --deploy --ignore-pipfile --system && \
uses it.

From https://pipenv.pypa.io/en/latest/commands.html#install

--ignore-pipfile — Install from the Pipfile.lock and completely ignore Pipfile information.

npm install

3. Build UI

npm run build

4. Switch to virtual environment

pipenv shell
mkdir notes # for local test, it's git-ignored

5. Run local server

FLATNOTES_PATH=notes FLATNOTES_AUTH_TYPE=none uvicorn --app-dir server main:app --port 8080

Dev instance will be available on http://127.0.0.1:8080
21 changes: 21 additions & 0 deletions server/notes/file_system/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
import time
import subprocess
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pedantic suggestion; recommend moving this to the line above to have pep-8 sorting. I know the existing code isn't strictly adhering to this. https://pypi.org/project/isort/ can help with this.

from datetime import datetime
from typing import List, Literal, Set, Tuple

Expand Down Expand Up @@ -58,6 +59,7 @@ def create(self, data: NoteCreate) -> Note:
"""Create a new note."""
filepath = self._path_from_title(data.title)
self._write_file(filepath, data.content)
self._exec_hook("create", filepath)
return Note(
title=data.title,
content=data.content,
Expand Down Expand Up @@ -93,6 +95,8 @@ def update(self, title: str, data: NoteUpdate) -> Note:
content = data.new_content
else:
content = self._read_file(filepath)

self._exec_hook("update", filepath)
return Note(
title=title,
content=content,
Expand All @@ -104,6 +108,7 @@ def delete(self, title: str) -> None:
is_valid_filename(title)
filepath = self._path_from_title(title)
os.remove(filepath)
self._exec_hook("delete", filepath)

def search(
self,
Expand Down Expand Up @@ -372,6 +377,22 @@ def _fieldnames_for_term(self, term: str) -> List[str]:
fields.append("tags")
return fields

def _exec_hook(self, hook_name: Literal["create", "update", "delete"], path: str):
"""Execute shell script for provided hook.
The hook script will be from FLATNOTES_HOOK_<name>, where name is a hook_name in upper case.
If there is no such env, hook will be ignored. Workdir is the storage path.
Changed path will be substituted from %s.
"""
hook_env_name = f"FLATNOTES_HOOK_{hook_name.strip().upper()}"
script = os.getenv(hook_env_name)
if not script:
return
subprocess.check_call(
script.replace("%s", path),
shell=True,
cwd=self.storage_path,
)

@staticmethod
def _get_matched_fields(matched_terms):
"""Return a set of matched fields from a set of ('field', 'term') "
Expand Down