Overview
The Spacedrive CLI (sd) is a command-line interface for interacting with the Spacedrive daemon. It provides a comprehensive set of commands for managing libraries, files, devices, and daemon lifecycle.
Architecture
The CLI follows a client-daemon architecture:- CLI Binary (
sd-cli/sd) - The client that sends commands - Daemon Binary (
sd-daemon) - The background service that does the actual work - Unix Socket - Communication channel between CLI and daemon
Daemon Management
Starting the Daemon
- Runs as a background process
- Listens on a Unix socket at
~/Library/Application Support/spacedrive/daemon/daemon.sock - Manages all libraries and background tasks
- Can run multiple instances with
--instanceflag
Auto-Start on Login
Install the daemon to start automatically on system boot:~/Library/LaunchAgents/com.spacedrive.daemon.plist that:
- Starts daemon on user login
- Restarts if it crashes
- Logs to
~/Library/Application Support/spacedrive/logs/
Multi-Instance Support
Run multiple daemon instances for different data directories:Configuration
The CLI stores configuration in~/Library/Application Support/spacedrive/cli.json:
Config Commands
Auto-Update System
The CLI includes a built-in update mechanism that fetches releases from a public GitHub repository.Update Architecture
Update Process
- Check for updates: Queries GitHub API for latest release
- Download binaries: Fetches platform-specific binaries
- Verify integrity: Checks file sizes match expected values
- Atomic replacement: Replaces binaries with rollback on failure
- Restart daemon: If daemon was running, restarts it
Release Repository Setup
The update system uses a public GitHub repository containing only releases (no source code):- Create public repo:
your-org/spacedrive-cli-releases - For each release:
- Create empty commit:
git commit --allow-empty -m "Release v0.1.0" - Tag it:
git tag v0.1.0 - Create GitHub release with binaries as assets
- Create empty commit:
- Configure CLI:
sd config set update.repo "your-org/spacedrive-cli-releases"
- macOS (arm64, x86_64)
- Linux (x86_64) - coming soon
- Windows (x86_64) - coming soon
Command Structure
All CLI commands follow this pattern:Global Flags
--data-dir- Override default data directory--instance- Connect to specific daemon instance--format- Output format:human(default) orjson
Core Commands
| Command | Description |
|---|---|
sd start | Start the daemon |
sd stop | Stop the daemon |
sd restart | Restart the daemon |
sd status | Show system status |
sd update | Update CLI and daemon |
Domain Commands
| Command | Description |
|---|---|
sd library | Manage libraries |
sd location | Manage indexed locations |
sd file | File operations |
sd index | Indexing operations |
sd search | Search files |
sd tag | Tag management |
sd volume | Volume operations |
sd device | Device management |
sd job | Job control |
sd network | Networking and pairing |
sd logs | View daemon logs |
sd config | Configuration management |
sd daemon | Daemon lifecycle |
Binary Distribution
For development and testing, binaries can be distributed without building from source:-
Build release binaries:
-
Copy both binaries to target machine:
sd-cli→ rename tosdsd-daemon→ keep assd-daemon
-
Place in PATH:
-
Remove quarantine (macOS):
Development
Building
Code Structure
Adding New Commands
- Create a new module in
domains/ - Define command enum with clap derives
- Implement
run()function - Register in
domains/mod.rsandmain.rs
Security Considerations
Instance Name Validation
Instance names are validated to prevent path traversal:- Only alphanumeric, dash, and underscore allowed
- Max 64 characters
- Cannot be empty
Binary Verification
The update system verifies downloaded binaries:- File size must match release asset size
- Future: SHA256 checksum verification
Daemon Socket Permissions
The Unix socket is created with restrictive permissions (user-only access).Troubleshooting
Daemon Won’t Start
Update Fails
Socket Connection Errors
The daemon socket is located at:- Verify daemon is running:
ps aux | grep sd-daemon - Check socket exists:
ls -la <socket-path> - Restart daemon:
sd restart
