feat: Add Onyx CLI tool source, tests, and PyInstaller build configuration.

This commit is contained in:
olivier committed 2025-11-20 19:49:16 -05:00
1 parent bf1dca3bc4
commit e498031f10
38 files changed
+43760 -77

No files matched your search

Binary file not shown.
+38 -38
View File
@@ -29,16 +29,16 @@ class OnyxCLI:
def _ensure_config_exists(self):
self.config_file.parent.mkdir(parents=True, exist_ok=True)
if not self.config_file.exists():
self.config_file.write_text(yaml.dump({'default_vault': None, 'vaults': {}}))
self.config_file.write_text(yaml.dump({'default_vault': None, 'vaults': {}}), encoding='utf-8-sig')
def _load_default_vault(self):
if self.config_file.exists():
config = yaml.safe_load(self.config_file.read_text())
config = yaml.safe_load(self.config_file.read_text(encoding='utf-8-sig'))
return config.get('default_vault')
return None
def _get_vault_path(self, vault_name: str) -> Path:
config = yaml.safe_load(self.config_file.read_text())
config = yaml.safe_load(self.config_file.read_text(encoding='utf-8-sig'))
vault_path = config.get('vaults', {}).get(vault_name)
if not vault_path:
raise click.UsageError(f"Vault '{vault_name}' not found in config")
@@ -63,7 +63,7 @@ class OnyxCLI:
periodic_notes_config = vault_path / ".obsidian" / "plugins" / "periodic-notes" / "data.json"
if periodic_notes_config.exists():
try:
config = json.loads(periodic_notes_config.read_text())
config = json.loads(periodic_notes_config.read_text(encoding='utf-8-sig'))
daily_config = config.get('daily', {})
# Check if daily notes are enabled in Periodic Notes
@@ -80,7 +80,7 @@ class OnyxCLI:
daily_notes_config = vault_path / ".obsidian" / "daily-notes.json"
if daily_notes_config.exists():
try:
config = json.loads(daily_notes_config.read_text())
config = json.loads(daily_notes_config.read_text(encoding='utf-8-sig'))
return {
'format': config.get('format', 'YYYY-MM-DD'),
'folder': config.get('folder', ''),
@@ -98,7 +98,7 @@ class OnyxCLI:
def set_default_vault(self, vault_name: str, vault_path: str = None):
try:
config = yaml.safe_load(self.config_file.read_text()) if self.config_file.exists() else {}
config = yaml.safe_load(self.config_file.read_text(encoding='utf-8-sig')) if self.config_file.exists() else {}
except Exception:
config = {}
@@ -118,11 +118,11 @@ class OnyxCLI:
# If no path and vault doesn't exist, show error
raise click.UsageError(
f"Vault '{vault_name}' not found. Please provide the path when setting a new vault:\n"
f"ob-cli set-default \"{vault_name}\" --path \"path/to/vault\""
f"onyx set-default \"{vault_name}\" --path \"path/to/vault\""
)
config['default_vault'] = vault_name
self.config_file.write_text(yaml.dump(config))
self.config_file.write_text(yaml.dump(config), encoding='utf-8-sig')
self.default_vault = vault_name
actual_path = config['vaults'][vault_name]
@@ -134,13 +134,13 @@ class OnyxCLI:
click.echo("No default vault set")
return
config = yaml.safe_load(self.config_file.read_text())
config = yaml.safe_load(self.config_file.read_text(encoding='utf-8-sig'))
vault_path = config.get('vaults', {}).get(self.default_vault)
if not vault_path:
raise click.UsageError(
f"Vault '{self.default_vault}' not found in config. Please set it up with:\n"
f"ob-cli set-default \"{self.default_vault}\" --path \"path/to/vault\""
f"onyx set-default \"{self.default_vault}\" --path \"path/to/vault\""
)
click.echo(f"Default vault: {self.default_vault}")
@@ -165,7 +165,7 @@ class OnyxCLI:
else:
mode = 'w'
with note_path.open(mode) as f:
with note_path.open(mode, encoding='utf-8-sig') as f:
if append and note_path.exists():
f.write('\n')
f.write(content)
@@ -280,7 +280,7 @@ class OnyxCLI:
periodic_notes_config = vault_path / ".obsidian" / "plugins" / "periodic-notes" / "data.json"
if periodic_notes_config.exists():
try:
config = json.loads(periodic_notes_config.read_text())
config = json.loads(periodic_notes_config.read_text(encoding='utf-8-sig'))
note_config = config.get(note_type, {})
if note_config.get('enabled', False):
settings = {
@@ -315,7 +315,7 @@ class OnyxCLI:
if not template_path.suffix:
template_path = template_path.with_suffix('.md')
if template_path.exists():
content = template_path.read_text()
content = template_path.read_text(encoding='utf-8-sig')
# Construct note path with folder
folder = settings['folder']
@@ -365,7 +365,7 @@ class OnyxCLI:
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
click.echo(note_path.read_text())
click.echo(note_path.read_text(encoding='utf-8-sig'))
def move_note(self, current_path: str, new_path: str, vault: str = None, open_after: bool = False):
vault_name, vault_path = self._get_vault_and_path(vault)
@@ -390,10 +390,10 @@ class OnyxCLI:
for md_file in vault_path.rglob("*.md"):
if md_file != new: # Skip the moved file
content = md_file.read_text()
content = md_file.read_text(encoding='utf-8-sig')
updated_content = re.sub(old_link_pattern, new_link, content)
if updated_content != content:
md_file.write_text(updated_content)
md_file.write_text(updated_content, encoding='utf-8-sig')
click.echo(f"Moved {current_path} to {new_path}")
@@ -427,7 +427,7 @@ class OnyxCLI:
return {}, content, 0
def _update_frontmatter(self, note_path: Path, updates: dict):
content = note_path.read_text()
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, rest_content, fm_length = self._parse_frontmatter(content)
frontmatter.update(updates)
@@ -437,7 +437,7 @@ class OnyxCLI:
new_content += '---\n'
new_content += rest_content if fm_length else content
note_path.write_text(new_content)
note_path.write_text(new_content, encoding='utf-8-sig')
def frontmatter_edit(self, note_name: str, key: str, value: str = None, vault: str = None):
vault_name, vault_path = self._get_vault_and_path(vault)
@@ -483,7 +483,7 @@ class OnyxCLI:
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
content = note_path.read_text()
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, _, _ = self._parse_frontmatter(content)
if key not in frontmatter:
@@ -538,7 +538,7 @@ class OnyxCLI:
if not note_path.exists():
raise click.UsageError(f"Note {note_name} does not exist")
content = note_path.read_text()
content = note_path.read_text(encoding='utf-8-sig')
frontmatter, rest_content, _ = self._parse_frontmatter(content)
keys = [k.strip() for k in re.split(r',|\[|\]', key) if k.strip()]
@@ -558,7 +558,7 @@ class OnyxCLI:
else:
new_content = rest_content
note_path.write_text(new_content)
note_path.write_text(new_content, encoding='utf-8-sig')
click.echo(f"Deleted frontmatter keys from {note_name}")
else:
click.echo("No matching keys found in frontmatter")
@@ -578,20 +578,20 @@ class OnyxCLI:
# CLI Commands
@click.group(name='ob-cli')
@click.group(name='onyx')
def cli():
"""Onyx CLI - Interact with Obsidian vaults from the command line
"""Onyx - Interact with Obsidian vaults from the command line
Manage your Obsidian notes, frontmatter, and periodic notes directly from the terminal.
EXAMPLES:
ob-cli open "my-note"
ob-cli daily
ob-cli frontmatter "my-note" --view --key "tags"
ob-cli --version
onyx open "my-note"
onyx daily
onyx frontmatter "my-note" --view --key "tags"
onyx --version
For more help on a specific command:
ob-cli COMMAND --help
onyx COMMAND --help
"""
pass
@@ -602,7 +602,7 @@ def set_default(vault_name, path):
"""Set or switch the default vault
USAGE:
ob-cli set-default VAULT_NAME --path /path/to/vault
onyx set-default VAULT_NAME --path /path/to/vault
"""
# Convert Windows backslashes to forward slashes if needed
if path:
@@ -623,8 +623,8 @@ def open(note_name, vault):
Searches for the note in the vault and opens it in Obsidian. Supports nested directories.
USAGE:
ob-cli open "my-note"
ob-cli open "folder/my-note"
onyx open "my-note"
onyx open "folder/my-note"
"""
OnyxCLI().open_note(note_name, vault)
@@ -671,8 +671,8 @@ def create(note_name, content, vault, overwrite, append, open_after):
"""Create or modify a note in the vault
USAGE:
ob-cli create "my-note"
ob-cli create "my-note" --content "Some content" --open
onyx create "my-note"
onyx create "my-note" --content "Some content" --open
"""
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after)
@@ -694,8 +694,8 @@ def move(current_path, new_path, vault, open_after):
Automatically updates all [[links]] pointing to this note in other vault files.
USAGE:
ob-cli move "old-name" "new-name"
ob-cli move "old-folder/note" "new-folder/note"
onyx move "old-name" "new-name"
onyx move "old-folder/note" "new-folder/note"
"""
OnyxCLI().move_note(current_path, new_path, vault, open_after)
@@ -721,9 +721,9 @@ def frontmatter_command(note_name, edit, view, clear, delete, key, value, vault)
Must specify exactly one action: --edit, --view, --clear, or --delete
USAGE:
ob-cli frontmatter "my-note" --view --key "tags"
ob-cli frontmatter "my-note" --edit --key "tags" --value "project,work"
ob-cli frontmatter "my-note" --delete --key "date"
onyx frontmatter "my-note" --view --key "tags"
onyx frontmatter "my-note" --edit --key "tags" --value "project,work"
onyx frontmatter "my-note" --delete --key "date"
"""
cli = OnyxCLI()
if sum([edit, view, clear, delete]) != 1: