feat: Implement core Onyx CLI logic in src/onyx.py, remove various test scripts, and update README.md.
This commit is contained in:
1 parent
e536630b91
commit
e7304b840e
12 files changed
+55
-265
No files matched your search
@@ -1,3 +0,0 @@
|
|||||||
default_vault: test
|
|
||||||
vaults:
|
|
||||||
test: C:/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/test_vault_newline
|
|
||||||
@@ -163,6 +163,7 @@ onyx --version
|
|||||||
|
|
||||||
- **Vault Management**: Set and switch between multiple Obsidian vaults
|
- **Vault Management**: Set and switch between multiple Obsidian vaults
|
||||||
- **Note Operations**: Create, open, move, rename, and delete notes
|
- **Note Operations**: Create, open, move, rename, and delete notes
|
||||||
|
- **Templates**: Support for Core Templates and Templater
|
||||||
- **Recursive Search**: Find notes in nested directories
|
- **Recursive Search**: Find notes in nested directories
|
||||||
- **Periodic Notes**: Support for daily, weekly, monthly, quarterly, and yearly notes
|
- **Periodic Notes**: Support for daily, weekly, monthly, quarterly, and yearly notes
|
||||||
- **Frontmatter Management**: Edit, view, clear, and delete YAML frontmatter
|
- **Frontmatter Management**: Edit, view, clear, and delete YAML frontmatter
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
# Add src to path
|
|
||||||
sys.path.append(str(Path.cwd() / 'src'))
|
|
||||||
from onyx import OnyxCLI
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
def test_extension_logic():
|
|
||||||
# Setup
|
|
||||||
test_dir = Path('reproduce_test_vault')
|
|
||||||
if test_dir.exists():
|
|
||||||
shutil.rmtree(test_dir)
|
|
||||||
test_dir.mkdir()
|
|
||||||
|
|
||||||
# We need to set a default vault for the CLI to work without error
|
|
||||||
# Since we can't easily inject config without mocking, we'll rely on the fact that
|
|
||||||
# create_note calls _get_vault_and_path.
|
|
||||||
# We will mock the config file path or just use the class methods that don't require it if possible,
|
|
||||||
# but create_note DOES require it.
|
|
||||||
|
|
||||||
# Let's just instantiate CLI and manually set the vault path in the internal config if needed,
|
|
||||||
# or better, just use the public method set_default_vault to configure our test vault.
|
|
||||||
|
|
||||||
cli = OnyxCLI()
|
|
||||||
try:
|
|
||||||
cli.set_default_vault('reproduce_vault', str(test_dir.resolve()))
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Setup warning: {e}")
|
|
||||||
|
|
||||||
# Test 1: No extension
|
|
||||||
print("Test 1: 'note1' -> should be 'note1.md'")
|
|
||||||
try:
|
|
||||||
cli.create_note('note1', 'content')
|
|
||||||
if (test_dir / 'note1.md').exists():
|
|
||||||
print("PASS: note1.md created")
|
|
||||||
else:
|
|
||||||
print("FAIL: note1.md NOT created")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FAIL: Test 1 error: {e}")
|
|
||||||
|
|
||||||
# Test 2: With .md extension
|
|
||||||
print("\nTest 2: 'note2.md' -> should be 'note2.md'")
|
|
||||||
try:
|
|
||||||
cli.create_note('note2.md', 'content')
|
|
||||||
if (test_dir / 'note2.md').exists() and not (test_dir / 'note2.md.md').exists():
|
|
||||||
print("PASS: note2.md created correctly")
|
|
||||||
else:
|
|
||||||
print("FAIL: note2.md handling incorrect")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FAIL: Test 2 error: {e}")
|
|
||||||
|
|
||||||
# Test 3: With other extension (potential issue)
|
|
||||||
print("\nTest 3: 'note3.txt' -> should be 'note3.txt.md' (assuming user intent)")
|
|
||||||
try:
|
|
||||||
cli.create_note('note3.txt', 'content')
|
|
||||||
if (test_dir / 'note3.txt.md').exists():
|
|
||||||
print("PASS: note3.txt.md created")
|
|
||||||
elif (test_dir / 'note3.txt').exists():
|
|
||||||
print("FAIL: created note3.txt instead of note3.txt.md")
|
|
||||||
else:
|
|
||||||
print("FAIL: unexpected result for note3.txt")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FAIL: Test 3 error: {e}")
|
|
||||||
|
|
||||||
# Test 4: Nested folders
|
|
||||||
print("\nTest 4: 'folder/nested/note' -> should create folders")
|
|
||||||
try:
|
|
||||||
cli.create_note('folder/nested/note', 'content')
|
|
||||||
if (test_dir / 'folder' / 'nested' / 'note.md').exists():
|
|
||||||
print("PASS: Nested folders created")
|
|
||||||
else:
|
|
||||||
print("FAIL: Nested folders NOT created")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"FAIL: Test 4 error: {e}")
|
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
try:
|
|
||||||
shutil.rmtree(test_dir)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_extension_logic()
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
import subprocess
|
|
||||||
import os
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
# Clean up previous test vault
|
|
||||||
vault_path = Path("test_vault").resolve()
|
|
||||||
if vault_path.exists():
|
|
||||||
shutil.rmtree(vault_path)
|
|
||||||
vault_path.mkdir(exist_ok=True)
|
|
||||||
|
|
||||||
# Create a note with French characters manually
|
|
||||||
note_name = "french_test.md"
|
|
||||||
note_path = vault_path / note_name
|
|
||||||
content = "C'est l'été à Paris. Ça va bien?"
|
|
||||||
|
|
||||||
# Write with default encoding (likely cp1252 on Windows or utf-8)
|
|
||||||
try:
|
|
||||||
# Write as UTF-8 (no BOM) to simulate a "good" file
|
|
||||||
note_path.write_text(content, encoding="utf-8")
|
|
||||||
print(f"Created {note_name} with utf-8")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating note: {e}")
|
|
||||||
|
|
||||||
# Import OnyxCLI
|
|
||||||
sys.path.append(str(Path("src").resolve()))
|
|
||||||
from onyx import OnyxCLI
|
|
||||||
|
|
||||||
cli = OnyxCLI()
|
|
||||||
# Mock config to point to our test vault
|
|
||||||
cli.config_file = Path("test_config.yaml").resolve()
|
|
||||||
cli.config_file.write_text(f"default_vault: test\nvaults:\n test: {str(vault_path).replace(os.sep, '/')}", encoding="utf-8")
|
|
||||||
|
|
||||||
print("\n--- Reading note with OnyxCLI ---")
|
|
||||||
try:
|
|
||||||
# This calls read_text() internally.
|
|
||||||
# With our fix (utf-8-sig), it should read utf-8 (no BOM) correctly too.
|
|
||||||
cli.print_note(note_name, vault="test")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading note: {e}")
|
|
||||||
|
|
||||||
print("\n--- Creating note with OnyxCLI ---")
|
|
||||||
new_note_name = "french_cli_created.md"
|
|
||||||
new_content = "Hélène et François"
|
|
||||||
try:
|
|
||||||
cli.create_note(new_note_name, content=new_content, vault="test", overwrite=True)
|
|
||||||
# Check how it was saved
|
|
||||||
saved_path = vault_path / new_note_name
|
|
||||||
# Read as bytes to check for BOM
|
|
||||||
raw_bytes = saved_path.read_bytes()
|
|
||||||
print(f"Saved bytes: {raw_bytes}")
|
|
||||||
|
|
||||||
if raw_bytes.startswith(b'\xef\xbb\xbf'):
|
|
||||||
print("SUCCESS: BOM detected")
|
|
||||||
else:
|
|
||||||
print("FAILURE: No BOM detected")
|
|
||||||
|
|
||||||
# Try reading it back with utf-8-sig (which handles BOM)
|
|
||||||
print(f"Content read back as utf-8-sig: {saved_path.read_text(encoding='utf-8-sig')}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating/checking note: {e}")
|
|
||||||
Binary file not shown.
+54
-3
@@ -146,13 +146,63 @@ class OnyxCLI:
|
|||||||
click.echo(f"Default vault: {self.default_vault}")
|
click.echo(f"Default vault: {self.default_vault}")
|
||||||
click.echo(f"Path: {vault_path}")
|
click.echo(f"Path: {vault_path}")
|
||||||
|
|
||||||
|
def _get_template_folder(self, vault_path: Path) -> Optional[Path]:
|
||||||
|
"""
|
||||||
|
Get template folder path from Templater plugin (if enabled),
|
||||||
|
falling back to core Templates plugin.
|
||||||
|
"""
|
||||||
|
# Try Templater plugin first
|
||||||
|
templater_config = vault_path / ".obsidian" / "plugins" / "templater-obsidian" / "data.json"
|
||||||
|
if templater_config.exists():
|
||||||
|
try:
|
||||||
|
config = json.loads(templater_config.read_text(encoding='utf-8-sig'))
|
||||||
|
folder = config.get('templates_folder')
|
||||||
|
if folder:
|
||||||
|
return vault_path / folder
|
||||||
|
except (json.JSONDecodeError, KeyError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Fall back to core Templates plugin
|
||||||
|
templates_config = vault_path / ".obsidian" / "templates.json"
|
||||||
|
if templates_config.exists():
|
||||||
|
try:
|
||||||
|
config = json.loads(templates_config.read_text(encoding='utf-8-sig'))
|
||||||
|
folder = config.get('folder')
|
||||||
|
if folder:
|
||||||
|
return vault_path / folder
|
||||||
|
except (json.JSONDecodeError, KeyError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def create_note(self, note_name: str, content: str = "", vault: str = None,
|
def create_note(self, note_name: str, content: str = "", vault: str = None,
|
||||||
overwrite: bool = False, append: bool = False, open_after: bool = False):
|
overwrite: bool = False, append: bool = False, open_after: bool = False, template: str = None):
|
||||||
vault_name, vault_path = self._get_vault_and_path(vault)
|
vault_name, vault_path = self._get_vault_and_path(vault)
|
||||||
note_path = vault_path / note_name
|
note_path = vault_path / note_name
|
||||||
if not str(note_path).endswith('.md'):
|
if not str(note_path).endswith('.md'):
|
||||||
note_path = Path(str(note_path) + '.md')
|
note_path = Path(str(note_path) + '.md')
|
||||||
|
|
||||||
|
if template:
|
||||||
|
template_folder = self._get_template_folder(vault_path)
|
||||||
|
if template_folder:
|
||||||
|
template_path = template_folder / template
|
||||||
|
if not template_path.suffix:
|
||||||
|
template_path = template_path.with_suffix('.md')
|
||||||
|
|
||||||
|
if template_path.exists():
|
||||||
|
template_content = template_path.read_text(encoding='utf-8-sig')
|
||||||
|
if content:
|
||||||
|
content = template_content + '\n' + content
|
||||||
|
else:
|
||||||
|
content = template_content
|
||||||
|
|
||||||
|
# Open by default if template is used
|
||||||
|
open_after = True
|
||||||
|
else:
|
||||||
|
click.echo(f"Warning: Template '{template}' not found at {template_path}")
|
||||||
|
else:
|
||||||
|
click.echo("Warning: Template folder not configured")
|
||||||
|
|
||||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
if note_path.exists():
|
if note_path.exists():
|
||||||
@@ -683,7 +733,8 @@ def yearly(vault):
|
|||||||
@click.option('--overwrite', is_flag=True, help="Overwrite existing note")
|
@click.option('--overwrite', is_flag=True, help="Overwrite existing note")
|
||||||
@click.option('--append', is_flag=True, help="Append to existing note")
|
@click.option('--append', is_flag=True, help="Append to existing note")
|
||||||
@click.option('--open', 'open_after', is_flag=True, help="Open note after creation")
|
@click.option('--open', 'open_after', is_flag=True, help="Open note after creation")
|
||||||
def create(note_name, content, vault, overwrite, append, open_after):
|
@click.option('--template', help="Template to use")
|
||||||
|
def create(note_name, content, vault, overwrite, append, open_after, template):
|
||||||
"""Create or modify a note in the vault
|
"""Create or modify a note in the vault
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
@@ -692,7 +743,7 @@ def create(note_name, content, vault, overwrite, append, open_after):
|
|||||||
"""
|
"""
|
||||||
if content:
|
if content:
|
||||||
content = content.replace('\\n', '\n')
|
content = content.replace('\\n', '\n')
|
||||||
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after)
|
OnyxCLI().create_note(note_name, content, vault, overwrite, append, open_after, template)
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument('note_name')
|
@click.argument('note_name')
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
default_vault: test
|
|
||||||
vaults:
|
|
||||||
test: C:/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/test_vault_exe
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
default_vault: test
|
|
||||||
vaults:
|
|
||||||
test: C:/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/test_vault
|
|
||||||
-48
@@ -1,48 +0,0 @@
|
|||||||
import subprocess
|
|
||||||
import shutil
|
|
||||||
from pathlib import Path
|
|
||||||
import sys
|
|
||||||
|
|
||||||
def test_exe():
|
|
||||||
exe_path = Path('dist/onyx.exe').resolve()
|
|
||||||
if not exe_path.exists():
|
|
||||||
print(f"Executable not found at {exe_path}")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
test_dir = Path('test_vault_exe')
|
|
||||||
if test_dir.exists():
|
|
||||||
shutil.rmtree(test_dir)
|
|
||||||
test_dir.mkdir()
|
|
||||||
|
|
||||||
print(f"Testing executable: {exe_path}")
|
|
||||||
|
|
||||||
# Set default vault
|
|
||||||
cmd = [str(exe_path), 'set-default', 'test_vault_exe', '--path', str(test_dir.resolve())]
|
|
||||||
print(f"Running: {' '.join(cmd)}")
|
|
||||||
subprocess.run(cmd, check=True)
|
|
||||||
|
|
||||||
# Create note without extension
|
|
||||||
cmd = [str(exe_path), 'create', 'exe_note1', '--content', 'content']
|
|
||||||
print(f"Running: {' '.join(cmd)}")
|
|
||||||
subprocess.run(cmd, check=True)
|
|
||||||
|
|
||||||
if (test_dir / 'exe_note1.md').exists():
|
|
||||||
print("PASS: exe_note1.md created")
|
|
||||||
else:
|
|
||||||
print("FAIL: exe_note1.md NOT created")
|
|
||||||
|
|
||||||
# Create note with .txt extension
|
|
||||||
cmd = [str(exe_path), 'create', 'exe_note2.txt', '--content', 'content']
|
|
||||||
print(f"Running: {' '.join(cmd)}")
|
|
||||||
subprocess.run(cmd, check=True)
|
|
||||||
|
|
||||||
if (test_dir / 'exe_note2.txt.md').exists():
|
|
||||||
print("PASS: exe_note2.txt.md created")
|
|
||||||
else:
|
|
||||||
print("FAIL: exe_note2.txt.md NOT created")
|
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
shutil.rmtree(test_dir)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_exe()
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
default_vault: test
|
|
||||||
vaults:
|
|
||||||
test: C:/Users/oli/Documents/o/Resources/repos/Onyx - obsidian cli tool/test_vault_newline
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
sys.path.append(str(Path("src").resolve()))
|
|
||||||
from click.testing import CliRunner
|
|
||||||
from onyx import cli
|
|
||||||
import os
|
|
||||||
|
|
||||||
# Setup test vault
|
|
||||||
vault_path = Path("test_vault_newline").resolve()
|
|
||||||
if vault_path.exists():
|
|
||||||
import shutil
|
|
||||||
shutil.rmtree(vault_path)
|
|
||||||
vault_path.mkdir(exist_ok=True)
|
|
||||||
|
|
||||||
# Create config
|
|
||||||
config_dir = Path(".onyx-cli-test")
|
|
||||||
config_dir.mkdir(exist_ok=True)
|
|
||||||
config_file = config_dir / "config.yaml"
|
|
||||||
config_file.write_text(f"default_vault: test\nvaults:\n test: {str(vault_path).replace(os.sep, '/')}", encoding="utf-8")
|
|
||||||
|
|
||||||
# Mock the config path in OnyxCLI (we need to monkeypatch or just rely on the fact that we can't easily change the class attribute globally without reloading,
|
|
||||||
# but wait, OnyxCLI is instantiated inside the commands.
|
|
||||||
# I'll just monkeypatch OnyxCLI.__init__ to use my config file for this test, or better, just set the env var if supported?
|
|
||||||
# The code uses Path.home() / '.onyx-cli' / 'config.yaml'.
|
|
||||||
# I'll monkeypatch Path.home to return my local dir.
|
|
||||||
|
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
with patch("pathlib.Path.home", return_value=Path.cwd() / "test_home"):
|
|
||||||
# Setup home dir
|
|
||||||
home = Path.cwd() / "test_home"
|
|
||||||
(home / ".onyx-cli").mkdir(parents=True, exist_ok=True)
|
|
||||||
(home / ".onyx-cli" / "config.yaml").write_text(f"default_vault: test\nvaults:\n test: {str(vault_path).replace(os.sep, '/')}", encoding="utf-8")
|
|
||||||
|
|
||||||
runner = CliRunner()
|
|
||||||
|
|
||||||
print("--- Testing with literal \\n ---")
|
|
||||||
result = runner.invoke(cli, ['create', 'newline_test', '--content', 'Line1\\nLine2', '--vault', 'test'])
|
|
||||||
|
|
||||||
if result.exit_code != 0:
|
|
||||||
print(f"Command failed: {result.output}")
|
|
||||||
else:
|
|
||||||
note_path = vault_path / "newline_test.md"
|
|
||||||
if note_path.exists():
|
|
||||||
content = note_path.read_text(encoding='utf-8-sig')
|
|
||||||
print(f"Content length: {len(content)}")
|
|
||||||
print(f"Content repr: {repr(content)}")
|
|
||||||
if '\n' in content and '\\n' not in content:
|
|
||||||
print("Result: Real newline found")
|
|
||||||
elif '\\n' in content:
|
|
||||||
print("Result: Literal \\n found")
|
|
||||||
else:
|
|
||||||
print("Result: Unexpected content")
|
|
||||||
else:
|
|
||||||
print("Note not created")
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
Line1
|
|
||||||
Line2
|
|
||||||
Reference in new issue
Block a user