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 = "cc")]
12#[command(about = "Cascade CLI - Stacked diffs for Bitbucket Server")]
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
227/// Git hooks actions
228#[derive(Debug, Subcommand)]
229pub enum HooksAction {
230    /// Install all Cascade Git hooks
231    Install {
232        /// Skip prerequisite checks (repository type, configuration validation)
233        #[arg(long)]
234        skip_checks: bool,
235
236        /// Allow installation on main/master branches (not recommended)
237        #[arg(long)]
238        allow_main_branch: bool,
239
240        /// Skip confirmation prompt
241        #[arg(long, short)]
242        yes: bool,
243
244        /// Force installation even if checks fail (not recommended)
245        #[arg(long)]
246        force: bool,
247    },
248
249    /// Uninstall all Cascade Git hooks
250    Uninstall,
251
252    /// Show Git hooks status
253    Status,
254
255    /// Install a specific hook
256    Add {
257        /// Hook name (post-commit, pre-push, commit-msg, prepare-commit-msg)
258        hook: String,
259
260        /// Skip prerequisite checks
261        #[arg(long)]
262        skip_checks: bool,
263
264        /// Force installation even if checks fail
265        #[arg(long)]
266        force: bool,
267    },
268
269    /// Remove a specific hook
270    Remove {
271        /// Hook name (post-commit, pre-push, commit-msg, prepare-commit-msg)
272        hook: String,
273    },
274}
275
276/// Visualization actions
277#[derive(Debug, Subcommand)]
278pub enum VizAction {
279    /// Show stack diagram
280    Stack {
281        /// Stack name (defaults to active stack)
282        name: Option<String>,
283        /// Output format (ascii, mermaid, dot, plantuml)
284        #[arg(long, short)]
285        format: Option<String>,
286        /// Output file path
287        #[arg(long, short)]
288        output: Option<String>,
289        /// Compact mode (less details)
290        #[arg(long)]
291        compact: bool,
292        /// Disable colors
293        #[arg(long)]
294        no_colors: bool,
295    },
296
297    /// Show dependency graph of all stacks
298    Deps {
299        /// Output format (ascii, mermaid, dot, plantuml)
300        #[arg(long, short)]
301        format: Option<String>,
302        /// Output file path
303        #[arg(long, short)]
304        output: Option<String>,
305        /// Compact mode (less details)
306        #[arg(long)]
307        compact: bool,
308        /// Disable colors
309        #[arg(long)]
310        no_colors: bool,
311    },
312}
313
314/// Shell completion actions
315#[derive(Debug, Subcommand)]
316pub enum CompletionsAction {
317    /// Generate completions for a shell
318    Generate {
319        /// Shell to generate completions for
320        #[arg(value_enum)]
321        shell: Shell,
322    },
323
324    /// Install completions for available shells
325    Install {
326        /// Specific shell to install for
327        #[arg(long, value_enum)]
328        shell: Option<Shell>,
329    },
330
331    /// Show completion installation status
332    Status,
333}
334
335#[derive(Subcommand)]
336pub enum ConfigAction {
337    /// Set a configuration value
338    Set {
339        /// Configuration key (e.g., bitbucket.url)
340        key: String,
341        /// Configuration value
342        value: String,
343    },
344
345    /// Get a configuration value
346    Get {
347        /// Configuration key
348        key: String,
349    },
350
351    /// List all configuration values
352    List,
353
354    /// Remove a configuration value
355    Unset {
356        /// Configuration key
357        key: String,
358    },
359}
360
361impl Cli {
362    pub async fn run(self) -> Result<()> {
363        // Set up logging based on verbosity
364        self.setup_logging();
365
366        match self.command {
367            Commands::Init {
368                bitbucket_url,
369                force,
370            } => commands::init::run(bitbucket_url, force).await,
371            Commands::Config { action } => commands::config::run(action).await,
372            Commands::Stacks { action } => commands::stack::run(action).await,
373            Commands::Entry { action } => commands::entry::run(action).await,
374            Commands::Repo => commands::status::run().await,
375            Commands::Version => commands::version::run().await,
376            Commands::Doctor => commands::doctor::run().await,
377
378            Commands::Completions { action } => match action {
379                CompletionsAction::Generate { shell } => {
380                    commands::completions::generate_completions(shell)
381                }
382                CompletionsAction::Install { shell } => {
383                    commands::completions::install_completions(shell)
384                }
385                CompletionsAction::Status => commands::completions::show_completions_status(),
386            },
387
388            Commands::Setup { force } => commands::setup::run(force).await,
389
390            Commands::Tui => commands::tui::run().await,
391
392            Commands::Hooks { action } => match action {
393                HooksAction::Install {
394                    skip_checks,
395                    allow_main_branch,
396                    yes,
397                    force,
398                } => {
399                    commands::hooks::install_with_options(
400                        skip_checks,
401                        allow_main_branch,
402                        yes,
403                        force,
404                    )
405                    .await
406                }
407                HooksAction::Uninstall => commands::hooks::uninstall().await,
408                HooksAction::Status => commands::hooks::status().await,
409                HooksAction::Add {
410                    hook,
411                    skip_checks,
412                    force,
413                } => commands::hooks::install_hook_with_options(&hook, skip_checks, force).await,
414                HooksAction::Remove { hook } => commands::hooks::uninstall_hook(&hook).await,
415            },
416
417            Commands::Viz { action } => match action {
418                VizAction::Stack {
419                    name,
420                    format,
421                    output,
422                    compact,
423                    no_colors,
424                } => {
425                    commands::viz::show_stack(
426                        name.clone(),
427                        format.clone(),
428                        output.clone(),
429                        compact,
430                        no_colors,
431                    )
432                    .await
433                }
434                VizAction::Deps {
435                    format,
436                    output,
437                    compact,
438                    no_colors,
439                } => {
440                    commands::viz::show_dependencies(
441                        format.clone(),
442                        output.clone(),
443                        compact,
444                        no_colors,
445                    )
446                    .await
447                }
448            },
449
450            Commands::Stack { verbose, mergeable } => {
451                commands::stack::show(verbose, mergeable).await
452            }
453
454            Commands::Push {
455                branch,
456                message,
457                commit,
458                since,
459                commits,
460                squash,
461                squash_since,
462                auto_branch,
463                allow_base_branch,
464            } => {
465                commands::stack::push(
466                    branch,
467                    message,
468                    commit,
469                    since,
470                    commits,
471                    squash,
472                    squash_since,
473                    auto_branch,
474                    allow_base_branch,
475                )
476                .await
477            }
478
479            Commands::Pop { keep_branch } => commands::stack::pop(keep_branch).await,
480
481            Commands::Land {
482                entry,
483                force,
484                dry_run,
485                auto,
486                wait_for_builds,
487                strategy,
488                build_timeout,
489            } => {
490                commands::stack::land(
491                    entry,
492                    force,
493                    dry_run,
494                    auto,
495                    wait_for_builds,
496                    strategy,
497                    build_timeout,
498                )
499                .await
500            }
501
502            Commands::Autoland {
503                force,
504                dry_run,
505                wait_for_builds,
506                strategy,
507                build_timeout,
508            } => {
509                commands::stack::autoland(force, dry_run, wait_for_builds, strategy, build_timeout)
510                    .await
511            }
512
513            Commands::Sync {
514                force,
515                skip_cleanup,
516                interactive,
517            } => commands::stack::sync(force, skip_cleanup, interactive).await,
518
519            Commands::Rebase {
520                interactive,
521                onto,
522                strategy,
523            } => commands::stack::rebase(interactive, onto, strategy).await,
524
525            Commands::Switch { name } => commands::stack::switch(name).await,
526
527            Commands::Deactivate { force } => commands::stack::deactivate(force).await,
528        }
529    }
530
531    fn setup_logging(&self) {
532        let level = if self.verbose {
533            tracing::Level::DEBUG
534        } else {
535            tracing::Level::INFO
536        };
537
538        let subscriber = tracing_subscriber::fmt()
539            .with_max_level(level)
540            .with_target(false)
541            .without_time();
542
543        if self.no_color {
544            subscriber.with_ansi(false).init();
545        } else {
546            subscriber.init();
547        }
548    }
549}