Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options:
--base-out-path <BASE_OUT_PATH> (Optional) Base file output path [default: ./base.yaml]
--sort-config-path <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_PATTERN> Exclude patterns to skip files (e.g., "*.terraform.yaml")
--config <CONFIG_FILE> (Optional) Configuration file
-h, --help Print help
-V, --version Print version
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

#[derive(Deserialize)]
Expand All @@ -79,6 +83,7 @@ struct Config {
base_out_path: Option<String>,
sort_config_path: Option<String>,
sort_only: Option<bool>,
exclude_patterns: Option<Vec<String>>,
}

fn sort_only_workflow(args: &Args) -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -113,6 +118,41 @@ fn sort_only_workflow(args: &Args) -> Result<(), Box<dyn Error>> {
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.");
Expand Down Expand Up @@ -256,6 +296,12 @@ fn main() -> Result<(), Box<dyn Error>> {
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
Expand Down