Skip to content

Commit

Permalink
optionally pipe contents of variable to stdin of an exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
tancred committed Apr 11, 2024
1 parent 2b66795 commit 4f3b192
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 38 deletions.
151 changes: 115 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ lazy_static = "1.4.0"
num_cpus = "1.13.0"
rand = "0.8.5"
hdrhistogram = "7.4.0"
tempfile = "3.10.1"

# Add openssl-sys as a direct dependency so it can be cross compiled to
# x86_64-unknown-linux-musl using the "vendored" feature below
Expand Down
25 changes: 23 additions & 2 deletions src/actions/exec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use async_trait::async_trait;
use colored::*;
use serde_json::json;
use std::process::Command;
use std::io::{Seek, SeekFrom, Write};
use std::process::{Command, Stdio};
use tempfile::tempfile;
use yaml_rust::Yaml;

use crate::actions::Runnable;
Expand All @@ -15,6 +17,7 @@ pub struct Exec {
name: String,
command: String,
pub assign: Option<String>,
pub stdin: Option<String>,
}

impl Exec {
Expand All @@ -26,11 +29,13 @@ impl Exec {
let name = extract(item, "name");
let command = extract(&item["exec"], "command");
let assign = extract_optional(item, "assign");
let stdin = extract_optional(item, "stdin");

Exec {
name,
command,
assign,
stdin,
}
}
}
Expand All @@ -46,7 +51,23 @@ impl Runnable for Exec {

let args = vec!["bash", "-c", "--", final_command.as_str()];

Check failure on line 52 in src/actions/exec.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `vec!`

error: useless use of `vec!` --> src/actions/exec.rs:52:16 | 52 | let args = vec!["bash", "-c", "--", final_command.as_str()]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["bash", "-c", "--", final_command.as_str()]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec = note: `-D clippy::useless-vec` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`

let execution = Command::new(args[0]).args(&args[1..]).output().expect("Couldn't run it");
let mut file_for_stdin = Stdio::null();

if let Some(ref key) = self.stdin {
let interpolator = interpolator::Interpolator::new(context);
let eval = format!("{{{{ {} }}}}", key);
let cmd_input = interpolator.resolve(&eval, true);
let mut f = tempfile().unwrap();
if let Err(why) = f.write_all(cmd_input.as_bytes()) {
panic!("couldn't write {}: {}", key, why);
}
if let Err(why) = f.seek(SeekFrom::Start(0)) {
panic!("couldn't rewind file: {}: {}", key, why);
}
file_for_stdin = Stdio::from(f);
}

let execution = Command::new(args[0]).args(&args[1..]).stdin(file_for_stdin).output().expect("Couldn't run it");

let output: String = String::from_utf8_lossy(&execution.stdout).into();
let output = output.trim_end().to_string();
Expand Down

0 comments on commit 4f3b192

Please sign in to comment.