Skip to content

Commit

Permalink
Exit with a non-zero status on error
Browse files Browse the repository at this point in the history
With `ls` from Debian coreutils 8.26-2

```
ls /bad/path
echo $? # => 2
```

Reproduced same behaviour with exa

Fix ogham#135
  • Loading branch information
spk committed Feb 26, 2017
1 parent 0ffb331 commit 64cc0d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ fn main() {
let mut stdout = stdout();

match Exa::new(&args, &mut stdout) {
Ok(mut exa) => if let Err(e) = exa.run() {
match e.kind() {
ErrorKind::BrokenPipe => exit(0),
_ => {
writeln!(stderr(), "{}", e).unwrap();
exit(1);
},
Ok(mut exa) => {
match exa.run() {
Ok(exit_status) => exit(exit_status),
Err(e) => {
match e.kind() {
ErrorKind::BrokenPipe => exit(0),
_ => {
writeln!(stderr(), "{}", e).unwrap();
exit(1);
},
};
}
};
},
Err(e) => {
Expand Down
15 changes: 10 additions & 5 deletions src/exa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
})
}

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

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

self.print_dirs(dirs, no_files, is_only_dir)
self.print_dirs(dirs, no_files, is_only_dir, exit_status)
}

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

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

try!(self.print_files(Some(&dir), children));
try!(self.print_dirs(child_dirs, false, false));
match self.print_dirs(child_dirs, false, false, exit_status) {
Ok(_) => (),
Err(e) => return Err(e),
}
continue;
}
}

try!(self.print_files(Some(&dir), children));
}

Ok(())
Ok(exit_status)
}

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

0 comments on commit 64cc0d6

Please sign in to comment.