stout 0.2.1

A fast, Rust-based Homebrew-compatible package manager
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
//! Mirror command - create and serve offline mirrors

use anyhow::{bail, Context, Result};
use clap::{Args as ClapArgs, Subcommand};
use console::style;
use humansize::{format_size, BINARY};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use stout_index::Database;
use stout_mirror::{
    create_mirror, detect_platform, serve_mirror, MirrorConfig, MirrorManifest, ServeConfig,
};
use stout_state::Paths;

/// Verify SHA256 checksum of a file
fn verify_file_checksum(path: &Path, expected: &str) -> Result<()> {
    let data = std::fs::read(path)?;
    let mut hasher = Sha256::new();
    hasher.update(&data);
    let actual = hex::encode(hasher.finalize());

    if actual == expected {
        Ok(())
    } else {
        bail!("expected {}, got {}", expected, actual)
    }
}

#[derive(ClapArgs)]
pub struct Args {
    #[command(subcommand)]
    pub command: MirrorCommand,
}

#[derive(Subcommand)]
pub enum MirrorCommand {
    /// Create a new mirror with specified packages
    Create {
        /// Output directory for the mirror
        output: PathBuf,

        /// Packages to include
        #[arg(required_unless_present = "all_installed")]
        packages: Vec<String>,

        /// Include all installed packages
        #[arg(long)]
        all_installed: bool,

        /// Create from Brewfile
        #[arg(long = "from-brewfile")]
        brewfile: Option<PathBuf>,

        /// Casks to include
        #[arg(long = "cask")]
        casks: Vec<String>,

        /// Linux apps to include
        #[arg(long = "linux-app")]
        linux_apps: Vec<String>,

        /// Platforms to include (default: current platform)
        #[arg(long)]
        platforms: Vec<String>,

        /// Include all platforms (warning: large download)
        #[arg(long)]
        all_platforms: bool,

        /// Skip dependency resolution
        #[arg(long)]
        no_deps: bool,

        /// Show what would be downloaded without actually downloading
        #[arg(long)]
        dry_run: bool,
    },

    /// Serve a mirror via HTTP
    Serve {
        /// Path to the mirror directory
        path: PathBuf,

        /// Port to listen on
        #[arg(long, short, default_value = "8080")]
        port: u16,

        /// Address to bind to
        #[arg(long, default_value = "0.0.0.0")]
        bind: String,

        /// Enable access logging
        #[arg(long)]
        log_access: bool,
    },

    /// Show information about a mirror
    Info {
        /// Path to the mirror directory
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Check for outdated packages in mirror
    Outdated {
        /// Path to the mirror directory
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Update packages in an existing mirror
    Update {
        /// Path to the mirror directory
        path: PathBuf,

        /// Specific packages to update (default: all)
        packages: Vec<String>,

        /// Update from Brewfile
        #[arg(long = "from-brewfile")]
        brewfile: Option<PathBuf>,

        /// Show what would be updated without actually updating
        #[arg(long)]
        dry_run: bool,
    },

    /// Remove old versions from mirror
    Prune {
        /// Path to the mirror directory
        path: PathBuf,

        /// Number of versions to keep per package
        #[arg(long, default_value = "1")]
        keep: usize,

        /// Show what would be removed without removing
        #[arg(long)]
        dry_run: bool,
    },

    /// Verify mirror integrity
    Verify {
        /// Path to the mirror directory
        path: PathBuf,

        /// Specific packages to verify (default: all)
        packages: Vec<String>,

        /// Show verbose output
        #[arg(long, short)]
        verbose: bool,
    },
}

pub async fn run(args: Args) -> Result<()> {
    match args.command {
        MirrorCommand::Create {
            output,
            packages,
            all_installed,
            brewfile,
            casks,
            linux_apps,
            platforms,
            all_platforms,
            no_deps,
            dry_run,
        } => {
            run_create(
                output,
                packages,
                all_installed,
                brewfile,
                casks,
                linux_apps,
                platforms,
                all_platforms,
                no_deps,
                dry_run,
            )
            .await
        }

        MirrorCommand::Serve {
            path,
            port,
            bind,
            log_access,
        } => run_serve(path, port, bind, log_access).await,

        MirrorCommand::Info { path, json } => run_info(path, json).await,

        MirrorCommand::Outdated { path, json } => run_outdated(path, json).await,

        MirrorCommand::Update {
            path,
            packages,
            brewfile,
            dry_run,
        } => run_update(path, packages, brewfile, dry_run).await,

        MirrorCommand::Prune {
            path,
            keep,
            dry_run,
        } => run_prune(path, keep, dry_run).await,

        MirrorCommand::Verify {
            path,
            packages,
            verbose,
        } => run_verify(path, packages, verbose).await,
    }
}

#[allow(clippy::too_many_arguments)]
async fn run_create(
    output: PathBuf,
    packages: Vec<String>,
    all_installed: bool,
    brewfile: Option<PathBuf>,
    casks: Vec<String>,
    linux_apps: Vec<String>,
    platforms: Vec<String>,
    all_platforms: bool,
    no_deps: bool,
    dry_run: bool,
) -> Result<()> {
    let paths = Paths::default();
    let db = Database::open(paths.index_db())?;

    // Collect packages
    let mut pkgs = packages;

    if all_installed {
        let installed = stout_state::InstalledPackages::load(&paths)?;
        for name in installed.names() {
            pkgs.push(name.clone());
        }
    }

    if pkgs.is_empty() && casks.is_empty() && linux_apps.is_empty() {
        bail!("No packages specified. Use --all-installed or provide package names.");
    }

    // Determine platforms
    let target_platforms = if all_platforms {
        vec![
            "arm64_sonoma".to_string(),
            "arm64_ventura".to_string(),
            "arm64_monterey".to_string(),
            "x86_64_sonoma".to_string(),
            "x86_64_ventura".to_string(),
            "x86_64_monterey".to_string(),
            "x86_64_linux".to_string(),
            "aarch64_linux".to_string(),
        ]
    } else if platforms.is_empty() {
        vec![detect_platform()]
    } else {
        platforms
    };

    println!(
        "\n{} mirror at {}\n",
        style("Creating").cyan().bold(),
        output.display()
    );

    if dry_run {
        println!("{}", style("DRY RUN - no files will be written").yellow());
    }

    println!("  Formulas: {}", pkgs.len());
    println!("  Casks: {}", casks.len());
    println!("  Linux apps: {}", linux_apps.len());
    println!("  Platforms: {:?}", target_platforms);
    println!("  Include deps: {}", !no_deps);
    println!();

    if dry_run {
        // Show what would be included
        println!("{}", style("Would include:").bold());
        for pkg in &pkgs {
            println!("  - {}", pkg);
        }
        return Ok(());
    }

    let config = MirrorConfig {
        output,
        packages: pkgs,
        casks,
        linux_apps,
        platforms: target_platforms,
        include_deps: !no_deps,
        brewfile,
    };

    let manifest = create_mirror(config, &db).await?;

    println!();
    println!("{}", style("Mirror created successfully!").green().bold());
    println!("  {} formulas", manifest.formulas.count);
    println!("  {} total size", format_size(manifest.total_size, BINARY));

    Ok(())
}

async fn run_serve(path: PathBuf, port: u16, bind: String, log_access: bool) -> Result<()> {
    if !path.exists() {
        bail!("Mirror directory not found: {}", path.display());
    }

    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!(
            "Invalid mirror: missing manifest.json at {}",
            path.display()
        );
    }

    println!(
        "\n{} mirror from {}\n",
        style("Serving").cyan().bold(),
        path.display()
    );

    let config = ServeConfig {
        mirror_path: path,
        port,
        bind,
        log_access,
    };

    serve_mirror(config).await?;

    Ok(())
}

async fn run_info(path: PathBuf, json: bool) -> Result<()> {
    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!("Invalid mirror: missing manifest.json");
    }

    let manifest = MirrorManifest::load(&manifest_path)?;

    if json {
        println!("{}", serde_json::to_string_pretty(&manifest)?);
    } else {
        println!("\n{} Information\n", style("Mirror").cyan().bold());
        println!("  Version: {}", manifest.version);
        println!("  Created: {}", manifest.created_at);
        println!("  stout version: {}", manifest.stout_version);
        println!();
        println!("  Formulas: {}", manifest.formulas.count);
        println!("  Casks: {}", manifest.casks.count);
        println!("  Linux apps: {}", manifest.linux_apps.count);
        println!();
        println!("  Platforms: {:?}", manifest.platforms);
        println!("  Total size: {}", format_size(manifest.total_size, BINARY));
        println!();
    }

    Ok(())
}

async fn run_outdated(path: PathBuf, json: bool) -> Result<()> {
    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!("Invalid mirror: missing manifest.json");
    }

    let manifest = MirrorManifest::load(&manifest_path)?;
    let paths = Paths::default();
    let db = Database::open(paths.index_db())?;

    println!(
        "\n{} for outdated packages in mirror...\n",
        style("Checking").cyan().bold()
    );

    let mut outdated = Vec::new();

    for (name, info) in &manifest.formulas.packages {
        if let Ok(Some(formula)) = db.get_formula(name) {
            if formula.version != info.version {
                outdated.push((name.clone(), info.version.clone(), formula.version.clone()));
            }
        }
    }

    if json {
        let output: Vec<_> = outdated
            .iter()
            .map(|(name, mirror_ver, latest_ver)| {
                serde_json::json!({
                    "name": name,
                    "mirror_version": mirror_ver,
                    "latest_version": latest_ver
                })
            })
            .collect();
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else if outdated.is_empty() {
        println!("{}", style("All packages are up to date!").green());
    } else {
        println!("{} packages outdated:\n", outdated.len());
        for (name, mirror_ver, latest_ver) in &outdated {
            println!(
                "  {} {} -> {}",
                style(name).white().bold(),
                style(mirror_ver).red(),
                style(latest_ver).green()
            );
        }
        println!();
        println!(
            "Run '{}' to update",
            style(format!("stout mirror update {}", path.display())).cyan()
        );
    }

    Ok(())
}

async fn run_update(
    path: PathBuf,
    packages: Vec<String>,
    _brewfile: Option<PathBuf>,
    dry_run: bool,
) -> Result<()> {
    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!("Invalid mirror: missing manifest.json");
    }

    let manifest = MirrorManifest::load(&manifest_path)?;

    println!(
        "\n{} mirror at {}\n",
        style("Updating").cyan().bold(),
        path.display()
    );

    if dry_run {
        println!("{}", style("DRY RUN - no files will be modified").yellow());
    }

    // Load the index to check for updates
    let paths = Paths::default();
    let db = Database::open(paths.index_db())
        .context("Failed to open index. Run 'stout update' first.")?;

    if !db.is_initialized()? {
        bail!("Index not initialized. Run 'stout update' first.");
    }

    // Determine which packages to update
    let packages_to_update: Vec<&String> = if packages.is_empty() {
        manifest.formulas.packages.keys().collect()
    } else {
        packages.iter().collect()
    };

    let mut updated = 0;
    let mut skipped = 0;

    for name in &packages_to_update {
        if let Some(current_info) = manifest.formulas.packages.get(*name) {
            // Get latest from index
            let latest = db.get_formula(name)?;

            match latest {
                Some(latest_formula) => {
                    if latest_formula.version != current_info.version
                        || latest_formula.revision != current_info.revision
                    {
                        println!(
                            "  {} {} -> {}",
                            style(name).white().bold(),
                            style(&current_info.version).red(),
                            style(&latest_formula.version).green()
                        );

                        if !dry_run {
                            // TODO: Implement bottle download and manifest update
                            // This requires integrating with stout-fetch for downloading
                            // and updating the manifest.json with new checksums
                            println!("    (update not yet implemented - run 'stout mirror create' to rebuild)");
                        }
                        updated += 1;
                    } else {
                        if verbose_output() {
                            println!("  {} is up to date", name);
                        }
                        skipped += 1;
                    }
                }
                None => {
                    println!(
                        "  {} {} (removed from upstream)",
                        style(name).white().bold(),
                        current_info.version
                    );
                    skipped += 1;
                }
            }
        } else if packages.is_empty() {
            // Package not in manifest, skip
            continue;
        } else {
            bail!("Package {} not found in mirror", name);
        }
    }

    println!();
    if dry_run {
        println!(
            "{} Would update {} packages, skip {}",
            style("DRY RUN:").yellow(),
            updated,
            skipped
        );
    } else {
        println!(
            "{} {} packages updated, {} skipped",
            style("").green(),
            updated,
            skipped
        );
        println!(
            "{}",
            style("Note: Package download not yet implemented").yellow()
        );
    }

    Ok(())
}

/// Helper to check if verbose output is enabled
fn verbose_output() -> bool {
    std::env::var("STOUT_VERBOSE").is_ok()
}

async fn run_prune(path: PathBuf, keep: usize, dry_run: bool) -> Result<()> {
    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!("Invalid mirror: missing manifest.json");
    }

    let manifest = MirrorManifest::load(&manifest_path)?;

    println!(
        "\n{} mirror at {} (keeping {} versions)\n",
        style("Pruning").cyan().bold(),
        path.display(),
        keep
    );

    if dry_run {
        println!("{}", style("DRY RUN - no files will be removed").yellow());
    }

    // Collect all files referenced in the manifest
    let mut referenced_files: std::collections::HashSet<String> = std::collections::HashSet::new();

    // Add formula JSON files
    for info in manifest.formulas.packages.values() {
        referenced_files.insert(info.json_path.clone());
        for bottle in info.bottles.values() {
            referenced_files.insert(bottle.path.clone());
        }
    }

    // Add cask files
    for info in manifest.casks.packages.values() {
        referenced_files.insert(info.json_path.clone());
        if let Some(artifact) = &info.artifact {
            referenced_files.insert(artifact.path.clone());
        }
    }

    // Find orphaned files (in mirror but not in manifest)
    let mut orphaned: Vec<PathBuf> = Vec::new();

    // Check formulas directory
    let formulas_dir = path.join("formulas");
    if formulas_dir.exists() {
        for entry in std::fs::read_dir(&formulas_dir)? {
            let entry = entry?;
            let rel_path = entry
                .path()
                .strip_prefix(&path)?
                .to_string_lossy()
                .to_string();
            if !referenced_files.contains(&rel_path) {
                orphaned.push(entry.path());
            }
        }
    }

    // Check bottles directory
    let bottles_dir = path.join("bottles");
    if bottles_dir.exists() {
        for entry in std::fs::read_dir(&bottles_dir)? {
            let entry = entry?;
            let rel_path = entry
                .path()
                .strip_prefix(&path)?
                .to_string_lossy()
                .to_string();
            if !referenced_files.contains(&rel_path) {
                orphaned.push(entry.path());
            }
        }
    }

    // Check casks directory
    let casks_dir = path.join("casks");
    if casks_dir.exists() {
        for entry in std::fs::read_dir(&casks_dir)? {
            let entry = entry?;
            let rel_path = entry
                .path()
                .strip_prefix(&path)?
                .to_string_lossy()
                .to_string();
            if !referenced_files.contains(&rel_path) {
                orphaned.push(entry.path());
            }
        }
    }

    // Check artifacts directory
    let artifacts_dir = path.join("artifacts");
    if artifacts_dir.exists() {
        for entry in std::fs::read_dir(&artifacts_dir)? {
            let entry = entry?;
            let rel_path = entry
                .path()
                .strip_prefix(&path)?
                .to_string_lossy()
                .to_string();
            if !referenced_files.contains(&rel_path) {
                orphaned.push(entry.path());
            }
        }
    }

    if orphaned.is_empty() {
        println!("  {} No orphaned files found", style("").green());
        println!();
        return Ok(());
    }

    // Calculate total size of orphaned files
    let mut total_size = 0u64;
    for p in &orphaned {
        if let Ok(m) = p.metadata() {
            total_size += m.len();
        }
    }

    println!(
        "  Found {} orphaned file{} ({})",
        orphaned.len(),
        if orphaned.len() == 1 { "" } else { "s" },
        humansize::format_size(total_size, BINARY)
    );
    println!();

    if dry_run {
        println!(
            "{} Would remove {} orphaned files:",
            style("DRY RUN:").yellow(),
            orphaned.len()
        );
        for p in &orphaned {
            println!("    {}", p.display());
        }
    } else {
        let mut removed = 0;
        let mut failed = 0;

        for p in orphaned {
            match std::fs::remove_file(&p) {
                Ok(_) => {
                    if verbose_output() {
                        println!("  Removed {}", p.display());
                    }
                    removed += 1;
                }
                Err(e) => {
                    println!(
                        "  {} Failed to remove {}: {}",
                        style("").red(),
                        p.display(),
                        e
                    );
                    failed += 1;
                }
            }
        }

        println!();
        if failed == 0 {
            println!("{} Removed {} orphaned files", style("").green(), removed);
        } else {
            println!(
                "{} Removed {}, {} failed",
                style("!").yellow(),
                removed,
                failed
            );
        }
    }

    Ok(())
}

async fn run_verify(path: PathBuf, packages: Vec<String>, verbose: bool) -> Result<()> {
    let manifest_path = path.join("manifest.json");
    if !manifest_path.exists() {
        bail!("Invalid mirror: missing manifest.json");
    }

    let manifest = MirrorManifest::load(&manifest_path)?;

    println!(
        "\n{} mirror at {}\n",
        style("Verifying").cyan().bold(),
        path.display()
    );

    let packages_to_check: Vec<&String> = if packages.is_empty() {
        manifest.formulas.packages.keys().collect()
    } else {
        packages.iter().collect()
    };

    let mut errors = 0;
    let mut verified = 0;

    // Check manifest
    print!("  Checking manifest.json... ");
    println!("{}", style("").green());
    verified += 1;

    // Check formula bottles
    for name in packages_to_check {
        if let Some(info) = manifest.formulas.packages.get(name) {
            for (platform, bottle) in &info.bottles {
                let bottle_path = path.join(&bottle.path);
                if verbose {
                    print!("  Checking {}/{} bottle... ", name, platform);
                }

                if bottle_path.exists() {
                    // Verify checksum
                    if let Err(e) = verify_file_checksum(&bottle_path, &bottle.sha256) {
                        if verbose {
                            println!("{} {}", style("").red(), e);
                        }
                        errors += 1;
                    } else {
                        if verbose {
                            println!("{}", style("").green());
                        }
                        verified += 1;
                    }
                } else {
                    if verbose {
                        println!("{}", style("✗ missing").red());
                    }
                    errors += 1;
                }
            }
        }
    }

    println!();
    if errors == 0 {
        println!("{} {} files verified", style("").green(), verified);
    } else {
        println!(
            "{} {} files verified, {} errors",
            style("!").yellow(),
            verified,
            errors
        );
    }
    println!();

    if errors > 0 {
        bail!("Mirror verification failed with {} errors", errors);
    }

    Ok(())
}