-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.rs
More file actions
211 lines (185 loc) · 5.02 KB
/
Copy pathcli.rs
File metadata and controls
211 lines (185 loc) · 5.02 KB
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
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use tracing::{error, info, Level};
use crate::linters::{
run_clippy_linter, run_cspell_linter, run_markdown_linter, run_rustfmt_linter,
run_shellcheck_linter, run_toml_linter, run_yaml_linter,
};
/// Initialize tracing with default configuration
pub fn init_tracing() {
tracing_subscriber::fmt()
.with_target(true)
.with_thread_ids(false)
.with_thread_names(false)
.with_level(true)
.with_max_level(Level::INFO)
.init();
}
/// Run the complete linter CLI application
///
/// This function initializes tracing, parses CLI arguments, and executes the appropriate linting commands.
///
/// # Errors
///
/// Returns an error if any linter fails or if CLI parsing fails.
pub fn run_cli() -> Result<()> {
init_tracing();
let cli = Cli::parse();
execute_command(cli.command.as_ref())?;
Ok(())
}
#[derive(Parser)]
#[command(name = "linter")]
#[command(about = "Unified linting tool")]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
pub enum Commands {
/// Run markdown linter
#[command(alias = "md")]
Markdown,
/// Run YAML linter
Yaml,
/// Run TOML linter using Taplo
Toml,
/// Run `CSpell` spell checker
#[command(alias = "spell")]
Cspell,
/// Run Rust clippy linter
Clippy,
/// Run Rust formatter check
#[command(alias = "fmt")]
Rustfmt,
/// Run `ShellCheck` linter
#[command(alias = "shell")]
Shellcheck,
/// Run all linters
All,
}
/// Run all linters and collect results
///
/// # Errors
///
/// Returns an error if any linter fails.
pub fn run_all_linters() -> Result<()> {
info!("Running All Linters");
let mut failed = false;
// Run markdown linter
match run_markdown_linter() {
Ok(()) => {}
Err(e) => {
error!("Markdown linting failed: {e}");
failed = true;
}
}
// Run YAML linter
match run_yaml_linter() {
Ok(()) => {}
Err(e) => {
error!("YAML linting failed: {e}");
failed = true;
}
}
// Run TOML linter
match run_toml_linter() {
Ok(()) => {}
Err(e) => {
error!("TOML linting failed: {e}");
failed = true;
}
}
// Run CSpell spell checker
match run_cspell_linter() {
Ok(()) => {}
Err(e) => {
error!("Spell checking failed: {e}");
failed = true;
}
}
// Run Rust clippy linter
match run_clippy_linter() {
Ok(()) => {}
Err(e) => {
error!("Rust clippy linting failed: {e}");
failed = true;
}
}
// Run Rust formatter check
match run_rustfmt_linter() {
Ok(()) => {}
Err(e) => {
error!("Rust formatting failed: {e}");
failed = true;
}
}
// Run ShellCheck linter
match run_shellcheck_linter() {
Ok(()) => {}
Err(e) => {
error!("Shell script linting failed: {e}");
failed = true;
}
}
if failed {
error!("Some linters failed");
return Err(anyhow::anyhow!("Some linters failed"));
}
info!("All linters passed");
Ok(())
}
/// Execute the linting command
///
/// # Errors
///
/// Returns an error if any linter fails.
pub fn execute_command(command: Option<&Commands>) -> Result<()> {
match command {
Some(Commands::Markdown) => {
run_markdown_linter()?;
}
Some(Commands::Yaml) => {
run_yaml_linter()?;
}
Some(Commands::Toml) => {
run_toml_linter()?;
}
Some(Commands::Cspell) => {
run_cspell_linter()?;
}
Some(Commands::Clippy) => {
run_clippy_linter()?;
}
Some(Commands::Rustfmt) => {
run_rustfmt_linter()?;
}
Some(Commands::Shellcheck) => {
run_shellcheck_linter()?;
}
Some(Commands::All) => {
run_all_linters()?;
}
None => {
// Show help when no command is provided
let mut cmd = Cli::command();
cmd.print_help()?;
print_usage_examples();
}
}
Ok(())
}
/// Print usage examples
pub fn print_usage_examples() {
println!("\n");
println!("Examples:");
println!(" cargo run --bin linter markdown # Run markdown linter");
println!(" cargo run --bin linter yaml # Run YAML linter");
println!(" cargo run --bin linter toml # Run TOML linter");
println!(" cargo run --bin linter cspell # Run CSpell spell checker");
println!(" cargo run --bin linter clippy # Run Rust clippy linter");
println!(" cargo run --bin linter rustfmt # Run Rust formatter check");
println!(" cargo run --bin linter shellcheck # Run ShellCheck linter");
println!(" cargo run --bin linter all # Run all linters");
}