Skip to content

Commit f1be6b8

Browse files
authored
Merge pull request ogham#146 from spk/fix-exit-status
Exit with a non-zero status on error
2 parents 98f0326 + 8b61a3a commit f1be6b8

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/bin/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ fn main() {
1010
let mut stdout = stdout();
1111

1212
match Exa::new(&args, &mut stdout) {
13-
Ok(mut exa) => if let Err(e) = exa.run() {
14-
match e.kind() {
15-
ErrorKind::BrokenPipe => exit(0),
16-
_ => {
17-
writeln!(stderr(), "{}", e).unwrap();
18-
exit(1);
19-
},
13+
Ok(mut exa) => {
14+
match exa.run() {
15+
Ok(exit_status) => exit(exit_status),
16+
Err(e) => {
17+
match e.kind() {
18+
ErrorKind::BrokenPipe => exit(0),
19+
_ => {
20+
writeln!(stderr(), "{}", e).unwrap();
21+
exit(1);
22+
},
23+
};
24+
}
2025
};
2126
},
2227
Err(e) => {

src/exa.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
6060
})
6161
}
6262

63-
pub fn run(&mut self) -> IOResult<()> {
63+
pub fn run(&mut self) -> IOResult<i32> {
6464
let mut files = Vec::new();
6565
let mut dirs = Vec::new();
66+
let mut exit_status = 0;
6667

6768
// List the current directory by default, like ls.
6869
if self.args.is_empty() {
@@ -72,6 +73,7 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
7273
for file_name in self.args.iter() {
7374
match File::from_path(Path::new(&file_name), None) {
7475
Err(e) => {
76+
exit_status = 2;
7577
writeln!(stderr(), "{}: {}", file_name, e)?;
7678
},
7779
Ok(f) => {
@@ -98,10 +100,10 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
98100
self.options.filter.filter_argument_files(&mut files);
99101
self.print_files(None, files)?;
100102

101-
self.print_dirs(dirs, no_files, is_only_dir)
103+
self.print_dirs(dirs, no_files, is_only_dir, exit_status)
102104
}
103105

104-
fn print_dirs(&mut self, dir_files: Vec<Dir>, mut first: bool, is_only_dir: bool) -> IOResult<()> {
106+
fn print_dirs(&mut self, dir_files: Vec<Dir>, mut first: bool, is_only_dir: bool, exit_status: i32) -> IOResult<i32> {
105107
for dir in dir_files {
106108

107109
// Put a gap between directories, or between the list of files and
@@ -141,15 +143,18 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
141143
}
142144

143145
self.print_files(Some(&dir), children)?;
144-
self.print_dirs(child_dirs, false, false)?;
146+
match self.print_dirs(child_dirs, false, false, exit_status) {
147+
Ok(_) => (),
148+
Err(e) => return Err(e),
149+
}
145150
continue;
146151
}
147152
}
148153

149154
self.print_files(Some(&dir), children)?;
150155
}
151156

152-
Ok(())
157+
Ok(exit_status)
153158
}
154159

155160
/// Prints the list of files using whichever view is selected.

0 commit comments

Comments
 (0)