Skip to content

fix: output handling #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -35,12 +35,19 @@ jobs:
name: Checkout code
- name: Install Dependencies
run: |
if [[ ${{ matrix.os }} == 'ubuntu-latest' ]]; then
sudo apt-get update -y;
sudo apt-get install -y build-essential gcc-multilib;
elif [[ ${{ matrix.os }} == 'macos-latest' ]]; then
brew install gcc;
fi
case "${{ matrix.os }}" in
"ubuntu-latest")
sudo apt-get update -y
sudo apt-get install -y build-essential gcc-multilib
;;
"macos-latest")
brew install gcc
;;
*)
echo "Unsupported OS: ${{ matrix.os }}"
exit 1
;;
esac
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod terraform;

use std::error::Error;
use terraform::install::install;
use terraform::run;
use terraform::version::find_required_version;
use terraform::{install::install, run, version::find_required_version};

fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = std::env::args().skip(1).collect();
81 changes: 47 additions & 34 deletions src/terraform/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::fmt;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{
fmt,
io::{BufRead, BufReader},
path::{Path, PathBuf},
process::{Command, Stdio},
sync::mpsc,
thread,
};

#[derive(Debug)]
pub enum TerraError {
@@ -16,7 +21,7 @@ impl fmt::Display for TerraError {
write!(f, "Invalid Terraform path: {}", path.display())
}
TerraError::CommandFailed(output) => {
write!(f, "Command failed with output:\n{}", output)
write!(f, "{}", output)
}
TerraError::InvalidArgs => write!(f, "No arguments provided"),
}
@@ -46,43 +51,51 @@ impl TerraExecutor {
return Err(TerraError::InvalidArgs);
}

let mut cmd = Command::new(&self.terra_path);
let mut cmd = Command::new(&self.terra_path)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();

for arg in args {
cmd.arg(arg);
}
let stdout = cmd.stdout.take().expect("failed to open stdout");
let stderr = cmd.stderr.take().expect("failed to open stderr");
let (tx, rx) = mpsc::channel();

let tx_clone = tx.clone();

let output = cmd
.output()
.map_err(|e| TerraError::CommandFailed(format!("Failed to execute command: {}", e)))?;

let stdout = match String::from_utf8(output.stdout) {
Ok(text) => text,
Err(e) => {
return Err(TerraError::CommandFailed(format!(
"Invalid UTF-8 sequence: {}",
e
)))
thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
let line = line.unwrap_or_default();
tx_clone.send(format!("{}", line)).unwrap();
}
};

let stderr = match String::from_utf8(output.stderr) {
Ok(text) => text,
Err(e) => {
return Err(TerraError::CommandFailed(format!(
"Invalid UTF-8 sequence: {}",
e
)))
});

thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
let line = line.unwrap_or_default();
tx.send(format!("{}", line)).unwrap();
}
};
});

let mut output = String::new();
for line in rx {
println!("{}", line);
output.push_str(&line);
output.push('\n');
}

let status = cmd
.wait()
.map_err(|e| TerraError::CommandFailed(e.to_string()))?;

if !output.status.success() {
let command_error = format!("{} {}", stdout.to_string(), stderr.to_string());
println!("{}", command_error);
return Err(TerraError::CommandFailed(command_error));
if !status.success() {
return Err(TerraError::CommandFailed(output));
}

Ok(stdout.to_string())
Ok(output)
}
}