diff --git a/Cargo.lock b/Cargo.lock index ab30934..f39028c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -486,7 +486,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "yabe-gitops" -version = "0.1.7" +version = "0.1.8" dependencies = [ "clap", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 2f1e985..eea53ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "yabe-gitops" description = "GitOps organizer" repository = "https://github.com/dvrkn/yabe" readme = "README.md" -version = "0.1.7" +version = "0.1.8" edition = "2021" keywords = ["gitops", "kubernetes", "argocd", "yaml", "helm"] license = "MIT" diff --git a/README.md b/README.md index fa0c3d9..a788177 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Options: --base-out-path (Optional) Base file output path [default: ./base.yaml] --sort-config-path (Optional) Sort configuration file path [default: ./sort-config.yaml], if not provided, will not sort --sort-only Sort only mode - only sort files without diffing + --exclude Exclude patterns to skip files (e.g., "*.terraform.yaml") --config (Optional) Configuration file -h, --help Print help -V, --version Print version @@ -94,6 +95,12 @@ Use the --sort-only flag to only sort YAML files without performing any diffing # Sort files to a specific output directory ./yabe --sort-only --sort-config-path sort-config.yaml -o ./sorted-files *.yaml + +# Sort files recursively while excluding certain patterns +./yabe --sort-only --sort-config-path sort-config.yaml -p "**/*.yaml" --exclude "*.terraform.yaml" --exclude "*-template.yaml" + +# Sort files in-place while excluding terraform files +./yabe --sort-only --sort-config-path sort-config.yaml -p "./envs/**/*.yaml" --exclude "*.terraform.yaml" -i ``` ### Using Configuration File @@ -121,6 +128,9 @@ quorum: 60 base_out_path: "./base_output.yaml" sort_config_path: "./sort_config.yaml" sort_only: false # Set to true for sort-only mode +exclude_patterns: # Optional: patterns to exclude from sorting + - "*.terraform.yaml" + - "*-template.yaml" ``` Then run the tool with: diff --git a/src/main.rs b/src/main.rs index acc538e..df32a3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,10 @@ struct Args { /// Sort only mode - only sort files without diffing #[arg(long = "sort-only")] sort_only: bool, + + /// Exclude patterns to skip files (e.g., "*.terraform.yaml") + #[arg(long = "exclude", value_name = "EXCLUDE_PATTERN")] + exclude_patterns: Vec, } #[derive(Deserialize)] @@ -79,6 +83,7 @@ struct Config { base_out_path: Option, sort_config_path: Option, sort_only: Option, + exclude_patterns: Option>, } fn sort_only_workflow(args: &Args) -> Result<(), Box> { @@ -113,6 +118,41 @@ fn sort_only_workflow(args: &Args) -> Result<(), Box> { expanded_input_files.sort(); expanded_input_files.dedup(); + // Filter out excluded files + if !args.exclude_patterns.is_empty() { + let original_count = expanded_input_files.len(); + expanded_input_files.retain(|file_path| { + for exclude_pattern in &args.exclude_patterns { + if let Ok(paths) = glob(exclude_pattern) { + for entry in paths { + if let Ok(excluded_path) = entry { + if let Some(excluded_path_str) = excluded_path.to_str() { + if file_path == excluded_path_str { + info!("Excluding file: {} (matches pattern: {})", file_path, exclude_pattern); + return false; + } + } + } + } + } + // Also check if the filename matches the pattern directly + if let Some(filename) = Path::new(file_path).file_name() { + if let Some(filename_str) = filename.to_str() { + if glob::Pattern::new(exclude_pattern).map_or(false, |p| p.matches(filename_str)) { + info!("Excluding file: {} (filename matches pattern: {})", file_path, exclude_pattern); + return false; + } + } + } + } + true + }); + let excluded_count = original_count - expanded_input_files.len(); + if excluded_count > 0 { + info!("Excluded {} files based on exclude patterns", excluded_count); + } + } + // Validate that we have files to process if expanded_input_files.is_empty() { eprintln!("Error: No input files found for sort-only mode. Please specify either input files or path patterns."); @@ -256,6 +296,12 @@ fn main() -> Result<(), Box> { args.sort_only = sort_only; } } + + if args.exclude_patterns.is_empty() { + if let Some(exclude_patterns) = config.exclude_patterns { + args.exclude_patterns = exclude_patterns; + } + } } // Initialize logger with appropriate level