81 lines
2.3 KiB
Python
Executable file
81 lines
2.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
from typer import Typer
|
|
|
|
from halig import logger
|
|
from halig.config import (
|
|
get_config,
|
|
DEFAULT_CONFIGURATION_PATH,
|
|
Config,
|
|
)
|
|
from halig.edit_note import edit_note
|
|
from halig.exceptions import InvalidNotePath
|
|
from halig.exceptions import handle_errors
|
|
from halig.new_note import new_note
|
|
from halig.utils import resolve_path
|
|
|
|
app = Typer(pretty_exceptions_enable=False)
|
|
|
|
|
|
@app.command()
|
|
def init(force_recreate: bool = False):
|
|
"""Create the config file. If the config file already exists, it'll not
|
|
be overwritten unless the `--force-recreate` flag is provided
|
|
"""
|
|
if DEFAULT_CONFIGURATION_PATH.exists() and not force_recreate:
|
|
logger.error(
|
|
"""$HOME/.config/halig/halig.yml already exists.
|
|
|
|
Execute again with --force-recreate in order to replace the configuration file's
|
|
contents with the default one"""
|
|
)
|
|
exit(1)
|
|
|
|
DEFAULT_CONFIGURATION_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
DEFAULT_CONFIGURATION_PATH.touch(mode=0o700, exist_ok=True)
|
|
with open(DEFAULT_CONFIGURATION_PATH, "w") as f:
|
|
yaml.dump(Config().dict(), f, Dumper=yaml.SafeDumper)
|
|
|
|
|
|
@app.command()
|
|
@handle_errors
|
|
def notebooks(
|
|
print_files: bool = False,
|
|
print_hidden: bool = False,
|
|
configuration_path: Path = DEFAULT_CONFIGURATION_PATH,
|
|
):
|
|
"""Print notebooks and their contents, tree-style"""
|
|
config = get_config(configuration_path)
|
|
logger.tree(
|
|
config.notes_root_path, print_files=print_files, print_hidden=print_hidden
|
|
)
|
|
|
|
|
|
@app.command()
|
|
@handle_errors
|
|
def edit(path: Path, configuration_path: Path = DEFAULT_CONFIGURATION_PATH):
|
|
"""Edit a new or existing note by providing a relative path. The relative path
|
|
will be appended to the notes root path that is specified in the configuration file.
|
|
Note that if only a dir is provided, an attempt to create or open
|
|
`<dir>/<current date>.age` will be made.
|
|
"""
|
|
|
|
config = get_config(configuration_path)
|
|
if path.is_absolute():
|
|
raise InvalidNotePath
|
|
|
|
full_path = resolve_path(path)
|
|
|
|
if full_path.is_dir():
|
|
return new_note(full_path / f"{datetime.now():%Y-%m-%d}.age", config)
|
|
|
|
if full_path.exists():
|
|
return edit_note(full_path, config)
|
|
|
|
new_note(full_path, config)
|
|
|
|
|
|
app()
|