cascade_cli/cli/mod.rs
1pub mod commands;
2
3use crate::errors::Result;
4use clap::{Parser, Subcommand};
5use clap_complete::Shell;
6use commands::entry::EntryAction;
7use commands::stack::StackAction;
8use commands::{MergeStrategyArg, RebaseStrategyArg};
9
10#[derive(Parser)]
11#[command(name = "ca")]
12#[command(about = "Cascade CLI - Stacked Diffs for Bitbucket")]
13#[command(version)]
14pub struct Cli {
15 #[command(subcommand)]
16 pub command: Commands,
17
18 /// Enable verbose logging
19 #[arg(long, short, global = true)]
20 pub verbose: bool,
21
22 /// Disable colored output
23 #[arg(long, global = true)]
24 pub no_color: bool,
25}
26
27#[derive(Subcommand)]
28pub enum Commands {
29 /// Initialize repository for Cascade
30 Init {
31 /// Bitbucket Server URL
32 #[arg(long)]
33 bitbucket_url: Option<String>,
34
35 /// Force initialization even if already initialized
36 #[arg(long)]
37 force: bool,
38 },
39
40 /// Configuration management
41 Config {
42 #[command(subcommand)]
43 action: ConfigAction,
44 },
45
46 /// Stack management
47 Stacks {
48 #[command(subcommand)]
49 action: StackAction,
50 },
51
52 /// Entry management and editing
53 Entry {
54 #[command(subcommand)]
55 action: EntryAction,
56 },
57
58 /// Show repository overview and all stacks
59 Repo,
60
61 /// Show version information
62 Version,
63
64 /// Check repository health and configuration
65 Doctor,
66
67 /// Generate shell completions
68 Completions {
69 #[command(subcommand)]
70 action: CompletionsAction,
71 },
72
73 /// Interactive setup wizard
74 Setup {
75 /// Force reconfiguration if already initialized
76 #[arg(long)]
77 force: bool,
78 },
79
80 /// Launch interactive TUI for stack management
81 Tui,
82
83 /// Git hooks management
84 Hooks {
85 #[command(subcommand)]
86 action: HooksAction,
87 },
88
89 /// Visualize stacks and dependencies
90 Viz {
91 #[command(subcommand)]
92 action: VizAction,
93 },
94
95 // Stack command shortcuts for commonly used operations
96 /// Show current stack details
97 Stack {
98 /// Show detailed pull request information
99 #[arg(short, long)]
100 verbose: bool,
101 /// Show mergability status for all PRs
102 #[arg(short, long)]
103 mergeable: bool,
104 },
105
106 /// Push current commit to the top of the stack (shortcut for 'stack push')
107 Push {
108 /// Branch name for this commit
109 #[arg(long, short)]
110 branch: Option<String>,
111 /// Commit message (if creating a new commit)
112 #[arg(long, short)]
113 message: Option<String>,
114 /// Use specific commit hash instead of HEAD
115 #[arg(long)]
116 commit: Option<String>,
117 /// Push commits since this reference (e.g., HEAD~3)
118 #[arg(long)]
119 since: Option<String>,
120 /// Push multiple specific commits (comma-separated)
121 #[arg(long)]
122 commits: Option<String>,
123 /// Squash last N commits into one before pushing
124 #[arg(long)]
125 squash: Option<usize>,
126 /// Squash all commits since this reference (e.g., HEAD~5)
127 #[arg(long)]
128 squash_since: Option<String>,
129 /// Auto-create feature branch when pushing from base branch
130 #[arg(long)]
131 auto_branch: bool,
132 /// Allow pushing commits from base branch (not recommended)
133 #[arg(long)]
134 allow_base_branch: bool,
135 },
136
137 /// Pop the top commit from the stack (shortcut for 'stack pop')
138 Pop {
139 /// Keep the branch (don't delete it)
140 #[arg(long)]
141 keep_branch: bool,
142 },
143
144 /// Land (merge) approved stack entries (shortcut for 'stack land')
145 Land {
146 /// Stack entry number to land (1-based index, optional)
147 entry: Option<usize>,
148 /// Force land even with blocking issues (dangerous)
149 #[arg(short, long)]
150 force: bool,
151 /// Dry run - show what would be landed without doing it
152 #[arg(short, long)]
153 dry_run: bool,
154 /// Use server-side validation (safer, checks approvals/builds)
155 #[arg(long)]
156 auto: bool,
157 /// Wait for builds to complete before merging
158 #[arg(long)]
159 wait_for_builds: bool,
160 /// Merge strategy to use
161 #[arg(long, value_enum, default_value = "squash")]
162 strategy: Option<MergeStrategyArg>,
163 /// Maximum time to wait for builds (seconds)
164 #[arg(long, default_value = "1800")]
165 build_timeout: u64,
166 },
167
168 /// Auto-land all ready PRs (shortcut for 'stack autoland')
169 Autoland {
170 /// Force land even with blocking issues (dangerous)
171 #[arg(short, long)]
172 force: bool,
173 /// Dry run - show what would be landed without doing it
174 #[arg(short, long)]
175 dry_run: bool,
176 /// Wait for builds to complete before merging
177 #[arg(long)]
178 wait_for_builds: bool,
179 /// Merge strategy to use
180 #[arg(long, value_enum, default_value = "squash")]
181 strategy: Option<MergeStrategyArg>,
182 /// Maximum time to wait for builds (seconds)
183 #[arg(long, default_value = "1800")]
184 build_timeout: u64,
185 },
186
187 /// Sync stack with remote repository (shortcut for 'stack sync')
188 Sync {
189 /// Force sync even if there are conflicts
190 #[arg(long)]
191 force: bool,
192 /// Skip cleanup of merged branches
193 #[arg(long)]
194 skip_cleanup: bool,
195 /// Interactive mode for conflict resolution
196 #[arg(long, short)]
197 interactive: bool,
198 },
199
200 /// Rebase stack on updated base branch (shortcut for 'stack rebase')
201 Rebase {
202 /// Interactive rebase
203 #[arg(long, short)]
204 interactive: bool,
205 /// Target base branch (defaults to stack's base branch)
206 #[arg(long)]
207 onto: Option<String>,
208 /// Rebase strategy to use
209 #[arg(long, value_enum)]
210 strategy: Option<RebaseStrategyArg>,
211 },
212
213 /// Switch to a different stack (shortcut for 'stacks switch')
214 Switch {
215 /// Name of the stack to switch to
216 name: String,
217 },
218
219 /// Deactivate the current stack - turn off stack mode (shortcut for 'stacks deactivate')
220 Deactivate {
221 /// Force deactivation without confirmation
222 #[arg(long)]
223 force: bool,
224 },
225
226 /// Submit a stack entry for review (shortcut for 'stacks submit')
227 Submit {
228 /// Stack entry number (1-based, defaults to all unsubmitted)
229 entry: Option<usize>,
230 /// Pull request title
231 #[arg(long, short)]
232 title: Option<String>,
233 /// Pull request description
234 #[arg(long, short)]
235 description: Option<String>,
236 /// Submit range of entries (e.g., "1-3" or "2,4,6")
237 #[arg(long)]
238 range: Option<String>,
239 /// Create draft pull requests (can be edited later)
240 #[arg(long)]
241 draft: bool,
242 },
243
244 /// Validate stack integrity and handle branch modifications (shortcut for 'stacks validate')
245 Validate {
246 /// Name of the stack (defaults to active stack)
247 name: Option<String>,
248 /// Auto-fix mode: incorporate, split, or reset
249 #[arg(long)]
250 fix: Option<String>,
251 },
252}
253
254/// Git hooks actions
255#[derive(Debug, Subcommand)]
256pub enum HooksAction {
257 /// Install all Cascade Git hooks
258 Install {
259 /// Skip prerequisite checks (repository type, configuration validation)
260 #[arg(long)]
261 skip_checks: bool,
262
263 /// Allow installation on main/master branches (not recommended)
264 #[arg(long)]
265 allow_main_branch: bool,
266
267 /// Skip confirmation prompt
268 #[arg(long, short)]
269 yes: bool,
270
271 /// Force installation even if checks fail (not recommended)
272 #[arg(long)]
273 force: bool,
274 },
275
276 /// Uninstall all Cascade Git hooks
277 Uninstall,
278
279 /// Show Git hooks status
280 Status,
281
282 /// Install a specific hook
283 Add {
284 /// Hook name (post-commit, pre-push, commit-msg, prepare-commit-msg)
285 hook: String,
286
287 /// Skip prerequisite checks
288 #[arg(long)]
289 skip_checks: bool,
290
291 /// Force installation even if checks fail
292 #[arg(long)]
293 force: bool,
294 },
295
296 /// Remove a specific hook
297 Remove {
298 /// Hook name (post-commit, pre-push, commit-msg, prepare-commit-msg)
299 hook: String,
300 },
301}
302
303/// Visualization actions
304#[derive(Debug, Subcommand)]
305pub enum VizAction {
306 /// Show stack diagram
307 Stack {
308 /// Stack name (defaults to active stack)
309 name: Option<String>,
310 /// Output format (ascii, mermaid, dot, plantuml)
311 #[arg(long, short)]
312 format: Option<String>,
313 /// Output file path
314 #[arg(long, short)]
315 output: Option<String>,
316 /// Compact mode (less details)
317 #[arg(long)]
318 compact: bool,
319 /// Disable colors
320 #[arg(long)]
321 no_colors: bool,
322 },
323
324 /// Show dependency graph of all stacks
325 Deps {
326 /// Output format (ascii, mermaid, dot, plantuml)
327 #[arg(long, short)]
328 format: Option<String>,
329 /// Output file path
330 #[arg(long, short)]
331 output: Option<String>,
332 /// Compact mode (less details)
333 #[arg(long)]
334 compact: bool,
335 /// Disable colors
336 #[arg(long)]
337 no_colors: bool,
338 },
339}
340
341/// Shell completion actions
342#[derive(Debug, Subcommand)]
343pub enum CompletionsAction {
344 /// Generate completions for a shell
345 Generate {
346 /// Shell to generate completions for
347 #[arg(value_enum)]
348 shell: Shell,
349 },
350
351 /// Install completions for available shells
352 Install {
353 /// Specific shell to install for
354 #[arg(long, value_enum)]
355 shell: Option<Shell>,
356 },
357
358 /// Show completion installation status
359 Status,
360}
361
362#[derive(Subcommand)]
363pub enum ConfigAction {
364 /// Set a configuration value
365 Set {
366 /// Configuration key (e.g., bitbucket.url)
367 key: String,
368 /// Configuration value
369 value: String,
370 },
371
372 /// Get a configuration value
373 Get {
374 /// Configuration key
375 key: String,
376 },
377
378 /// List all configuration values
379 List,
380
381 /// Remove a configuration value
382 Unset {
383 /// Configuration key
384 key: String,
385 },
386}
387
388impl Cli {
389 pub async fn run(self) -> Result<()> {
390 // Set up logging based on verbosity
391 self.setup_logging();
392
393 match self.command {
394 Commands::Init {
395 bitbucket_url,
396 force,
397 } => commands::init::run(bitbucket_url, force).await,
398 Commands::Config { action } => commands::config::run(action).await,
399 Commands::Stacks { action } => commands::stack::run(action).await,
400 Commands::Entry { action } => commands::entry::run(action).await,
401 Commands::Repo => commands::status::run().await,
402 Commands::Version => commands::version::run().await,
403 Commands::Doctor => commands::doctor::run().await,
404
405 Commands::Completions { action } => match action {
406 CompletionsAction::Generate { shell } => {
407 commands::completions::generate_completions(shell)
408 }
409 CompletionsAction::Install { shell } => {
410 commands::completions::install_completions(shell)
411 }
412 CompletionsAction::Status => commands::completions::show_completions_status(),
413 },
414
415 Commands::Setup { force } => commands::setup::run(force).await,
416
417 Commands::Tui => commands::tui::run().await,
418
419 Commands::Hooks { action } => match action {
420 HooksAction::Install {
421 skip_checks,
422 allow_main_branch,
423 yes,
424 force,
425 } => {
426 commands::hooks::install_with_options(
427 skip_checks,
428 allow_main_branch,
429 yes,
430 force,
431 )
432 .await
433 }
434 HooksAction::Uninstall => commands::hooks::uninstall().await,
435 HooksAction::Status => commands::hooks::status().await,
436 HooksAction::Add {
437 hook,
438 skip_checks,
439 force,
440 } => commands::hooks::install_hook_with_options(&hook, skip_checks, force).await,
441 HooksAction::Remove { hook } => commands::hooks::uninstall_hook(&hook).await,
442 },
443
444 Commands::Viz { action } => match action {
445 VizAction::Stack {
446 name,
447 format,
448 output,
449 compact,
450 no_colors,
451 } => {
452 commands::viz::show_stack(
453 name.clone(),
454 format.clone(),
455 output.clone(),
456 compact,
457 no_colors,
458 )
459 .await
460 }
461 VizAction::Deps {
462 format,
463 output,
464 compact,
465 no_colors,
466 } => {
467 commands::viz::show_dependencies(
468 format.clone(),
469 output.clone(),
470 compact,
471 no_colors,
472 )
473 .await
474 }
475 },
476
477 Commands::Stack { verbose, mergeable } => {
478 commands::stack::show(verbose, mergeable).await
479 }
480
481 Commands::Push {
482 branch,
483 message,
484 commit,
485 since,
486 commits,
487 squash,
488 squash_since,
489 auto_branch,
490 allow_base_branch,
491 } => {
492 commands::stack::push(
493 branch,
494 message,
495 commit,
496 since,
497 commits,
498 squash,
499 squash_since,
500 auto_branch,
501 allow_base_branch,
502 )
503 .await
504 }
505
506 Commands::Pop { keep_branch } => commands::stack::pop(keep_branch).await,
507
508 Commands::Land {
509 entry,
510 force,
511 dry_run,
512 auto,
513 wait_for_builds,
514 strategy,
515 build_timeout,
516 } => {
517 commands::stack::land(
518 entry,
519 force,
520 dry_run,
521 auto,
522 wait_for_builds,
523 strategy,
524 build_timeout,
525 )
526 .await
527 }
528
529 Commands::Autoland {
530 force,
531 dry_run,
532 wait_for_builds,
533 strategy,
534 build_timeout,
535 } => {
536 commands::stack::autoland(force, dry_run, wait_for_builds, strategy, build_timeout)
537 .await
538 }
539
540 Commands::Sync {
541 force,
542 skip_cleanup,
543 interactive,
544 } => commands::stack::sync(force, skip_cleanup, interactive).await,
545
546 Commands::Rebase {
547 interactive,
548 onto,
549 strategy,
550 } => commands::stack::rebase(interactive, onto, strategy).await,
551
552 Commands::Switch { name } => commands::stack::switch(name).await,
553
554 Commands::Deactivate { force } => commands::stack::deactivate(force).await,
555
556 Commands::Submit {
557 entry,
558 title,
559 description,
560 range,
561 draft,
562 } => {
563 // Delegate to the stacks submit functionality
564 let submit_action = StackAction::Submit {
565 entry,
566 title,
567 description,
568 range,
569 draft,
570 };
571 commands::stack::run(submit_action).await
572 }
573
574 Commands::Validate { name, fix } => {
575 // Delegate to the stacks validate functionality
576 let validate_action = StackAction::Validate { name, fix };
577 commands::stack::run(validate_action).await
578 }
579 }
580 }
581
582 fn setup_logging(&self) {
583 let level = if self.verbose {
584 tracing::Level::DEBUG
585 } else {
586 tracing::Level::INFO
587 };
588
589 let subscriber = tracing_subscriber::fmt()
590 .with_max_level(level)
591 .with_target(false)
592 .without_time();
593
594 if self.no_color {
595 subscriber.with_ansi(false).init();
596 } else {
597 subscriber.init();
598 }
599 }
600}