Skip to content

Commit

Permalink
Merge pull request #3 from Velka-DEV/features/cli-presets
Browse files Browse the repository at this point in the history
feat: added preset flag to use a predefined set of files
  • Loading branch information
Velka-DEV authored Oct 3, 2024
2 parents 01d3e74 + fa155a2 commit 9e8c932
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 8 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
45 changes: 37 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,56 @@ use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

use clap::Parser;
use serde::Deserialize;
use serde_json::from_str;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
/// Path to the directory
#[arg(long, default_value = ".", short = 'p')]
path: String,

/// File extensions to include (comma-separated)
#[arg(long, value_delimiter = ',', short = 'e')]
extensions: Vec<String>,

/// Exclude files/folders matching the pattern (comma-separated)
#[arg(long, value_delimiter = ',', short = 'x')]
exclude: Vec<String>,

#[arg(long, short = 'r')]
preset: Option<String>,
}

#[derive(Deserialize)]
struct Preset {
extensions: Vec<String>,
exclude: Vec<String>,
}

fn main() {
let args = Args::parse();
let path = &args.path;

let extensions: Vec<&str> = args.extensions.iter().map(|s| s.as_str()).collect();
let exclude_patterns: Vec<&str> = args.exclude.iter().map(|s| s.as_str()).collect();
let presets: HashMap<String, Preset> = load_presets();

let mut extensions = args.extensions.clone();
let mut exclude_patterns = args.exclude.clone();

if let Some(preset_name) = &args.preset {
if let Some(preset) = presets.get(preset_name) {
if extensions.is_empty() {
extensions = preset.extensions.clone();
}
if exclude_patterns.is_empty() {
exclude_patterns = preset.exclude.clone();
}
} else {
eprintln!("Preset '{}' not found", preset_name);
}
}

let extensions: Vec<&str> = extensions.iter().map(|s| s.as_str()).collect();
let exclude_patterns: Vec<&str> = exclude_patterns.iter().map(|s| s.as_str()).collect();

let mut extension_map = HashMap::new();
extension_map.insert("go", "go");
Expand Down Expand Up @@ -63,6 +88,11 @@ fn main() {
print_file_contents(&path, &extensions, &extension_map, &exclude_patterns);
}

fn load_presets() -> HashMap<String, Preset> {
let preset_data = include_str!("presets.json");
from_str(preset_data).expect("Failed to parse presets.json")
}

fn print_directory_tree(dir: &str, level: usize, exclude_patterns: &[&str]) {
let entries = fs::read_dir(dir).unwrap();

Expand All @@ -87,7 +117,6 @@ fn print_directory_tree(dir: &str, level: usize, exclude_patterns: &[&str]) {
println!("{}", path.file_name().unwrap().to_str().unwrap());
}
}

}

fn print_file_contents(
Expand Down Expand Up @@ -141,4 +170,4 @@ fn is_excluded(path: &Path, exclude_patterns: &[&str]) -> bool {
exclude_patterns
.iter()
.any(|pattern| path.to_str().unwrap().contains(pattern))
}
}
142 changes: 142 additions & 0 deletions src/presets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"rust": {
"extensions": ["rs", "toml", "md"],
"exclude": [".git", "target", "node_modules", "dist", "build", "tmp", "temp"]
},
"python": {
"extensions": ["py", "pyw", "ipynb", "txt", "md", "yaml", "yml", "json"],
"exclude": [".git", "__pycache__", "venv", "env", "node_modules", "dist", "build", "tmp", "temp"]
},
"javascript": {
"extensions": ["js", "jsx", "json", "md", "html", "css"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp", "bower_components"]
},
"typescript": {
"extensions": ["ts", "tsx", "json", "md", "html", "css"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp", "bower_components"]
},
"go": {
"extensions": ["go", "mod", "sum", "md"],
"exclude": [".git", "vendor", "node_modules", "dist", "build", "tmp", "temp"]
},
"java": {
"extensions": ["java", "xml", "properties", "gradle", "md"],
"exclude": [".git", "target", "node_modules", "dist", "build", "tmp", "temp"]
},
"csharp": {
"extensions": ["cs", "csproj", "sln", "config", "md"],
"exclude": [".git", "bin", "obj", "node_modules", "dist", "build", "tmp", "temp"]
},
"cpp": {
"extensions": ["cpp", "hpp", "h", "c", "md"],
"exclude": [".git", "build", "node_modules", "dist", "tmp", "temp"]
},
"php": {
"extensions": ["php", "html", "css", "js", "md"],
"exclude": [".git", "vendor", "node_modules", "dist", "build", "tmp", "temp"]
},
"ruby": {
"extensions": ["rb", "erb", "rake", "md"],
"exclude": [".git", "vendor", "node_modules", "dist", "build", "tmp", "temp"]
},
"kotlin": {
"extensions": ["kt", "kts", "java", "xml", "md"],
"exclude": [".git", "build", "node_modules", "dist", "tmp", "temp"]
},
"swift": {
"extensions": ["swift", "h", "m", "mm", "md"],
"exclude": [".git", "build", "node_modules", "dist", "tmp", "temp"]
},
"haskell": {
"extensions": ["hs", "lhs", "md"],
"exclude": [".git", "dist", "build", "node_modules", "tmp", "temp"]
},
"scala": {
"extensions": ["scala", "sc", "java", "md"],
"exclude": [".git", "target", "node_modules", "dist", "build", "tmp", "temp"]
},
"elixir": {
"extensions": ["ex", "exs", "md"],
"exclude": [".git", "_build", "deps", "node_modules", "dist", "build", "tmp", "temp"]
},
"erlang": {
"extensions": ["erl", "hrl", "md"],
"exclude": [".git", "ebin", "node_modules", "dist", "build", "tmp", "temp"]
},
"lua": {
"extensions": ["lua", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"react": {
"extensions": ["js", "jsx", "ts", "tsx", "json", "md", "html", "css"],
"exclude": [".git", "node_modules", "build", "dist", "tmp", "temp"]
},
"vue": {
"extensions": ["vue", "js", "ts", "json", "md", "html", "css"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"angular": {
"extensions": ["ts", "html", "css", "json", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"django": {
"extensions": ["py", "html", "css", "js", "json", "md"],
"exclude": [".git", "__pycache__", "venv", "env", "node_modules", "dist", "build", "tmp", "temp"]
},
"flask": {
"extensions": ["py", "html", "css", "js", "json", "md"],
"exclude": [".git", "__pycache__", "venv", "env", "node_modules", "dist", "build", "tmp", "temp"]
},
"rails": {
"extensions": ["rb", "erb", "html", "css", "js", "md"],
"exclude": [".git", "vendor", "node_modules", "dist", "build", "tmp", "temp"]
},
"laravel": {
"extensions": ["php", "blade.php", "html", "css", "js", "md"],
"exclude": [".git", "vendor", "node_modules", "dist", "build", "tmp", "temp"]
},
"spring": {
"extensions": ["java", "xml", "properties", "md"],
"exclude": [".git", "target", "node_modules", "dist", "build", "tmp", "temp"]
},
"express": {
"extensions": ["js", "ts", "json", "md", "html", "css"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"nextjs": {
"extensions": ["js", "jsx", "ts", "tsx", "json", "md", "html", "css"],
"exclude": [".git", ".next", "node_modules", "dist", "build", "tmp", "temp"]
},
"nuxt": {
"extensions": ["js", "ts", "vue", "json", "md", "html", "css"],
"exclude": [".git", ".nuxt", "node_modules", "dist", "build", "tmp", "temp"]
},
"svelte": {
"extensions": ["svelte", "js", "ts", "json", "md", "html", "css"],
"exclude": [".git", "public/build", "node_modules", "dist", "build", "tmp", "temp"]
},
"meteor": {
"extensions": ["js", "jsx", "ts", "tsx", "json", "md", "html", "css"],
"exclude": [".git", ".meteor/local", "node_modules", "dist", "build", "tmp", "temp"]
},
"aspnet": {
"extensions": ["cs", "cshtml", "config", "json", "md", "html", "css"],
"exclude": [".git", "bin", "obj", "node_modules", "dist", "build", "tmp", "temp"]
},
"html": {
"extensions": ["html", "htm", "css", "js", "json", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"css": {
"extensions": ["css", "scss", "sass", "less", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"docker": {
"extensions": ["dockerfile", "Dockerfile", "yaml", "yml", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
},
"kubernetes": {
"extensions": ["yaml", "yml", "json", "md"],
"exclude": [".git", "node_modules", "dist", "build", "tmp", "temp"]
}
}

0 comments on commit 9e8c932

Please sign in to comment.