Skip to content

Commit

Permalink
feat(cli): terraform command
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrubek committed Feb 17, 2023
1 parent 1e19f13 commit 5a23102
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum AppError {

#[error("invalid args: {0}")]
InvalidArgs(String),
#[error("terraform error: {0}")]
TerraformError(i32),
}

pub type AppResult<T> = Result<T, AppError>;
19 changes: 17 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,27 @@ async fn main() -> AppResult<()> {
.ok_or_else(|| error::AppError::InvalidArgs("no terraform args".to_string()))?;

// read all args and print them
info!(?workspace, ?args);
info!(?workspace, ?args, "spawning terraform command");

// Call the terraform executable with the provided args
// The workspace will determine the directory to run terraform in (using the -chdir flag)
todo!()
// The args will be passed to terraform as-is
let mut terraform = tokio::process::Command::new("terraform")
.arg(format!("-chdir={workspace}"))
.args(args)
.spawn()?;

// From here, let the process take over. Display the output from it, both stdout and stderr
let status = terraform.wait().await?;
info!(?status);

// If the process exited with a non-zero exit code, return an error
if !status.success() {
let code = status.code();
return Err(error::AppError::TerraformError(code.expect("no exit code")));
}
}
}

Ok(())
}
1 change: 1 addition & 0 deletions flake-parts/cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
rustfmt
cargo-nextest
# misc
pkgs.terraform
];

extraNativeBuildInputs = [
Expand Down

0 comments on commit 5a23102

Please sign in to comment.