Add CLI source code and update .gitignore

This commit is contained in:
2026-04-07 09:38:12 +08:00
parent 6ef2dddf3f
commit 9e17f1bf29
18 changed files with 2663 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
"""
Revision operations for CodiMD CLI.
"""
import json
from typing import List, Dict, Any
from datetime import datetime
from .client import CodiMDClient
class RevisionManager:
"""Manager for revision operations."""
def __init__(self, client: CodiMDClient):
self.client = client
def list_revisions(self, note_id: str) -> List[Dict[str, Any]]:
"""List all revisions for a note."""
response = self.client.get(f"/{note_id}/revision")
if response.status_code == 404:
raise FileNotFoundError(f"Note not found: {note_id}")
if response.status_code == 403:
raise PermissionError("No permission to view revisions")
if response.status_code != 200:
raise Exception(f"Failed to list revisions: {response.status_code}")
try:
data = response.json()
return data.get("revisions", [])
except:
return []
def get_revision_at_time(self, note_id: str, timestamp: int) -> Dict[str, Any]:
"""Get note content at specific revision time."""
response = self.client.get(
f"/{note_id}/revision/{timestamp}",
allow_redirects=True
)
if response.status_code == 404:
raise FileNotFoundError(f"Note or revision not found")
if response.status_code != 200:
raise Exception(f"Failed to get revision: {response.status_code}")
try:
return response.json()
except:
return {"note_id": note_id, "timestamp": timestamp, "content": response.text}
def get_latest_revision(self, note_id: str) -> Dict[str, Any]:
"""Get the latest revision of a note."""
revisions = self.list_revisions(note_id)
if not revisions:
raise FileNotFoundError(f"No revisions found for note: {note_id}")
latest = revisions[0]
return self.get_revision_at_time(note_id, latest.get("time", 0))