#command-runner #execution #execute-command #tokio #asynchronous #exit-status

lightweight-command-runner

A lightweight asynchronous command execution library using Tokio

1 unstable release

new 0.1.0 Feb 9, 2025

#20 in #exit-status

Download history 111/week @ 2025-02-05

111 downloads per month
Used in 6 crates (2 directly)

MIT/Apache

12KB
80 lines

Lightweight Command Runner

Lightweight Command Runner is a lightweight asynchronous command execution library that provides a simple trait-based interface for running system commands using Tokio. With a default implementation and the ability to customize, it lets you integrate command execution easily into your asynchronous Rust applications.

Features

  • Asynchronous Execution: Run system commands using async/await.
  • Trait-Based API: Easily swap out or extend the command runner with your own implementation.
  • Cross-Platform Support: Includes Unix/Windows specific extensions for exit status handling.
  • Ergonomic Design: Clean, minimal API for straightforward command execution.

Installation

Add the following to your Cargo.toml:

[dependencies]
lightweight-command-runner = "0.1.0"

Usage

Here's a basic example using the default implementation:

use lightweight_command_runner::{CommandRunner, DefaultCommandRunner};
use tokio::process::Command;

#[tokio::main]
async fn main() {
    let runner = DefaultCommandRunner;

    // Configure the command based on the target OS
    let mut cmd = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.args(["/C", "echo hello"]);
        c
    } else {
        let mut c = Command::new("echo");
        c.arg("hello");
        c
    };

    match runner.run_command(cmd).await {
        Ok(output) => {
            println!("Command executed successfully!");
            println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
        },
        Err(e) => {
            eprintln!("Command execution failed: {}", e);
        }
    }
}

License

This project is dual-licensed under either the MIT license or the Apache License, Version 2.0, at your option.

Contributing

Contributions are welcome! Please check out the repository for details.

Dependencies

~2.3–8MB
~58K SLoC