vibe-workspace 0.0.5

Extremely lightweight CLI for managing multiple git repositories and workspace configurations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
// Warning Denial Implementation (TEMPORARILY DISABLED):
// - Future: RUSTFLAGS="-D warnings" in release build justfile commands
// - Future: #![cfg_attr(not(debug_assertions), deny(warnings))] at crate level
// - Currently disabled until intentional dead code is properly annotated with #[allow(dead_code)]
// - To re-enable: Add RUSTFLAGS="-D warnings" to zigbuild-release and build-release commands

use anyhow::Result;
use clap::{Parser, Subcommand};
use console::style;
use std::path::PathBuf;

mod apps;
mod cache;
mod git;
mod mcp;
mod output;
mod repository;
mod ui;
mod uri;
mod utils;
mod workspace;

use ui::{prompts, state::VibeState};
use workspace::WorkspaceManager;

#[derive(Parser)]
#[command(name = "vibe")]
#[command(
    about = "Lightweight CLI for managing multiple git repositories and workspace configurations",
    long_about = "Vibe helps you manage multiple git repositories from a single place.\n\
                  Think of it as a smart launcher for your development projects.\n\n\
                  GETTING STARTED:\n  \
                  1. Run 'vibe' to start the interactive menu\n  \
                  2. Or run 'vibe setup' for first-time configuration\n  \
                  3. Clone repos with 'vibe clone owner/repo'\n  \
                  4. Open repos with 'vibe launch [name]'\n\n\
                  For detailed help: vibe guide getting-started"
)]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Enable verbose logging
    #[arg(short, long, global = true)]
    verbose: bool,

    /// Configuration file path
    #[arg(short, long, global = true)]
    config: Option<PathBuf>,

    /// Override workspace root directory
    #[arg(short, long, global = true)]
    root: Option<PathBuf>,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize workspace configuration in current directory
    Init {
        /// Workspace name
        #[arg(short, long)]
        name: Option<String>,

        /// Root directory for workspace
        #[arg(short, long)]
        root: Option<PathBuf>,
    },

    /// Manage app integrations
    Apps {
        #[command(subcommand)]
        command: AppsCommands,
    },

    /// Interactive menu system
    Menu,

    /// Create a new repository in the workspace
    Create {
        /// Repository name
        name: Option<String>,

        /// App to open with after creating
        #[arg(short, long)]
        app: Option<String>,

        /// Skip app configuration
        #[arg(long)]
        no_configure: bool,

        /// Skip opening after create
        #[arg(long)]
        no_open: bool,
    },

    /// Manage workspace configuration
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },

    /// Git repository operations
    Git {
        #[command(subcommand)]
        command: GitCommands,
    },

    /// Open repository with configured app
    Open {
        /// Repository name
        repo: String,

        /// App to open with (warp, iterm2, vscode, wezterm, cursor, windsurf)
        #[arg(short, long)]
        app: Option<String>,

        /// Disable iTermocil for iTerm2 (use Dynamic Profiles instead)
        #[arg(long)]
        no_itermocil: bool,
    },

    /// Quick launch recent repository or specific repository
    Launch {
        /// Repository name or number (1-9 for recent repos)
        #[arg(value_name = "REPO")]
        repo: Option<String>,

        /// App to open with (overrides default/last used)
        #[arg(short, long)]
        app: Option<String>,
    },

    /// Clone, configure, and open a repository in one command
    Clone {
        /// Repository URL or GitHub shorthand (owner/repo)
        url: String,

        /// App to open with after cloning
        #[arg(short, long)]
        app: Option<String>,

        /// Skip app configuration
        #[arg(long)]
        no_configure: bool,

        /// Skip opening after clone
        #[arg(long)]
        no_open: bool,
    },

    /// Run first-time setup wizard
    Setup {
        /// Skip the setup wizard
        #[arg(long)]
        skip: bool,
    },

    /// Run as MCP (Model Context Protocol) server
    Mcp {
        /// Use HTTP transport on specified port
        #[arg(long, conflicts_with = "stdio")]
        port: Option<u16>,

        /// Use stdio transport (default)
        #[arg(long, default_value = "true")]
        stdio: bool,
    },

    /// Show help for specific topics
    Guide {
        #[command(subcommand)]
        topic: HelpTopic,
    },
}

#[derive(Subcommand)]
enum HelpTopic {
    /// Getting started guide
    GettingStarted,
}

#[derive(Subcommand)]
enum AppsCommands {
    /// Configure app integration for a repository
    Configure {
        /// Repository name
        repo: String,

        /// App to configure (warp, iterm2, vscode, wezterm, cursor, windsurf)
        app: String,

        /// Template to use
        #[arg(short, long)]
        template: Option<String>,
    },

    /// Show app configurations
    Show {
        /// Filter by repository name
        #[arg(long)]
        repo: Option<String>,

        /// Filter by app name
        #[arg(long)]
        app: Option<String>,
    },

    /// Manage app templates
    Template {
        #[command(subcommand)]
        command: TemplateCommands,
    },

    /// Install developer tools and applications
    Install,
}

#[derive(Subcommand)]
enum TemplateCommands {
    /// List available templates
    List {
        /// App to list templates for
        app: String,
    },

    /// Create a new template
    Create {
        /// App to create template for
        app: String,

        /// Template name
        name: String,

        /// Source file for template content
        #[arg(short, long)]
        from_file: Option<PathBuf>,
    },

    /// Delete a template
    Delete {
        /// App to delete template from
        app: String,

        /// Template name
        name: String,
    },

    /// Update default templates with current bundled versions
    UpdateDefaults {
        /// Only update specific app's default template
        #[arg(short, long)]
        app: Option<String>,

        /// Skip confirmation prompt
        #[arg(short, long)]
        force: bool,
    },
}

#[derive(Subcommand)]
enum ConfigCommands {
    /// Initialize a new workspace configuration
    Init {
        /// Workspace name
        #[arg(short, long)]
        name: Option<String>,

        /// Workspace root directory
        #[arg(short, long)]
        root: Option<PathBuf>,

        /// Enable auto-discovery of repositories
        #[arg(short, long)]
        auto_discover: bool,
    },

    /// Edit workspace configuration with default editor
    Edit {
        /// Open editor directly without prompts
        #[arg(short, long)]
        direct: bool,
    },

    /// Show current workspace configuration
    Show {
        /// Output format: yaml (default), json, pretty
        #[arg(short, long, default_value = "yaml")]
        format: String,

        /// Show only a specific section: workspace, repositories, groups, apps
        #[arg(short, long)]
        section: Option<String>,
    },

    /// Validate workspace configuration
    Validate {
        /// Check if all repository paths exist
        #[arg(short, long)]
        check_paths: bool,

        /// Check if all remote URLs are accessible
        #[arg(short, long)]
        check_remotes: bool,

        /// Validate app integrations
        #[arg(short, long)]
        check_apps: bool,
    },

    /// Factory reset - clear all configuration and reinitialize
    Reset {
        /// Skip confirmation prompts
        #[arg(long)]
        force: bool,
    },

    /// Create backup archive of all configuration files
    Backup {
        /// Output directory for backup file
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Custom backup name (default: timestamp)
        #[arg(short, long)]
        name: Option<String>,
    },

    /// Restore configuration from backup archive
    Restore {
        /// Backup file to restore from
        #[arg(short, long)]
        backup: Option<PathBuf>,

        /// Skip confirmation prompts
        #[arg(long)]
        force: bool,
    },
}

#[derive(Subcommand)]
enum GitCommands {
    /// Scan workspace for git repositories
    Scan {
        /// Directory to scan for repositories
        path: Option<PathBuf>,

        /// Maximum depth to scan
        #[arg(long, default_value = "3")]
        depth: usize,

        /// Add newly found repositories to config
        #[arg(short, long)]
        import: bool,

        /// Re-clone missing repositories from config
        #[arg(long)]
        restore: bool,

        /// Remove missing repositories from config
        #[arg(long)]
        clean: bool,
    },

    /// Discover git repositories in directory structure (deprecated: use scan)
    Discover {
        /// Directory to scan for repositories
        path: Option<PathBuf>,

        /// Maximum depth to scan
        #[arg(short, long, default_value = "3")]
        depth: usize,

        /// Auto-add discovered repositories to config
        #[arg(short, long)]
        auto_add: bool,
    },

    /// Show status across all repositories
    Status {
        /// Show only repositories with changes
        #[arg(short, long)]
        dirty_only: bool,

        /// Output format: table, json, compact
        #[arg(short, long, default_value = "table")]
        format: String,

        /// Filter by group name
        #[arg(short, long)]
        group: Option<String>,
    },

    /// Execute git commands across repositories
    Exec {
        /// Git command to execute
        command: String,

        /// Target repositories (comma-separated)
        #[arg(short, long)]
        repos: Option<String>,

        /// Target group
        #[arg(short, long)]
        group: Option<String>,

        /// Run in parallel
        #[arg(short, long)]
        parallel: bool,
    },

    /// Sync repositories (fetch and pull)
    Sync {
        /// Only fetch, don't pull
        #[arg(short, long)]
        fetch_only: bool,

        /// Prune remote tracking branches
        #[arg(short, long)]
        prune: bool,

        /// Auto-commit dirty changes to dirty/{timestamp} branch before sync
        #[arg(short, long)]
        save_dirty: bool,

        /// Target group
        #[arg(short, long)]
        group: Option<String>,
    },

    /// Clone a repository to the workspace
    Clone {
        /// Repository URL or identifier
        url: String,

        /// Override default clone location
        #[arg(short, long)]
        path: Option<PathBuf>,

        /// Open in configured editor after cloning
        #[arg(short, long)]
        open: bool,

        /// Run post-install commands (npm install, etc.)
        #[arg(short, long)]
        install: bool,
    },

    /// Search for repositories interactively
    Search,

    /// Reset repository configuration (clear all tracked repositories)
    Reset {
        /// Skip confirmation prompt
        #[arg(long)]
        force: bool,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Determine output mode based on command
    let output_mode = match &cli.command {
        Some(Commands::Mcp { .. }) => output::OutputMode::Mcp,
        _ => output::OutputMode::Cli,
    };

    // Initialize output system (this handles tracing setup)
    output::init_with_verbosity(output_mode, cli.verbose);

    // Load or create workspace configuration
    let config_path = cli
        .config
        .unwrap_or_else(|| workspace::constants::get_default_config_path());

    let mut workspace_manager =
        WorkspaceManager::new_with_root_override(config_path.clone(), cli.root).await?;

    match cli.command {
        None => {
            // No command provided, start menu mode
            prompts::run_menu_mode(&mut workspace_manager).await?;
        }
        Some(command) => match command {
            Commands::Init { name, root } => {
                let workspace_name = name.unwrap_or_else(|| {
                    std::env::current_dir()
                        .ok()
                        .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
                        .unwrap_or_else(|| "workspace".to_string())
                });

                let workspace_root = root.unwrap_or_else(|| std::env::current_dir().unwrap());

                workspace_manager
                    .init_workspace(&workspace_name, &workspace_root)
                    .await?;

                display_println!(
                    "{} Initialized workspace '{}' in {}",
                    style("").green().bold(),
                    style(&workspace_name).cyan().bold(),
                    style(workspace_root.display()).dim()
                );
            }

            Commands::Apps { command } => match command {
                AppsCommands::Configure {
                    repo,
                    app,
                    template,
                } => {
                    let template_name = template.as_deref().unwrap_or("default");
                    workspace_manager
                        .configure_app_for_repo(&repo, &app, template_name)
                        .await?;
                    display_println!(
                        "{} Configured {} for repository '{}' with template '{}'",
                        style("").green().bold(),
                        style(&app).cyan(),
                        style(&repo).cyan(),
                        style(template_name).dim()
                    );
                }

                AppsCommands::Show { repo, app } => {
                    if let Some(repo_name) = repo {
                        let apps = workspace_manager.list_apps_for_repo(&repo_name)?;
                        display_println!(
                            "{} Apps configured for repository '{}':",
                            style("📱").blue(),
                            style(&repo_name).cyan().bold()
                        );
                        for (app_name, template) in apps {
                            display_println!(
                                "  {} {} (template: {})",
                                style("").dim(),
                                style(&app_name).green(),
                                style(&template).dim()
                            );
                        }
                    } else if let Some(app_name) = app {
                        let repos = workspace_manager.list_repos_with_app(&app_name);
                        display_println!(
                            "{} Repositories with {} configured:",
                            style("📱").blue(),
                            style(&app_name).cyan().bold()
                        );
                        for (repo, template) in repos {
                            display_println!(
                                "  {} {} (template: {})",
                                style("").dim(),
                                style(&repo.name).green(),
                                style(&template).dim()
                            );
                        }
                    } else {
                        // Show all app configurations
                        workspace_manager.show_app_configurations().await?;
                    }
                }

                AppsCommands::Template { command } => match command {
                    TemplateCommands::List { app } => {
                        let templates = workspace_manager.list_templates(&app).await?;
                        display_println!(
                            "{} Available templates for {}:",
                            style("📄").blue(),
                            style(&app).cyan().bold()
                        );
                        for template in templates {
                            display_println!("  {} {}", style("").dim(), style(&template).green());
                        }
                    }

                    TemplateCommands::Create {
                        app,
                        name,
                        from_file,
                    } => {
                        let content = if let Some(file_path) = from_file {
                            tokio::fs::read_to_string(&file_path).await?
                        } else {
                            // Use default template as starting point
                            workspace_manager.get_default_template(&app).await?
                        };

                        workspace_manager
                            .save_template(&app, &name, &content)
                            .await?;
                        display_println!(
                            "{} Created template '{}' for {}",
                            style("").green().bold(),
                            style(&name).cyan(),
                            style(&app).cyan()
                        );
                    }

                    TemplateCommands::Delete { app, name } => {
                        workspace_manager.delete_template(&app, &name).await?;
                        display_println!(
                            "{} Deleted template '{}' from {}",
                            style("").green().bold(),
                            style(&name).cyan(),
                            style(&app).cyan()
                        );
                    }

                    TemplateCommands::UpdateDefaults { app, force } => {
                        let apps_to_update = if let Some(app_name) = app {
                            vec![app_name]
                        } else {
                            vec![
                                "warp".to_string(),
                                "iterm2".to_string(),
                                "wezterm".to_string(),
                                "vscode".to_string(),
                                "cursor".to_string(),
                                "windsurf".to_string(),
                            ]
                        };

                        if !force {
                            display_println!(
                                "{} This will overwrite existing default templates for: {}",
                                style("⚠️").yellow(),
                                apps_to_update.join(", ")
                            );
                            print!("Continue? [y/N] ");
                            use std::io::{self, Write};
                            io::stdout().flush()?;

                            let mut input = String::new();
                            io::stdin().read_line(&mut input)?;

                            if !input.trim().eq_ignore_ascii_case("y") {
                                display_println!("{} Update cancelled", style("ℹ️").blue());
                                return Ok(());
                            }
                        }

                        workspace_manager
                            .update_default_templates(apps_to_update)
                            .await?;
                        display_println!(
                            "{} Updated default templates with current bundled versions",
                            style("").green().bold()
                        );
                    }
                },

                AppsCommands::Install => {
                    // Run the interactive installer
                    apps::run_interactive_installer().await?;
                }
            },

            Commands::Menu => {
                prompts::run_menu_mode(&mut workspace_manager).await?;
            }

            Commands::Create {
                name,
                app,
                no_configure,
                no_open,
            } => {
                use ui::workflows::{execute_workflow, CreateRepositoryWorkflow};

                // Use workflow system for repository creation
                let workflow = Box::new(CreateRepositoryWorkflow {
                    suggested_name: name,
                    app,
                    skip_configure: no_configure,
                    skip_open: no_open,
                });

                execute_workflow(workflow, &mut workspace_manager).await?;
            }

            Commands::Config { command } => match command {
                ConfigCommands::Init {
                    name,
                    root,
                    auto_discover,
                } => {
                    workspace_manager
                        .init_config(name.as_deref(), root.as_deref(), auto_discover)
                        .await?;
                }

                ConfigCommands::Edit { direct } => {
                    workspace_manager.edit_config(direct).await?;
                }

                ConfigCommands::Show { format, section } => {
                    workspace_manager
                        .show_config(&format, section.as_deref())
                        .await?;
                }

                ConfigCommands::Validate {
                    check_paths,
                    check_remotes,
                    check_apps,
                } => {
                    workspace_manager
                        .validate_config(check_paths, check_remotes, check_apps)
                        .await?;
                }

                ConfigCommands::Reset { force } => {
                    workspace_manager.factory_reset(force).await?;
                }

                ConfigCommands::Backup { output, name } => {
                    let backup_path = workspace_manager.create_backup(output, name).await?;
                    display_println!(
                        "{} Backup created successfully: {}",
                        style("").green().bold(),
                        style(backup_path.display()).cyan()
                    );
                }

                ConfigCommands::Restore { backup, force } => {
                    workspace_manager.restore_from_backup(backup, force).await?;
                }
            },

            Commands::Git { command } => match command {
                GitCommands::Scan {
                    path,
                    depth,
                    import,
                    restore,
                    clean,
                } => {
                    // Validate conflicting flags
                    if restore && clean {
                        anyhow::bail!("Cannot use --restore and --clean together");
                    }

                    let scan_path =
                        path.unwrap_or_else(|| workspace_manager.get_workspace_root().clone());

                    workspace_manager
                        .scan_repositories(&scan_path, depth, import, restore, clean)
                        .await?;
                }

                GitCommands::Discover {
                    path,
                    depth,
                    auto_add,
                } => {
                    // Show deprecation warning
                    display_println!(
                        "{} The 'discover' command is deprecated. Use 'scan --import' instead.",
                        style("⚠️").yellow()
                    );

                    let scan_path =
                        path.unwrap_or_else(|| workspace_manager.get_workspace_root().clone());

                    display_println!(
                        "{} Discovering repositories in {} (depth: {})",
                        style("🔍").blue(),
                        style(scan_path.display()).cyan(),
                        depth
                    );

                    let repos = workspace_manager
                        .discover_repositories(&scan_path, depth)
                        .await?;

                    if repos.is_empty() {
                        display_println!("{} No git repositories found", style("").yellow());
                        return Ok(());
                    }

                    display_println!(
                        "\n{} Found {} repositories:",
                        style("📁").green(),
                        style(repos.len()).bold()
                    );

                    for repo in &repos {
                        display_println!("  {} {}", style("").dim(), style(repo.display()).cyan());
                    }

                    if auto_add {
                        workspace_manager
                            .add_discovered_repositories(&repos)
                            .await?;
                        display_println!(
                            "\n{} Added repositories to workspace configuration",
                            style("").green().bold()
                        );
                    } else {
                        display_println!(
                            "\n{} Run with --auto-add to add these to your workspace",
                            style("💡").yellow()
                        );
                    }
                }

                GitCommands::Status {
                    dirty_only,
                    format,
                    group,
                } => {
                    workspace_manager
                        .show_status(dirty_only, &format, group.as_deref())
                        .await?;
                }

                GitCommands::Exec {
                    command,
                    repos,
                    group,
                    parallel,
                } => {
                    workspace_manager
                        .execute_command(&command, repos.as_deref(), group.as_deref(), parallel)
                        .await?;
                }

                GitCommands::Sync {
                    fetch_only,
                    prune,
                    save_dirty,
                    group,
                } => {
                    workspace_manager
                        .sync_repositories(fetch_only, prune, save_dirty, group.as_deref())
                        .await?;
                }

                GitCommands::Clone {
                    url,
                    path,
                    open,
                    install,
                } => {
                    let git_config = git::GitConfig::default();
                    let _cloned_path = git::CloneCommand::execute(
                        url,
                        path,
                        open,
                        install,
                        &mut workspace_manager,
                        &git_config,
                    )
                    .await?;
                }

                GitCommands::Search => {
                    let git_config = git::GitConfig::default();
                    git::SearchCommand::execute_interactive(&mut workspace_manager, &git_config)
                        .await?;
                }

                GitCommands::Reset { force } => {
                    workspace_manager.reset_repositories(force).await?;
                }
            },

            Commands::Open {
                repo,
                app,
                no_itermocil,
            } => {
                if let Some(app_name) = app {
                    // Open with specific app
                    workspace_manager
                        .open_repo_with_app_options(&repo, &app_name, no_itermocil)
                        .await?;
                } else {
                    // Open with default or show available apps
                    let apps = workspace_manager.list_apps_for_repo(&repo)?;
                    if apps.is_empty() {
                        display_println!(
                        "{} No apps configured for repository '{}'. Configure with: vibe apps configure {} <app>",
                        style("⚠️").yellow(),
                        style(&repo).cyan(),
                        style(&repo).cyan()
                    );
                    } else if apps.len() == 1 {
                        // Only one app configured, use it
                        let (app_name, _) = &apps[0];
                        workspace_manager
                            .open_repo_with_app(&repo, app_name)
                            .await?;
                    } else {
                        // Multiple apps configured, show options
                        display_println!(
                            "{} Multiple apps configured for '{}'. Please specify one:",
                            style("🤔").yellow(),
                            style(&repo).cyan()
                        );
                        for (app_name, _template) in &apps {
                            display_println!(
                                "  {} vibe open {} --app {}",
                                style("").dim(),
                                &repo,
                                style(app_name).green()
                            );
                        }
                    }
                }
            }

            Commands::Launch { repo, app } => {
                // Load state to get recent repos
                let mut state = VibeState::load().unwrap_or_default();

                let repo_to_open = if let Some(repo_name) = repo {
                    // Check if it's a number (1-9) for recent repos
                    if let Ok(num) = repo_name.parse::<usize>() {
                        if num >= 1 && num <= 9 {
                            let recent_repos = state.get_recent_repos(15);
                            if num <= recent_repos.len() {
                                recent_repos[num - 1].repo_id.clone()
                            } else {
                                anyhow::bail!("No recent repository at position {}", num);
                            }
                        } else {
                            repo_name
                        }
                    } else {
                        repo_name
                    }
                } else {
                    // No repo specified, open the most recent one
                    let recent_repos = state.get_recent_repos(1);
                    if recent_repos.is_empty() {
                        anyhow::bail!(
                            "No recent repositories found. Use 'vibe' to browse repositories."
                        );
                    }
                    recent_repos[0].repo_id.clone()
                };

                // Get the repository info
                let repo_info = workspace_manager
                    .get_repository(&repo_to_open)
                    .ok_or_else(|| anyhow::anyhow!("Repository '{}' not found", repo_to_open))?;

                // Determine which app to use
                let app_to_use = if let Some(app_name) = app {
                    app_name
                } else if let Some(last_app) = state.get_last_app(&repo_to_open) {
                    last_app.clone()
                } else {
                    // Get configured apps and use first one
                    let apps = workspace_manager.list_apps_for_repo(&repo_to_open)?;
                    if apps.is_empty() {
                        anyhow::bail!("No apps configured for repository '{}'", repo_to_open);
                    }
                    apps[0].0.clone()
                };

                // Open the repository
                workspace_manager
                    .open_repo_with_app(&repo_to_open, &app_to_use)
                    .await?;

                // Update state with this access
                state.add_recent_repo(
                    repo_to_open.clone(),
                    repo_info.path.clone(),
                    Some(app_to_use.clone()),
                );
                state.save()?;

                display_println!(
                    "{} Launched {} with {}",
                    style("🚀").green(),
                    style(&repo_to_open).cyan().bold(),
                    style(&app_to_use).blue()
                );
            }

            Commands::Clone {
                url,
                app,
                no_configure,
                no_open,
            } => {
                use ui::workflows::{execute_workflow, CloneWorkflow};

                // Use workflow system if not skipping steps
                if !no_configure || !no_open {
                    let workflow = Box::new(CloneWorkflow {
                        url: url.clone(),
                        app: app.clone(),
                    });

                    execute_workflow(workflow, &mut workspace_manager).await?;
                } else {
                    // Just clone without workflow
                    let git_config = git::GitConfig::default();
                    let _cloned_path = git::CloneCommand::execute(
                        url,
                        None,
                        false,
                        false,
                        &mut workspace_manager,
                        &git_config,
                    )
                    .await?;

                    display_println!(
                        "{} Repository cloned successfully!",
                        style("").green().bold()
                    );
                }
            }

            Commands::Setup { skip } => {
                if skip {
                    let mut state = VibeState::load().unwrap_or_default();
                    state.complete_setup_wizard();
                    state.save()?;
                    display_println!("{} Setup wizard skipped", style("ℹ️").blue());
                } else {
                    use ui::workflows::{execute_workflow, SetupWorkspaceWorkflow};

                    // Run setup workflow
                    let workflow = Box::new(SetupWorkspaceWorkflow {
                        auto_discover: true,
                    });

                    execute_workflow(workflow, &mut workspace_manager).await?;

                    // Mark setup as complete
                    let mut state = VibeState::load().unwrap_or_default();
                    state.complete_setup_wizard();
                    state.save()?;
                }
            }

            Commands::Mcp { port, stdio: _ } => {
                use std::sync::Arc;
                use tokio::sync::Mutex;

                // Create shared workspace manager for MCP server
                let shared_workspace = Arc::new(Mutex::new(workspace_manager));

                if let Some(port_num) = port {
                    // HTTP transport not implemented in this example
                    // You would need to implement HTTP transport support
                    display_eprintln!(
                        "{} HTTP transport on port {} not yet implemented",
                        style("⚠️").yellow(),
                        port_num
                    );
                    display_eprintln!(
                        "{} Use --stdio flag for stdio transport",
                        style("💡").blue()
                    );
                    std::process::exit(1);
                } else {
                    // Run with stdio transport (default)
                    display_eprintln!(
                        "{} Starting vibe-workspace MCP server (stdio transport)...",
                        style("🚀").green()
                    );

                    let mcp_server = mcp::VibeMCPServer::new(shared_workspace);
                    mcp_server.run().await?;
                }
            }

            Commands::Guide { topic } => match topic {
                HelpTopic::GettingStarted => {
                    print_getting_started_guide();
                }
            },
        },
    }

    Ok(())
}

/// Print the getting started guide
fn print_getting_started_guide() {
    display_println!("{}", style("🚀 Getting Started with Vibe").cyan().bold());
    display_println!("{}", style("".repeat(40)).dim());
    display_println!();

    display_println!("{}", style("What is Vibe?").yellow().bold());
    display_println!("Vibe helps you manage multiple git repositories from a single place.");
    display_println!("Think of it as a smart launcher for your development projects.");
    display_println!();

    display_println!("{}", style("Quick Start").yellow().bold());
    display_println!(
        "1. {} - Start the interactive menu (recommended)",
        style("vibe").cyan().bold()
    );
    display_println!("2. {} - Run the setup wizard", style("vibe setup").cyan());
    display_println!(
        "3. {} - Clone and open a GitHub repo",
        style("vibe go owner/repo").cyan()
    );
    display_println!();

    display_println!("{}", style("Basic Commands").yellow().bold());
    display_println!(
        "  {} - Interactive menu with quick launch",
        style("vibe").cyan()
    );
    display_println!(
        "  {} - Clone, configure, and open in one command",
        style("vibe go <url>").cyan()
    );
    display_println!(
        "  {} - Open a specific repository",
        style("vibe launch <name>").cyan()
    );
    display_println!(
        "  {} - Quick open recent (use 1-9)",
        style("vibe launch <number>").cyan()
    );
    display_println!(
        "  {} - Open repository with specific app",
        style("vibe open <repo> -a <app>").cyan()
    );
    display_println!();

    display_println!("{}", style("Repository Management").yellow().bold());
    display_println!(
        "  {} - Clone a repository",
        style("vibe git clone <url>").cyan()
    );
    display_println!(
        "  {} - Search GitHub repositories",
        style("vibe git search <query>").cyan()
    );
    display_println!(
        "  {} - Scan directory for repositories",
        style("vibe git scan [path]").cyan()
    );
    display_println!(
        "  {} - Show git status across all repos",
        style("vibe git status").cyan()
    );
    display_println!(
        "  {} - Sync all repositories",
        style("vibe git sync").cyan()
    );
    display_println!();

    display_println!("{}", style("App Configuration").yellow().bold());
    display_println!(
        "  {} - Configure app for repository",
        style("vibe apps configure <repo>").cyan()
    );
    display_println!(
        "  {} - Install app integrations",
        style("vibe apps install").cyan()
    );
    display_println!(
        "  {} - Show app configurations",
        style("vibe apps show").cyan()
    );
    display_println!();

    display_println!("{}", style("Tips").yellow().bold());
    display_println!(
        "• In the menu, press {} to quickly open recent repositories",
        style("1-9").cyan()
    );
    display_println!("• Apps are your development tools (VS Code, iTerm2, etc.)");
    display_println!("• Templates define how to open your repo in each app");
    display_println!(
        "• Run {} to see all available commands",
        style("vibe --help").cyan()
    );
    display_println!();

    display_println!("{}", style("Next Steps").green().bold());
    display_println!("1. Run {} to start exploring", style("vibe").cyan().bold());
    display_println!(
        "2. Clone your first repo with {}",
        style("vibe go <owner/repo>").cyan()
    );
    display_println!("3. Configure your favorite apps");
    display_println!();
}