claude_code_toolkit/cli/mod.rs
1//! Command-line interface components and argument parsing.
2//!
3//! This module provides the complete CLI structure for the Claude Code Toolkit,
4//! including command definitions, argument parsing, and subcommand organization.
5//! Built using the [`clap`] crate with derive macros for clean, maintainable
6//! command definitions.
7//!
8//! ## Command Structure
9//!
10//! The CLI is organized into several main command groups:
11//!
12//! - **Status & Monitoring**: `status`, `timer` - Session information and real-time monitoring
13//! - **Organization Management**: `org add/remove/list` - GitHub organization configuration
14//! - **Repository Management**: `repo add/remove/list` - Individual repository setup
15//! - **Synchronization**: `sync`, `sync force`, `sync status` - Credential sync operations
16//! - **Service Management**: `service install/start/stop/restart` - Daemon lifecycle
17//! - **Configuration**: `configure` - Interactive setup wizard
18//!
19//! ## Usage Examples
20//!
21//! ### Basic Status Commands
22//!
23//! ```bash
24//! # Check current session and sync status
25//! claude-code-toolkit status
26//!
27//! # Show real-time session timer
28//! claude-code-toolkit timer
29//! ```
30//!
31//! ### Organization and Repository Management
32//!
33//! ```bash
34//! # Add GitHub organization for automatic sync
35//! claude-code-toolkit org add my-organization
36//!
37//! # Add specific repository
38//! claude-code-toolkit repo add owner/repository-name
39//!
40//! # List configured targets
41//! claude-code-toolkit org list
42//! claude-code-toolkit repo list
43//! ```
44//!
45//! ### Credential Synchronization
46//!
47//! ```bash
48//! # Smart sync (only if credentials changed)
49//! claude-code-toolkit sync
50//!
51//! # Force sync to all targets
52//! claude-code-toolkit sync force
53//!
54//! # Check sync status across all targets
55//! claude-code-toolkit sync status
56//!
57//! # View recent sync logs
58//! claude-code-toolkit sync logs --lines 100
59//! ```
60//!
61//! ### Daemon Service Management
62//!
63//! ```bash
64//! # Install and start background service
65//! claude-code-toolkit service install
66//!
67//! # Control service lifecycle
68//! claude-code-toolkit service start
69//! claude-code-toolkit service stop
70//! claude-code-toolkit service restart
71//!
72//! # Remove service (keeping config)
73//! claude-code-toolkit service uninstall --keep-config
74//! ```
75//!
76//! ## Command Documentation
77//!
78//! Each command and subcommand includes comprehensive help text accessible via:
79//!
80//! ```bash
81//! claude-code-toolkit --help
82//! claude-code-toolkit org --help
83//! claude-code-toolkit sync --help
84//! ```
85
86pub mod commands;
87
88use clap::{ Parser, Subcommand };
89
90/// Main CLI structure for the Claude Code Toolkit.
91///
92/// This struct defines the root command and global options for the CLI.
93/// All functionality is accessed through subcommands defined in [`Commands`].
94#[derive(Parser)]
95#[command(name = "claude-code-toolkit")]
96#[command(about = "Claude Code Toolkit for credential sync and session monitoring")]
97#[command(version = env!("CARGO_PKG_VERSION"))]
98pub struct Cli {
99 /// The subcommand to execute
100 #[command(subcommand)]
101 pub command: Commands,
102}
103
104/// Available top-level commands for the Claude Code Toolkit.
105///
106/// These commands provide access to all major functionality including
107/// session monitoring, credential synchronization, service management,
108/// and configuration.
109#[derive(Subcommand)]
110pub enum Commands {
111 /// Show Claude Code session and sync status
112 ///
113 /// Displays current session information including:
114 /// - Session expiration time and remaining duration
115 /// - Last successful sync timestamps
116 /// - Configured organizations and repositories
117 /// - Service status (running, stopped, etc.)
118 Status,
119
120 /// Show real-time session timer
121 ///
122 /// Displays a live countdown timer showing time remaining
123 /// in the current Claude Code session. Updates every second
124 /// until session expires.
125 Timer,
126
127 /// Run as daemon (used by systemd)
128 ///
129 /// Starts the background daemon service for automatic
130 /// credential synchronization. This command is typically
131 /// called by systemd and not run directly by users.
132 Daemon,
133
134 /// Organization management
135 ///
136 /// Commands for managing GitHub organizations that will
137 /// receive automatic credential synchronization.
138 #[command(subcommand)]
139 Org(OrgCommands),
140
141 /// Repository management
142 ///
143 /// Commands for managing individual GitHub repositories
144 /// for credential synchronization.
145 #[command(subcommand)]
146 Repo(RepoCommands),
147
148 /// Sync credentials to all configured targets (smart - only if changed)
149 ///
150 /// Performs credential synchronization with intelligent change detection.
151 /// By default, only syncs if credentials have changed since last sync.
152 /// Use subcommands for forced sync or status checking.
153 Sync {
154 #[command(subcommand)]
155 command: Option<SyncCommands>,
156 },
157
158 /// Service management
159 ///
160 /// Commands for managing the background daemon service,
161 /// including installation, lifecycle control, and removal.
162 #[command(subcommand)]
163 Service(ServiceCommands),
164
165 /// Interactive configuration wizard
166 ///
167 /// Launches an interactive setup process to configure
168 /// GitHub integration, sync settings, and service options.
169 Configure,
170}
171
172/// GitHub organization management commands.
173///
174/// These commands configure which GitHub organizations will receive
175/// automatic credential synchronization from the Claude Code session.
176#[derive(Subcommand)]
177pub enum OrgCommands {
178 /// Add a GitHub organization for credential sync
179 ///
180 /// Adds a GitHub organization to the list of sync targets.
181 /// All repositories in this organization will receive
182 /// credential updates when changes are detected.
183 Add {
184 /// GitHub organization name (e.g., "my-company")
185 name: String,
186 },
187
188 /// Remove a GitHub organization
189 ///
190 /// Removes an organization from the sync target list.
191 /// Credentials will no longer be synchronized to this organization.
192 Remove {
193 /// GitHub organization name to remove
194 name: String,
195 },
196
197 /// List configured organizations
198 ///
199 /// Shows all GitHub organizations currently configured
200 /// for credential synchronization, including last sync status.
201 List,
202}
203
204/// GitHub repository management commands.
205///
206/// These commands configure individual repositories for credential sync,
207/// providing more granular control than organization-wide sync.
208#[derive(Subcommand)]
209pub enum RepoCommands {
210 /// Add a GitHub repository for credential sync (format: owner/repo)
211 ///
212 /// Adds a specific repository to the sync target list.
213 /// This allows selective syncing without configuring entire organizations.
214 Add {
215 /// Repository in format "owner/repository-name" (e.g., "user/my-repo")
216 repo: String,
217 },
218
219 /// Remove a GitHub repository
220 ///
221 /// Removes a repository from the sync target list.
222 /// Credentials will no longer be synchronized to this repository.
223 Remove {
224 /// Repository in format "owner/repository-name" to remove
225 repo: String,
226 },
227
228 /// List configured repositories
229 ///
230 /// Shows all repositories currently configured for credential
231 /// synchronization, including last sync status and timestamps.
232 List,
233}
234
235/// Credential synchronization commands.
236///
237/// These commands control when and how credentials are synchronized
238/// to configured GitHub targets, with options for forced sync and monitoring.
239#[derive(Subcommand)]
240pub enum SyncCommands {
241 /// Force sync credentials to all targets (ignores change detection)
242 ///
243 /// Performs immediate synchronization to all configured organizations
244 /// and repositories, bypassing change detection. Useful for testing
245 /// or when manual sync is required.
246 Force,
247
248 /// Show detailed sync status for all targets
249 ///
250 /// Displays comprehensive sync status including:
251 /// - Last successful sync timestamps for each target
252 /// - Failed sync attempts and error details
253 /// - Credential change detection status
254 /// - Next scheduled sync times
255 Status,
256
257 /// Show daemon sync logs
258 ///
259 /// Displays recent log entries from the background daemon,
260 /// including sync operations, errors, and system events.
261 Logs {
262 /// Number of log lines to display (default: 50)
263 #[arg(short, long, default_value = "50")]
264 lines: usize,
265 },
266}
267
268/// Background service management commands.
269///
270/// These commands control the lifecycle of the background daemon service
271/// that handles automatic credential synchronization.
272#[derive(Subcommand)]
273pub enum ServiceCommands {
274 /// Install and start the sync daemon
275 ///
276 /// Installs the systemd service and starts automatic background synchronization.
277 /// Requires Linux or WSL with systemd enabled. Creates necessary configuration
278 /// files and sets up auto-start on system boot.
279 ///
280 /// **Platform Requirements**: Linux or WSL with systemd enabled
281 Install,
282
283 /// Stop and uninstall the sync daemon
284 ///
285 /// Stops the running service and removes it from the system.
286 /// Optionally preserves configuration files for future reinstallation.
287 Uninstall {
288 /// Keep configuration files when uninstalling
289 #[arg(long)]
290 keep_config: bool,
291 },
292
293 /// Start the sync daemon
294 ///
295 /// Starts the background service if it's installed but not running.
296 /// The service will begin monitoring for credential changes and
297 /// performing automatic synchronization.
298 Start,
299
300 /// Stop the sync daemon
301 ///
302 /// Stops the running background service while leaving it installed.
303 /// Automatic synchronization will cease until the service is restarted.
304 Stop,
305
306 /// Restart the sync daemon
307 ///
308 /// Stops and then starts the background service, useful for
309 /// applying configuration changes or recovering from errors.
310 Restart,
311
312 /// Enable daemon auto-start on system boot
313 ///
314 /// Configures the service to start automatically when the system boots.
315 /// Ensures continuous credential synchronization across reboots.
316 Enable,
317
318 /// Disable daemon auto-start
319 ///
320 /// Prevents the service from starting automatically on system boot.
321 /// The service can still be started manually when needed.
322 Disable,
323}