-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshellcheck.rs
More file actions
171 lines (145 loc) · 4.76 KB
/
shellcheck.rs
File metadata and controls
171 lines (145 loc) · 4.76 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
use anyhow::Result;
use std::process::Command;
use tracing::{error, info, warn};
use crate::utils::is_command_available;
/// Install shellcheck using system package manager
///
/// # Errors
///
/// Returns an error if no supported package manager is found or if installation fails.
fn install_shellcheck() -> Result<()> {
info!("Installing ShellCheck...");
// Try different package managers
if is_command_available("apt-get") {
let output = Command::new("sudo").args(["apt-get", "update"]).output()?;
if !output.status.success() {
warn!("Failed to update package list");
}
let output = Command::new("sudo")
.args(["apt-get", "install", "-y", "shellcheck"])
.output()?;
if output.status.success() {
info!("shellcheck installed successfully");
return Ok(());
}
} else if is_command_available("dnf") {
let output = Command::new("sudo")
.args(["dnf", "install", "-y", "ShellCheck"])
.output()?;
if output.status.success() {
info!("shellcheck installed successfully");
return Ok(());
}
} else if is_command_available("pacman") {
let output = Command::new("sudo")
.args(["pacman", "-S", "--noconfirm", "shellcheck"])
.output()?;
if output.status.success() {
info!("shellcheck installed successfully");
return Ok(());
}
} else if is_command_available("brew") {
let output = Command::new("brew")
.args(["install", "shellcheck"])
.output()?;
if output.status.success() {
info!("shellcheck installed successfully");
return Ok(());
}
}
error!("Could not install shellcheck: unsupported package manager");
info!("Please install shellcheck manually: https://github.com/koalaman/shellcheck#installing");
Err(anyhow::anyhow!("Could not install shellcheck"))
}
/// Find shell scripts in the current directory
///
/// # Errors
///
/// Returns an error if the find command fails.
fn find_shell_scripts() -> Result<Vec<String>> {
let mut files = Vec::new();
// Find .sh files
let sh_output = Command::new("find")
.args([
".",
"-name",
"*.sh",
"-type",
"f",
"-not",
"-path",
"*/.git/*",
"-not",
"-path",
"*/.terraform/*",
])
.output()?;
if sh_output.status.success() {
let stdout = String::from_utf8_lossy(&sh_output.stdout);
files.extend(stdout.lines().filter(|s| !s.is_empty()).map(String::from));
}
// Find .bash files
let bash_output = Command::new("find")
.args([
".",
"-name",
"*.bash",
"-type",
"f",
"-not",
"-path",
"*/.git/*",
"-not",
"-path",
"*/.terraform/*",
])
.output()?;
if bash_output.status.success() {
let stdout = String::from_utf8_lossy(&bash_output.stdout);
files.extend(stdout.lines().filter(|s| !s.is_empty()).map(String::from));
}
Ok(files)
}
/// Run the `ShellCheck` linter
///
/// # Errors
///
/// Returns an error if shellcheck is not available, cannot be installed,
/// or if the linting fails.
pub fn run_shellcheck_linter() -> Result<()> {
info!(target: "shellcheck", "Running ShellCheck on shell scripts...");
// Check if shellcheck is installed
if !is_command_available("shellcheck") {
warn!(target: "shellcheck", "shellcheck not found. Attempting to install...");
install_shellcheck()?;
}
// Find shell scripts
let shell_files = find_shell_scripts()?;
if shell_files.is_empty() {
warn!(target: "shellcheck", "No shell scripts found");
return Ok(());
}
info!(target: "shellcheck", "Found {} shell script(s) to check", shell_files.len());
// Prepare the shellcheck command
let mut cmd = Command::new("shellcheck");
cmd.args(["--source-path=SCRIPTDIR", "--exclude=SC1091"]);
cmd.args(&shell_files);
let output = cmd.output()?;
if output.status.success() {
info!(target: "shellcheck", "shellcheck passed");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
// Print the output from shellcheck
if !stdout.is_empty() {
println!("{stdout}");
}
if !stderr.is_empty() {
eprintln!("{stderr}");
}
println!();
error!(target: "shellcheck", "shellcheck failed");
Err(anyhow::anyhow!("shellcheck failed"))
}
}