Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

56 lines · 1.6 KiB · Rust

Last changed by

Just trying some things out, initial functionalityMax Trianobefore adoption0f0fa76

History of this file

Take it

1use std::io;2use std::io::Write;3use std::process::Command;4use std::sync::Mutex;56use crate::error::print_error;7use crate::exit_codes::ExitCode;89/// Executes a command.10pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {11    // Spawn the supplied command.12    /* let output = cmd.output(); */13    // This sort of works:14    let child = cmd.spawn().expect("failed to wait on child");15    let output = child16    .wait_with_output()17    .expect("failed to wait on child"); 18    ExitCode::Success19    /* let child_handle = cmd.spawn();20    match child_handle {21        Ok(output) => {22            let exit_code = output.wait();23        }24        Err() => {2526        }27    } */28    // Then wait for the command to exit, if it was spawned.29    /* match output {30        Ok(output) => {31            // While this lock is active, this thread will be the only thread allowed32            // to write its outputs.33            let _lock = out_perm.lock().unwrap();3435            let stdout = io::stdout();36            let stderr = io::stderr();3738            //let _ = stdout.lock().write_all(&output.stdout);39            //let _ = stderr.lock().write_all(&output.stderr);4041            if output.status.code() == Some(0) {42                ExitCode::Success43            } else {44                ExitCode::GeneralError45            }46        }47        Err(ref why) if why.kind() == io::ErrorKind::NotFound => {48            print_error(format!("Command not found: {:?}", cmd));49            ExitCode::GeneralError50        }51        Err(why) => {52            print_error(format!("Problem while executing command: {}", why));53            ExitCode::GeneralError54        }55    } */56}