ferrflow 5.25.1

Universal semantic versioning for monorepos and classic repos
Documentation
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
use std::path::PathBuf;

use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;

use crate::config::ConfigFileFormat;
use crate::logging::LogFormat;
use crate::status::OutputFormat;
use crate::timing::Timing;

#[derive(Parser)]
#[command(name = "ferrflow")]
#[command(about = "Universal semantic versioning for monorepos and classic repos")]
#[command(version)]
pub struct Cli {
    /// Dry run — show what would happen without making changes
    #[arg(long, global = true)]
    pub dry_run: bool,

    /// Verbose output
    #[arg(short, long, global = true)]
    pub verbose: bool,

    /// Path to config file (overrides auto-detection, env: FERRFLOW_CONFIG)
    #[arg(long, global = true, env = "FERRFLOW_CONFIG")]
    pub config: Option<PathBuf>,

    /// Print a per-stage timing breakdown to stderr after the command finishes
    #[arg(long, global = true)]
    pub timing: bool,

    /// Max threads for CPU-parallel work (per-package planning). Default: all cores. `1` forces single-threaded.
    #[arg(long, global = true, env = "FERRFLOW_JOBS", value_name = "N")]
    pub jobs: Option<usize>,

    /// Log output format. `human` (default) keeps the colored terminal output; `json` emits one structured JSON event per line for CI ingestion.
    #[arg(long, value_enum, default_value_t = LogFormat::default(), global = true)]
    pub log_format: LogFormat,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Show what versions would be bumped (dry run)
    Check {
        /// Output as JSON
        #[arg(long)]
        json: bool,
        /// Pre-release channel override (e.g. beta, rc, dev)
        #[arg(long)]
        channel: Option<String>,
        /// Post a preview comment on the current PR/MR
        #[arg(long)]
        comment: bool,
    },
    /// Bump versions, update changelogs, create tags and push
    Release {
        /// Output a single JSON object describing the release
        #[arg(long)]
        json: bool,
        /// Allow floating tags to move backward to a lower version
        #[arg(long)]
        force: bool,
        /// Force a specific version, skipping commit analysis.
        /// Format: VERSION (single repo) or NAME@VERSION (monorepo)
        #[arg(long, value_name = "VERSION")]
        force_version: Option<String>,
        /// Pre-release channel override (e.g. beta, rc, dev)
        #[arg(long)]
        channel: Option<String>,
        /// Create releases as drafts (GitHub only). A subsequent `ferrflow release`
        /// without --draft will detect and publish existing drafts automatically.
        /// On GitLab this is a no-op — the release is published immediately
        /// (the GitLab releases API has no draft state); a warning is printed.
        #[arg(long)]
        draft: bool,
        /// Break an existing `.git/ferrflow.lock` before acquiring it.
        /// Use only when you're sure no other `ferrflow release` is running —
        /// for example, after a crash that left the lockfile behind.
        #[arg(long)]
        force_unlock: bool,
    },
    /// Run the configured publishers for the currently-released version
    /// of each package, without bumping or tagging. Use after `release`
    /// has cut the version — typically in a separate CI job that has the
    /// build toolchain and registry auth the publishers need (docker
    /// buildx, helm, npm, …).
    Publish {
        /// Packages to publish. Omit to auto-detect from the triggering tag
        /// (GITHUB_REF / CI_COMMIT_TAG), falling back to every package.
        packages: Vec<String>,
        /// Publish every package, ignoring any triggering-tag scope.
        #[arg(short = 'a', long)]
        all: bool,
    },
    /// Generate/update CHANGELOG.md only
    Changelog,
    /// Scaffold a ferrflow configuration file
    Init {
        /// Config file format (json, json5, toml)
        #[arg(long)]
        format: Option<ConfigFileFormat>,
        /// Also scaffold a .ferrflow.manifest.json snapshot and enable
        /// manifest mode in the generated config
        #[arg(long)]
        manifest: bool,
    },
    /// Print each package name, current version, and last release tag
    Status {
        /// Output format
        #[arg(long, value_enum, default_value = "text")]
        output: OutputFormat,
    },
    /// Print the current version of a package
    Version {
        /// Package name (required in monorepos, optional in single repos)
        package: Option<String>,
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
    /// Print the last release tag of a package
    Tag {
        /// Package name (required in monorepos, optional in single repos)
        package: Option<String>,
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
    /// Validate config and versioned files
    Validate {
        /// Output as JSON
        #[arg(long)]
        json: bool,
        /// Remote repository (e.g. owner/repo for GitHub, or gitlab:group/project)
        #[arg(long)]
        repo: Option<String>,
        /// Git ref for remote validation (branch, tag, commit)
        #[arg(long, name = "ref")]
        git_ref: Option<String>,
    },
    /// Generate shell completion scripts
    Completions {
        /// Shell to generate completions for
        shell: Shell,
    },
    /// Manage the cross-run cache under .git/ferrflow-cache/
    Cache {
        #[command(subcommand)]
        command: CacheCommand,
    },
    /// Regenerate the version manifest from current filesystem state.
    /// Requires workspace.manifest_file to be configured. Use this to
    /// repair a manifest that diverged from the package files (e.g. after
    /// a crashed release).
    SyncManifest,
}

#[derive(Subcommand)]
pub enum CacheCommand {
    /// Delete the cross-run cache directory
    Clear,
}

impl Commands {
    pub fn name(&self) -> &'static str {
        match self {
            Commands::Check { .. } => "check",
            Commands::Release { .. } => "release",
            Commands::Publish { .. } => "publish",
            Commands::Changelog => "changelog",
            Commands::Init { .. } => "init",
            Commands::Status { .. } => "status",
            Commands::Version { .. } => "version",
            Commands::Tag { .. } => "tag",
            Commands::Validate { .. } => "validate",
            Commands::Completions { .. } => "completions",
            Commands::Cache { .. } => "cache",
            Commands::SyncManifest => "sync-manifest",
        }
    }
}

impl Cli {
    pub fn run(self) -> Result<()> {
        let mut timing = Timing::new(self.timing);
        let result = self.dispatch(&mut timing);
        timing.report();
        result
    }

    fn dispatch(self, timing: &mut Timing) -> Result<()> {
        match self.command {
            Commands::Check {
                json,
                channel,
                comment,
            } => crate::monorepo::check(
                self.config.as_deref(),
                self.verbose,
                json,
                channel.as_deref(),
                comment,
                timing,
            ),
            Commands::Release {
                json,
                force,
                force_version,
                channel,
                draft,
                force_unlock,
            } => crate::monorepo::release(
                self.config.as_deref(),
                self.dry_run,
                self.verbose,
                json,
                force,
                force_version.as_deref(),
                channel.as_deref(),
                draft,
                force_unlock,
                timing,
            ),
            Commands::Publish { packages, all } => crate::publish::run(
                self.config.as_deref(),
                &packages,
                all,
                self.dry_run,
                self.verbose,
            ),
            Commands::Changelog => {
                crate::changelog::generate_only(self.config.as_deref(), self.dry_run)
            }
            Commands::Init { format, manifest } => crate::config::init(format, manifest),
            Commands::Status { output } => {
                crate::status::run(self.config.as_deref(), &output, timing)
            }
            Commands::Version { package, json } => {
                crate::query::version(self.config.as_deref(), package.as_deref(), json)
            }
            Commands::Tag { package, json } => {
                crate::query::tag(self.config.as_deref(), package.as_deref(), json, timing)
            }
            Commands::Validate {
                json,
                repo,
                git_ref,
            } => crate::validate::run(
                self.config.as_deref(),
                json,
                repo.as_deref(),
                git_ref.as_deref(),
            ),
            Commands::Completions { shell } => {
                clap_complete::generate(
                    shell,
                    &mut Cli::command(),
                    "ferrflow",
                    &mut std::io::stdout(),
                );
                Ok(())
            }
            Commands::Cache { command } => match command {
                CacheCommand::Clear => crate::cache::clear_cwd(),
            },
            Commands::SyncManifest => crate::manifest::sync_cwd(self.config.as_deref()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    fn parse(args: &[&str]) -> Cli {
        Cli::try_parse_from(args).unwrap()
    }

    #[test]
    fn parse_check() {
        let cli = parse(&["ferrflow", "check"]);
        assert!(matches!(
            cli.command,
            Commands::Check {
                json: false,
                channel: None,
                comment: false,
            }
        ));
    }

    #[test]
    fn parse_check_json() {
        let cli = parse(&["ferrflow", "check", "--json"]);
        assert!(matches!(cli.command, Commands::Check { json: true, .. }));
    }

    #[test]
    fn parse_check_channel() {
        let cli = parse(&["ferrflow", "check", "--channel", "beta"]);
        match cli.command {
            Commands::Check { channel, .. } => assert_eq!(channel.as_deref(), Some("beta")),
            _ => panic!("expected Check"),
        }
    }

    #[test]
    fn parse_release() {
        let cli = parse(&["ferrflow", "release"]);
        assert!(matches!(
            cli.command,
            Commands::Release {
                json: false,
                force: false,
                force_version: None,
                channel: None,
                draft: false,
                force_unlock: false,
            }
        ));
    }

    #[test]
    fn parse_release_json() {
        let cli = parse(&["ferrflow", "release", "--json"]);
        match cli.command {
            Commands::Release { json, .. } => assert!(json),
            _ => panic!("expected Release"),
        }
    }

    #[test]
    fn parse_release_force_unlock() {
        let cli = parse(&["ferrflow", "release", "--force-unlock"]);
        match cli.command {
            Commands::Release { force_unlock, .. } => assert!(force_unlock),
            _ => panic!("expected Release"),
        }
    }

    #[test]
    fn parse_release_force_draft_channel() {
        let cli = parse(&[
            "ferrflow",
            "release",
            "--force",
            "--draft",
            "--channel",
            "rc",
        ]);
        match cli.command {
            Commands::Release {
                force,
                channel,
                draft,
                ..
            } => {
                assert!(force);
                assert!(draft);
                assert_eq!(channel.as_deref(), Some("rc"));
            }
            _ => panic!("expected Release"),
        }
    }

    #[test]
    fn parse_release_force_version() {
        let cli = parse(&["ferrflow", "release", "--force-version", "api@2.0.0"]);
        match cli.command {
            Commands::Release { force_version, .. } => {
                assert_eq!(force_version.as_deref(), Some("api@2.0.0"));
            }
            _ => panic!("expected Release"),
        }
    }

    #[test]
    fn parse_init_no_format() {
        let cli = parse(&["ferrflow", "init"]);
        assert!(matches!(
            cli.command,
            Commands::Init {
                format: None,
                manifest: false
            }
        ));
    }

    #[test]
    fn parse_init_with_format() {
        let cli = parse(&["ferrflow", "init", "--format", "toml"]);
        match cli.command {
            Commands::Init { format, .. } => assert!(format.is_some()),
            _ => panic!("expected Init"),
        }
    }

    #[test]
    fn parse_init_with_manifest() {
        let cli = parse(&["ferrflow", "init", "--manifest"]);
        match cli.command {
            Commands::Init { manifest, .. } => assert!(manifest),
            _ => panic!("expected Init"),
        }
    }

    #[test]
    fn parse_sync_manifest() {
        let cli = parse(&["ferrflow", "sync-manifest"]);
        assert!(matches!(cli.command, Commands::SyncManifest));
        assert_eq!(cli.command.name(), "sync-manifest");
    }

    #[test]
    fn parse_status_default() {
        let cli = parse(&["ferrflow", "status"]);
        assert!(matches!(cli.command, Commands::Status { .. }));
    }

    #[test]
    fn parse_publish_no_package() {
        let cli = parse(&["ferrflow", "publish"]);
        assert!(
            matches!(cli.command, Commands::Publish { packages, all } if packages.is_empty() && !all)
        );
    }

    #[test]
    fn parse_publish_with_packages_and_dry_run() {
        let cli = parse(&["ferrflow", "--dry-run", "publish", "api", "web"]);
        assert!(cli.dry_run);
        match cli.command {
            Commands::Publish { packages, all } => {
                assert_eq!(packages, vec!["api", "web"]);
                assert!(!all);
            }
            _ => panic!("expected Publish"),
        }
    }

    #[test]
    fn parse_publish_all_flag() {
        let cli = parse(&["ferrflow", "publish", "--all"]);
        match cli.command {
            Commands::Publish { packages, all } => {
                assert!(packages.is_empty());
                assert!(all);
            }
            _ => panic!("expected Publish"),
        }
    }

    #[test]
    fn parse_status_json() {
        let cli = parse(&["ferrflow", "status", "--output", "json"]);
        match cli.command {
            Commands::Status { output } => assert!(matches!(output, OutputFormat::Json)),
            _ => panic!("expected Status"),
        }
    }

    #[test]
    fn parse_version_no_package() {
        let cli = parse(&["ferrflow", "version"]);
        assert!(matches!(
            cli.command,
            Commands::Version {
                package: None,
                json: false
            }
        ));
    }

    #[test]
    fn parse_version_with_package_json() {
        let cli = parse(&["ferrflow", "version", "my-pkg", "--json"]);
        match cli.command {
            Commands::Version { package, json } => {
                assert_eq!(package.as_deref(), Some("my-pkg"));
                assert!(json);
            }
            _ => panic!("expected Version"),
        }
    }

    #[test]
    fn parse_tag_no_package() {
        let cli = parse(&["ferrflow", "tag"]);
        assert!(matches!(
            cli.command,
            Commands::Tag {
                package: None,
                json: false
            }
        ));
    }

    #[test]
    fn parse_tag_with_package() {
        let cli = parse(&["ferrflow", "tag", "core"]);
        match cli.command {
            Commands::Tag { package, .. } => assert_eq!(package.as_deref(), Some("core")),
            _ => panic!("expected Tag"),
        }
    }

    #[test]
    fn parse_validate() {
        let cli = parse(&["ferrflow", "validate"]);
        assert!(matches!(
            cli.command,
            Commands::Validate {
                json: false,
                repo: None,
                git_ref: None
            }
        ));
    }

    #[test]
    fn parse_validate_remote() {
        let cli = parse(&[
            "ferrflow",
            "validate",
            "--json",
            "--repo",
            "owner/repo",
            "--git-ref",
            "main",
        ]);
        match cli.command {
            Commands::Validate {
                json,
                repo,
                git_ref,
            } => {
                assert!(json);
                assert_eq!(repo.as_deref(), Some("owner/repo"));
                assert_eq!(git_ref.as_deref(), Some("main"));
            }
            _ => panic!("expected Validate"),
        }
    }

    #[test]
    fn parse_completions() {
        let cli = parse(&["ferrflow", "completions", "bash"]);
        assert!(matches!(cli.command, Commands::Completions { .. }));
    }

    #[test]
    fn parse_changelog() {
        let cli = parse(&["ferrflow", "changelog"]);
        assert!(matches!(cli.command, Commands::Changelog));
    }

    #[test]
    fn parse_cache_clear() {
        let cli = parse(&["ferrflow", "cache", "clear"]);
        assert!(matches!(
            cli.command,
            Commands::Cache {
                command: CacheCommand::Clear
            }
        ));
        assert_eq!(cli.command.name(), "cache");
    }

    #[test]
    fn cache_requires_subcommand() {
        assert!(Cli::try_parse_from(["ferrflow", "cache"]).is_err());
    }

    #[test]
    fn global_dry_run() {
        let cli = parse(&["ferrflow", "--dry-run", "check"]);
        assert!(cli.dry_run);
    }

    #[test]
    fn global_verbose() {
        let cli = parse(&["ferrflow", "-v", "check"]);
        assert!(cli.verbose);
    }

    #[test]
    fn global_config_path() {
        let cli = parse(&["ferrflow", "--config", "/tmp/ferrflow.json", "check"]);
        assert_eq!(cli.config, Some(PathBuf::from("/tmp/ferrflow.json")));
    }

    #[test]
    fn global_timing_default_off() {
        let cli = parse(&["ferrflow", "check"]);
        assert!(!cli.timing);
    }

    #[test]
    fn global_timing() {
        let cli = parse(&["ferrflow", "--timing", "check"]);
        assert!(cli.timing);
    }

    #[test]
    fn global_jobs_default_none() {
        let cli = parse(&["ferrflow", "check"]);
        assert_eq!(cli.jobs, None);
    }

    #[test]
    fn global_jobs_flag() {
        let cli = parse(&["ferrflow", "release", "--jobs", "1", "--dry-run"]);
        assert_eq!(cli.jobs, Some(1));
    }

    #[test]
    fn global_timing_after_subcommand() {
        let cli = parse(&["ferrflow", "release", "--timing", "--dry-run"]);
        assert!(cli.timing);
        assert!(cli.dry_run);
    }

    #[test]
    fn global_flags_after_subcommand() {
        let cli = parse(&["ferrflow", "release", "--dry-run", "--verbose"]);
        assert!(cli.dry_run);
        assert!(cli.verbose);
    }

    #[test]
    fn unknown_subcommand_fails() {
        assert!(Cli::try_parse_from(["ferrflow", "unknown"]).is_err());
    }

    #[test]
    fn missing_subcommand_fails() {
        assert!(Cli::try_parse_from(["ferrflow"]).is_err());
    }

    #[test]
    fn command_names() {
        assert_eq!(parse(&["ferrflow", "check"]).command.name(), "check");
        assert_eq!(parse(&["ferrflow", "release"]).command.name(), "release");
        assert_eq!(
            parse(&["ferrflow", "changelog"]).command.name(),
            "changelog"
        );
        assert_eq!(parse(&["ferrflow", "init"]).command.name(), "init");
        assert_eq!(parse(&["ferrflow", "status"]).command.name(), "status");
        assert_eq!(parse(&["ferrflow", "version"]).command.name(), "version");
        assert_eq!(parse(&["ferrflow", "tag"]).command.name(), "tag");
        assert_eq!(parse(&["ferrflow", "validate"]).command.name(), "validate");
    }
}