Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

49 lines · 1.6 KiB · Rust

Last changed by

Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd

History of this file

Take it

1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::io;10use std::io::Write;11use std::process::Command;12use std::sync::Mutex;1314use crate::exit_codes::ExitCode;1516/// Executes a command.17pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {18    // Spawn the supplied command.19    let output = cmd.output();2021    // Then wait for the command to exit, if it was spawned.22    match output {23        Ok(output) => {24            // While this lock is active, this thread will be the only thread allowed25            // to write its outputs.26            let _lock = out_perm.lock().unwrap();2728            let stdout = io::stdout();29            let stderr = io::stderr();3031            let _ = stdout.lock().write_all(&output.stdout);32            let _ = stderr.lock().write_all(&output.stderr);3334            if output.status.code() == Some(0) {35                ExitCode::Success36            } else {37                ExitCode::GeneralError38            }39        }40        Err(ref why) if why.kind() == io::ErrorKind::NotFound => {41            print_error!("Command not found: {:?}", cmd);42            ExitCode::GeneralError43        }44        Err(why) => {45            print_error!("Problem while executing command: {}", why);46            ExitCode::GeneralError47        }48    }49}