Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

39 lines · 1.3 KiB · Rust

Last changed by

Unify error messages, closes #342sharkdpbefore adoption81a27fa

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::{Arc, Mutex};1314/// Executes a command.15pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {16    // Spawn the supplied command.17    let output = cmd.output();1819    // Then wait for the command to exit, if it was spawned.20    match output {21        Ok(output) => {22            // While this lock is active, this thread will be the only thread allowed23            // to write its outputs.24            let _lock = out_perm.lock().unwrap();2526            let stdout = io::stdout();27            let stderr = io::stderr();2829            let _ = stdout.lock().write_all(&output.stdout);30            let _ = stderr.lock().write_all(&output.stderr);31        }32        Err(ref why) if why.kind() == io::ErrorKind::NotFound => {33            print_error!("Command not found: {:?}", cmd);34        }35        Err(why) => {36            print_error!("Problem while executing command: {}", why);37        }38    }39}