|
| 1 | +use clap::Parser; |
| 2 | + |
| 3 | +use super::{ |
| 4 | + FlowSpecificConfig, |
| 5 | + OverridesConfig, |
| 6 | +}; |
| 7 | +use crate::cli::common::args::{ |
| 8 | + InputConfig, |
| 9 | + OutputConfig, |
| 10 | + Validation as CommonValidation, |
| 11 | +}; |
| 12 | +use crate::error::ZervError; |
| 13 | + |
| 14 | +/// Generate version with intelligent pre-release management based on Git branch patterns |
| 15 | +#[derive(Parser)] |
| 16 | +#[command( |
| 17 | + about = "Generate version with intelligent pre-release management based on Git branch patterns" |
| 18 | +)] |
| 19 | +#[command( |
| 20 | + long_about = "Generate version strings with automatic pre-release detection based on Git branch patterns. |
| 21 | +This command acts as an intelligent wrapper around 'zerv version' that automatically determines |
| 22 | +pre-release information from the current Git branch using configurable pattern matching. |
| 23 | +
|
| 24 | +INPUT/OUTPUT OPTIONS: |
| 25 | + -s, --source <TYPE> Input source: git, stdin |
| 26 | + -f, --input-format <FMT> Input format: auto, semver, pep440 |
| 27 | + -o, --output-format <FMT> Output format: semver, pep440, zerv |
| 28 | + -t, --output-template <TPL> Custom output template (Handlebars) |
| 29 | + -p, --output-prefix <PFX> Add prefix to version output |
| 30 | +
|
| 31 | +BRANCH PATTERN DETECTION: |
| 32 | + --branch-rules <RON> Custom branch pattern rules in RON format |
| 33 | + Default: develop -> alpha, release/* -> rc |
| 34 | +
|
| 35 | +OUTPUT MODES: |
| 36 | + --with-pre-release Show full version including pre-release (default) |
| 37 | + --base-only Show only base version without pre-release |
| 38 | +
|
| 39 | +POST DISTANCE MODES: |
| 40 | + --post-mode <MODE> Post calculation: tag (default), commit |
| 41 | +
|
| 42 | +OVERRIDES: |
| 43 | + --tag-version <TAG> Override detected tag version |
| 44 | + --distance <NUM> Override distance from tag |
| 45 | + --dirty Override dirty state to true |
| 46 | + --no-dirty Override dirty state to false |
| 47 | + --clean Force clean state (distance=0, dirty=false) |
| 48 | + --current-branch <NAME> Override branch name for pattern matching |
| 49 | + --commit-hash <HASH> Override commit hash |
| 50 | +
|
| 51 | +EXAMPLES: |
| 52 | + # Basic flow version with automatic pre-release detection |
| 53 | + zerv flow |
| 54 | +
|
| 55 | + # Custom branch rules (RON format) |
| 56 | + zerv flow --branch-rules \"[(branch_pattern: \"feature/*\", pre_release: \"beta\", number: 2)]\" |
| 57 | +
|
| 58 | + # Different output formats |
| 59 | + zerv flow --output-format pep440 |
| 60 | + zerv flow --output-format zerv |
| 61 | + zerv flow --output-prefix v |
| 62 | +
|
| 63 | + # Show base version only |
| 64 | + zerv flow --base-only |
| 65 | +
|
| 66 | + # Override branch for testing |
| 67 | + zerv flow --current-branch release/v1.2.0 |
| 68 | +
|
| 69 | + # Force clean release state |
| 70 | + zerv flow --clean |
| 71 | +
|
| 72 | + # Use in different directory |
| 73 | + zerv flow -C /path/to/repo" |
| 74 | +)] |
| 75 | +#[derive(Debug, Default)] |
| 76 | +pub struct FlowArgs { |
| 77 | + #[command(flatten)] |
| 78 | + pub input: InputConfig, |
| 79 | + |
| 80 | + #[command(flatten)] |
| 81 | + pub output: OutputConfig, |
| 82 | + |
| 83 | + #[command(flatten)] |
| 84 | + pub flow_specific: FlowSpecificConfig, |
| 85 | + |
| 86 | + #[command(flatten)] |
| 87 | + pub overrides: OverridesConfig, |
| 88 | +} |
| 89 | + |
| 90 | +impl FlowArgs { |
| 91 | + /// Validate arguments and return early errors |
| 92 | + /// This provides early validation before flow processing |
| 93 | + /// Note: source and format validation is handled by clap's value parser |
| 94 | + pub fn validate(&mut self) -> Result<(), ZervError> { |
| 95 | + // Use shared validation for input/output |
| 96 | + CommonValidation::validate_io(&self.input, &self.output)?; |
| 97 | + |
| 98 | + // Validate flow-specific modules |
| 99 | + use super::Validation; |
| 100 | + Validation::validate_flow_specific(&self.flow_specific)?; |
| 101 | + Validation::validate_overrides(&self.overrides)?; |
| 102 | + Validation::validate_branch_rules(&self.flow_specific.branch_rules)?; |
| 103 | + |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | + |
| 107 | + /// Get the dirty override state (None = use VCS, Some(bool) = override) |
| 108 | + pub fn dirty_override(&self) -> Option<bool> { |
| 109 | + self.overrides.dirty_override() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + use crate::cli::common::args::{ |
| 117 | + InputConfig, |
| 118 | + OutputConfig, |
| 119 | + }; |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_flow_args_default() { |
| 123 | + let args = FlowArgs::default(); |
| 124 | + assert_eq!(args.input.source, "git"); |
| 125 | + assert_eq!(args.output.output_format, "semver"); |
| 126 | + assert!(!args.flow_specific.with_pre_release); |
| 127 | + assert!(!args.flow_specific.base_only); |
| 128 | + assert_eq!(args.flow_specific.post_mode, "tag"); |
| 129 | + assert_eq!(args.dirty_override(), None); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_flow_args_validation_success() { |
| 134 | + let mut args = FlowArgs::default(); |
| 135 | + // Should validate successfully with defaults |
| 136 | + assert!(args.validate().is_ok()); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_flow_args_dirty_override_integration() { |
| 141 | + let args = FlowArgs { |
| 142 | + overrides: OverridesConfig { |
| 143 | + dirty: true, |
| 144 | + no_dirty: false, |
| 145 | + ..Default::default() |
| 146 | + }, |
| 147 | + ..FlowArgs::default() |
| 148 | + }; |
| 149 | + assert_eq!(args.dirty_override(), Some(true)); |
| 150 | + |
| 151 | + let args = FlowArgs { |
| 152 | + overrides: OverridesConfig { |
| 153 | + dirty: false, |
| 154 | + no_dirty: true, |
| 155 | + ..Default::default() |
| 156 | + }, |
| 157 | + ..FlowArgs::default() |
| 158 | + }; |
| 159 | + assert_eq!(args.dirty_override(), Some(false)); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_flow_args_with_input_output() { |
| 164 | + let mut args = FlowArgs { |
| 165 | + input: InputConfig { |
| 166 | + source: "git".to_string(), |
| 167 | + input_format: "auto".to_string(), |
| 168 | + directory: Some("/test/path".to_string()), |
| 169 | + }, |
| 170 | + output: OutputConfig { |
| 171 | + output_format: "zerv".to_string(), |
| 172 | + output_prefix: Some("v".to_string()), |
| 173 | + output_template: None, |
| 174 | + }, |
| 175 | + ..FlowArgs::default() |
| 176 | + }; |
| 177 | + assert_eq!(args.input.source, "git"); |
| 178 | + assert_eq!(args.output.output_format, "zerv"); |
| 179 | + assert_eq!(args.output.output_prefix, Some("v".to_string())); |
| 180 | + assert!(args.validate().is_ok()); |
| 181 | + } |
| 182 | +} |
0 commit comments