Skip to content

Commit cc0ade3

Browse files
committed
feat: Add standalone Rust CLI binary with TypeScript wrapper
Create a comprehensive CLI for CodeGraph that can be integrated into TypeScript applications without requiring an HTTP server. New Components: 1. Rust CLI Binary (crates/codegraph-cli): - Comprehensive command-line interface - Commands: transaction, version, branch, search, status - Multiple output formats: JSON, pretty, table - Full integration with TransactionalGraph - Colorized human-readable output with 'colored' crate - Built with clap for robust argument parsing 2. TypeScript Wrapper (sdk/codegraph-cli-wrapper.ts): - Type-safe wrapper for spawning Rust CLI - Promise-based async API - Automatic JSON parsing - Timeout handling and error recovery - Full TypeScript types for all operations 3. Integration Example (sdk/examples/cli-usage.ts): - Complete workflow demonstration - Transaction management example - Version and branch operations - Error handling patterns 4. Comprehensive Documentation: - CLI README with usage examples - TypeScript integration guide - API reference - Troubleshooting section - Performance tips Key Features: Transaction Management: - Begin/commit/rollback with isolation levels - Transaction statistics - Serializable isolation support Version Management: - Create versions with metadata - List, get, tag versions - Compare versions (diffs) - Parent-child relationships Branch Management: - Create, list, get, delete branches - Merge branches with conflict detection - Branch metadata tracking Architecture Benefits: ✅ No HTTP server needed - Direct process spawning ✅ Type-safe TypeScript integration ✅ Standalone tool - works independently ✅ Simple IPC - JSON over stdin/stdout ✅ Fast - Native Rust performance ✅ Unix philosophy - Composable CLI tools Integration Pattern: ┌────────────────────────────┐ │ TypeScript CLI │ │ const cg = CodeGraph() │ │ await cg.createVersion() │ └──────────┬─────────────────┘ │ spawn() ┌──────────▼─────────────────┐ │ Rust CLI Binary │ │ codegraph version create │ └────────────────────────────┘ Usage: // TypeScript import CodeGraph from './sdk/codegraph-cli-wrapper'; const cg = new CodeGraph(); await cg.createVersion({ name: 'v1.0.0', description: 'Release', author: 'user@example.com' }); // Or directly: $ codegraph version create --name v1.0 --description "Release" --author user Build: cargo build --release --bin codegraph Related to: User request for TypeScript CLI integration
1 parent fc4c8d5 commit cc0ade3

File tree

7 files changed

+2088
-0
lines changed

7 files changed

+2088
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members = [
1313
"crates/codegraph-lb",
1414
"crates/codegraph-ai",
1515
"crates/codegraph-api",
16+
"crates/codegraph-cli",
1617
"crates/core-rag-mcp-server",
1718
"scripts",
1819
"tests/integration"

crates/codegraph-cli/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "codegraph-cli"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
8+
[[bin]]
9+
name = "codegraph"
10+
path = "src/main.rs"
11+
12+
[dependencies]
13+
# Workspace dependencies
14+
tokio = { workspace = true }
15+
serde = { workspace = true }
16+
serde_json = { workspace = true }
17+
anyhow = { workspace = true }
18+
uuid = { workspace = true }
19+
chrono = { workspace = true }
20+
21+
# CLI parsing
22+
clap = { version = "4.5", features = ["derive", "env"] }
23+
24+
# Colored terminal output
25+
colored = "2.1"
26+
27+
# Table formatting
28+
tabled = "0.16"
29+
30+
# Local crates
31+
codegraph-core = { path = "../codegraph-core" }
32+
codegraph-api = { path = "../codegraph-api" }
33+
codegraph-graph = { path = "../codegraph-graph" }

crates/codegraph-cli/README.md

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
# CodeGraph CLI
2+
3+
A command-line interface for CodeGraph version control and dependency management.
4+
5+
## Installation
6+
7+
### Build from source
8+
9+
```bash
10+
# Build the CLI binary
11+
cargo build --release --bin codegraph
12+
13+
# Install to system path
14+
cargo install --path crates/codegraph-cli
15+
```
16+
17+
### Verify installation
18+
19+
```bash
20+
codegraph --version
21+
```
22+
23+
## Quick Start
24+
25+
```bash
26+
# Check status
27+
codegraph status
28+
29+
# Create a version
30+
codegraph version create \
31+
--name "v1.0.0" \
32+
--description "Initial release" \
33+
--author "user@example.com"
34+
35+
# List versions
36+
codegraph version list
37+
38+
# Create a branch
39+
codegraph branch create \
40+
--name "feature/new-api" \
41+
--from <version-id> \
42+
--author "user@example.com"
43+
```
44+
45+
## Commands
46+
47+
### Transaction Management
48+
49+
```bash
50+
# Begin a transaction
51+
codegraph transaction begin --isolation serializable
52+
53+
# Commit a transaction
54+
codegraph transaction commit <transaction-id>
55+
56+
# Rollback a transaction
57+
codegraph transaction rollback <transaction-id>
58+
59+
# Get transaction statistics
60+
codegraph transaction stats
61+
```
62+
63+
### Version Management
64+
65+
```bash
66+
# Create a version
67+
codegraph version create \
68+
--name "v1.0.0" \
69+
--description "Initial release" \
70+
--author "user@example.com" \
71+
--parents <parent-id-1>,<parent-id-2>
72+
73+
# List versions (default: 50)
74+
codegraph version list --limit 100
75+
76+
# Get version details
77+
codegraph version get <version-id>
78+
79+
# Tag a version
80+
codegraph version tag <version-id> \
81+
--tag "stable" \
82+
--author "user@example.com" \
83+
--message "Stable release"
84+
85+
# Compare versions
86+
codegraph version compare <from-id> <to-id>
87+
```
88+
89+
### Branch Management
90+
91+
```bash
92+
# Create a branch
93+
codegraph branch create \
94+
--name "feature/auth" \
95+
--from <version-id> \
96+
--author "user@example.com" \
97+
--description "Authentication feature"
98+
99+
# List branches
100+
codegraph branch list
101+
102+
# Get branch details
103+
codegraph branch get <branch-name>
104+
105+
# Delete a branch
106+
codegraph branch delete <branch-name>
107+
108+
# Merge branches
109+
codegraph branch merge \
110+
--source "feature/auth" \
111+
--target "main" \
112+
--author "user@example.com" \
113+
--message "Merge authentication feature"
114+
```
115+
116+
## Output Formats
117+
118+
The CLI supports multiple output formats:
119+
120+
```bash
121+
# JSON output (default for programmatic use)
122+
codegraph --output json version list
123+
124+
# Pretty formatted output (human-readable)
125+
codegraph --output pretty version list
126+
127+
# Table output
128+
codegraph --output table version list
129+
```
130+
131+
## Global Options
132+
133+
```bash
134+
--output, -o <format> Output format: json, pretty, table (default: pretty)
135+
--storage <path> Storage path for CodeGraph data
136+
--verbose, -v Enable verbose output
137+
```
138+
139+
## Environment Variables
140+
141+
```bash
142+
CODEGRAPH_STORAGE Storage path for CodeGraph data
143+
```
144+
145+
## Integration with TypeScript
146+
147+
The CLI is designed to be easily integrated with TypeScript applications:
148+
149+
```typescript
150+
import { CodeGraph } from './sdk/codegraph-cli-wrapper';
151+
152+
const cg = new CodeGraph({
153+
binaryPath: 'codegraph', // Optional, defaults to searching PATH
154+
storagePath: '~/.codegraph', // Optional
155+
verbose: false,
156+
timeout: 30000
157+
});
158+
159+
// Create a version
160+
const version = await cg.createVersion({
161+
name: 'v1.0.0',
162+
description: 'Initial release',
163+
author: 'user@example.com',
164+
parents: []
165+
});
166+
167+
console.log(`Created version: ${version.version_id}`);
168+
169+
// List versions
170+
const versions = await cg.listVersions(10);
171+
versions.forEach(v => {
172+
console.log(`${v.name}: ${v.description}`);
173+
});
174+
175+
// Create a branch
176+
const branch = await cg.createBranch({
177+
name: 'feature/api',
178+
from: version.version_id,
179+
author: 'user@example.com'
180+
});
181+
182+
// Merge branches
183+
const result = await cg.mergeBranches({
184+
source: 'feature/api',
185+
target: 'main',
186+
author: 'user@example.com'
187+
});
188+
189+
if (result.success) {
190+
console.log('Merge successful!');
191+
} else {
192+
console.log(`Merge had ${result.conflicts} conflicts`);
193+
}
194+
```
195+
196+
## Examples
197+
198+
### Creating a Release Workflow
199+
200+
```bash
201+
#!/bin/bash
202+
203+
# Start a transaction
204+
TX=$(codegraph --output json transaction begin --isolation serializable | jq -r '.transaction_id')
205+
206+
# Create a version
207+
VERSION=$(codegraph --output json version create \
208+
--name "v2.0.0" \
209+
--description "Major release with breaking changes" \
210+
--author "release-bot@example.com" | jq -r '.version_id')
211+
212+
# Tag the version
213+
codegraph version tag $VERSION \
214+
--tag "stable" \
215+
--author "release-bot@example.com" \
216+
--message "Stable release ready for production"
217+
218+
# Commit the transaction
219+
codegraph transaction commit $TX
220+
221+
echo "Release v2.0.0 created: $VERSION"
222+
```
223+
224+
### Branch and Merge Workflow
225+
226+
```bash
227+
#!/bin/bash
228+
229+
# Get current main branch head
230+
MAIN_HEAD=$(codegraph --output json branch get main | jq -r '.head')
231+
232+
# Create feature branch
233+
codegraph branch create \
234+
--name "feature/search" \
235+
--from $MAIN_HEAD \
236+
--author "dev@example.com" \
237+
--description "Add search functionality"
238+
239+
# ... do work ...
240+
241+
# Merge back to main
242+
codegraph branch merge \
243+
--source "feature/search" \
244+
--target "main" \
245+
--author "dev@example.com" \
246+
--message "feat: Add search functionality"
247+
```
248+
249+
## Architecture
250+
251+
The CLI is a thin wrapper around the CodeGraph Rust libraries:
252+
253+
```
254+
┌─────────────────────────────┐
255+
│ codegraph CLI │
256+
│ (this crate) │
257+
└──────────┬──────────────────┘
258+
259+
├──> codegraph-core
260+
├──> codegraph-api (AppState)
261+
├──> codegraph-graph (TransactionalGraph)
262+
└──> codegraph-vector
263+
```
264+
265+
All commands operate on the local storage (default: `~/.codegraph`) using the same transactional graph implementation used by the API server.
266+
267+
## Troubleshooting
268+
269+
### Binary not found
270+
271+
```bash
272+
# Check if codegraph is in PATH
273+
which codegraph
274+
275+
# If not, use full path
276+
/path/to/codegraph version list
277+
278+
# Or add to PATH
279+
export PATH="$PATH:/path/to/cargo/bin"
280+
```
281+
282+
### Storage path issues
283+
284+
```bash
285+
# Specify storage path explicitly
286+
codegraph --storage /custom/path version list
287+
288+
# Or set environment variable
289+
export CODEGRAPH_STORAGE=/custom/path
290+
codegraph version list
291+
```
292+
293+
### Verbose debugging
294+
295+
```bash
296+
# Enable verbose output
297+
codegraph -v version create --name "v1.0" --description "Test" --author "user"
298+
```
299+
300+
## License
301+
302+
MIT OR Apache-2.0

0 commit comments

Comments
 (0)