Skip to content

Commit b7502ef

Browse files
committed
better error message for invalid files
1 parent 1516d35 commit b7502ef

2 files changed

Lines changed: 49 additions & 17 deletions

File tree

sbuild-linter/src/comments.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ impl Comments {
2727
let file = File::open(file_path)?;
2828
let reader = BufReader::new(file);
2929
let mut current_comments = Vec::new();
30-
let mut is_first_line = true;
30+
let mut shebang_added = false;
3131

3232
for line in reader.lines() {
3333
let line = line?;
3434
let trimmed = line.trim();
3535

36-
if is_first_line {
37-
is_first_line = false;
38-
if trimmed.starts_with("#!/SBUILD") {
36+
if trimmed.starts_with("#!/SBUILD") {
37+
if !shebang_added {
3938
self.header_comments.push(trimmed.to_string());
40-
continue;
41-
} else {
42-
self.header_comments.push("#!/SBUILD".to_string());
39+
shebang_added = true;
4340
}
41+
continue;
4442
}
4543

4644
if trimmed.starts_with('#') {

sbuild-linter/src/main.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::HashSet,
33
env,
4+
fmt::Display,
45
fs::File,
56
io::{BufRead, BufReader, BufWriter, Write},
67
process::{Command, ExitStatus, Stdio},
@@ -70,24 +71,44 @@ fn deserialize_yaml(yaml_str: &str) -> Result<BuildConfig, serde_yml::Error> {
7071
deserializer.deserialize_map(visitor)
7172
}
7273

73-
fn read_yaml(file_path: &str) -> Result<String, Box<dyn std::error::Error>> {
74-
let file = File::open(file_path)?;
74+
enum FileError {
75+
InvalidFile(String),
76+
NotFound(String),
77+
}
78+
79+
impl Display for FileError {
80+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
match self {
82+
FileError::InvalidFile(fp) => writeln!(
83+
f,
84+
"[{}] Invalid file {}. Please provide a valid YAML file.",
85+
&*CROSS_MARK, fp
86+
),
87+
FileError::NotFound(fp) => writeln!(f, "[{}] File {} not found.", &*CROSS_MARK, fp),
88+
}
89+
}
90+
}
91+
92+
fn read_yaml(file_path: &str) -> Result<String, FileError> {
93+
let Ok(file) = File::open(file_path) else {
94+
return Err(FileError::NotFound(file_path.into()));
95+
};
7596
let reader = BufReader::new(file);
7697

7798
let mut yaml_content = String::new();
7899
let mut lines = reader.lines();
79100

80101
if let Some(line) = lines.next() {
81-
let line = line?;
102+
let line = line.map_err(|_| FileError::InvalidFile(file_path.into()))?;
82103
if !line.trim_start().starts_with("#!/SBUILD") {
83104
println!("[{}] File doesn't start with '#!/SBUILD'", &*WARN);
84105
}
85106
} else {
86-
return Err("Invalid file".into());
107+
return Err(FileError::InvalidFile(file_path.into()));
87108
}
88109

89110
for line in lines {
90-
let line = line?;
111+
let line = line.map_err(|_| FileError::InvalidFile(file_path.into()))?;
91112
yaml_content.push_str(&line);
92113
yaml_content.push('\n');
93114
}
@@ -134,8 +155,9 @@ fn is_pkgver_success(config: &BuildConfig, pkgver_path: &str) -> bool {
134155
let _ = writer.write_all(&pkgver.as_bytes());
135156

136157
println!(
137-
"[{}] pkgver written to {}",
158+
"[{}] Version ({}) from pkgver written to {}",
138159
&*CHECK_MARK,
160+
pkgver,
139161
pkgver_path.bright_cyan()
140162
);
141163
}
@@ -165,17 +187,18 @@ fn is_pkgver_success(config: &BuildConfig, pkgver_path: &str) -> bool {
165187
&*CROSS_MARK
166188
);
167189
output_str.lines().for_each(|line| {
168-
println!("-> {}", line);
190+
println!("-> {}", line.trim());
169191
});
170192
success = false;
171193
} else {
172194
let file = File::create(pkgver_path).unwrap();
173195
let mut writer = BufWriter::new(file);
174-
let _ = writer.write_all(&out);
196+
let _ = writer.write_all(&output_str.as_bytes());
175197

176198
println!(
177-
"[{}] x_exec.pkgver written to {}",
199+
"[{}] Fetched version ({}) using x_exec.pkgver written to {}",
178200
&*CHECK_MARK,
201+
&output_str,
179202
pkgver_path.bright_cyan()
180203
);
181204
}
@@ -188,6 +211,9 @@ fn is_pkgver_success(config: &BuildConfig, pkgver_path: &str) -> bool {
188211
"x_exec.pkgver".bold()
189212
);
190213
success = false;
214+
if !cmd.stderr.is_empty() {
215+
eprintln!("{}", String::from_utf8_lossy(&cmd.stderr));
216+
}
191217
}
192218
} else {
193219
eprintln!(
@@ -298,9 +324,17 @@ fn main() {
298324
}
299325
}
300326

327+
println!("sbuild-linter v{}", env!("CARGO_PKG_VERSION"));
328+
301329
let now = Instant::now();
302330
for file_path in &files {
303-
let yaml_str = read_yaml(file_path).expect("Invalid file.");
331+
let yaml_str = match read_yaml(file_path) {
332+
Ok(y) => y,
333+
Err(err) => {
334+
eprintln!("{}", err);
335+
continue;
336+
}
337+
};
304338

305339
println!("\n[{}] Linting {}", "-".bright_blue().bold(), file_path);
306340
match deserialize_yaml(&yaml_str) {

0 commit comments

Comments
 (0)