Crate cmd_utils

Source
Expand description

§cmd-utils crate

Safe Rust Command utility traits

  • run command (spawn & wait wrapper)
  • pipe commands
  • output to file (spawn & wait & outuput to file wrapper)

§CmdRun trait

use std::process::Command;
use cmd_utils::CmdRun;

// `spawn` the command and `wait` for child process to end
// note that `run` does not return the command output, just Ok(())
// but you can redirect stdout & stderr where you want
Command::new("test")
    // .stdout(cfg)
    // .stderr(cfg)
    .args(["-n", "a"])
    .run()
    .unwrap();

§CmdPipe trait

use std::process::Command;
use std::str;
use cmd_utils::CmdPipe;

// pipe echo & wc commands
// equivalent to bash: echo test | wc -c
let mut echo = Command::new("echo");
let mut wc = Command::new("wc");
let output = echo.arg("test")
    .pipe(&mut wc.arg("-c"))
    .unwrap();
let res = str::from_utf8(&output.stdout).unwrap();
println!("pipe result: {}", &res);

§CmdToFile trait

use std::fs::File;
use std::process::Command;
use cmd_utils::CmdToFile;

let stdout = File::create("tmp/my_file.stdout").unwrap();
let stderr = File::create("tmp/my_file.stderr").unwrap();
let mut cmd = Command::new("echo");
// writes stdout to file
cmd.arg("test").to_file(stdout, Some(stderr));

Structs§

ChildError
Child process error

Enums§

CmdSpawnError
Error result of a command spawn

Traits§

CmdPipe
CmdRun
CmdToFile

Functions§

command_pipe
Pipe stdout of command to stdin of piped command
command_pipe_base
Pipe stdout of command to stdin of piped command, and pipe stdout of piped to piped_stdout
command_pipe_to_file
Pipe stdout of command to stdin of piped command and pipe stdout of piped to file
command_spawn
spawn and wait command
command_to_file
spawn, wait and send the stdout of the command to a file