Skip to content

Commit

Permalink
feat(exe): TFAM_EXE
Browse files Browse the repository at this point in the history
  • Loading branch information
Ant0wan committed May 12, 2023
1 parent 4783d7e commit 71dfd11
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ By providing these features, Terraform Apply Manager offers a seamless and flexi

## Usage

`TF_WORKSPACE_FORMAT`: awk-like format for workspace from file path $1 = $n-1, $2 = $n-2 `-workspace-format='hello$2_$1_$3'
`TFAM_EXE`: `TFAM_EXE=tfexe tfam init` or `TFAM_EXE=terraform tfam init` or `TFAM_EXE=path/to/your/binary tfam init`

`TF_WORKSPACE_FORMAT`: awk-like format for workspace from file path $1=$n-1, $2=$n-2 `-workspace-format='hello$2_$1_$3'

`export TF_WORKSPACE_FORMAT='$-3_$-1_$-2'`
or

`tfam -interactive -workspace-format='$-3_$-1_$-2`

`-interactive`: selection menu
Expand All @@ -54,6 +57,10 @@ or
complete -W "-interactive -concurrent -workspace-format" -d -f -C $(which terraform) tfam
```

## Coming features

When performing `workspace` command + -interactive, it will display a workspace menu list.

## License

This repository is protected by the GPL3 (GNU General Public License v3.0). You can find the full text of the license in the LICENSE file. Please review and comply with the terms and conditions of the GPL3 license before using or contributing to this project.
Expand Down
34 changes: 23 additions & 11 deletions src/exec.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
use std::env;
use std::process::Command;
use std::thread;

use crate::cli::Commands;
use crate::workspace::get_workspace;

pub fn execute_varfiles(args: Vec<String>, cmd: Commands) {
let executable = match env::var("TFAM_EXE") {
Ok(exe) => exe,
Err(_) => "terraform".to_string(),
};
if cmd.concurrent {
multi_threads_exec(args, cmd);
multi_threads_exec(args, cmd, executable);
} else {
single_threaded_exec(args, cmd);
single_threaded_exec(args, cmd, executable);
}
}

fn exec(args: Vec<String>, varfile: String, workspaceformat: String) {
fn exec(args: Vec<String>, varfile: String, workspaceformat: String, executable: String) {
let workspace = get_workspace(varfile.clone(), workspaceformat);
println!(
"TF_WORKSPACE={} terraform {} -var-file={:?}",
"TF_WORKSPACE={} {} {} -var-file={:?}",
workspace,
executable,
args.join(" "),
varfile
);
let _status = Command::new("terraform")
let _status = Command::new(executable)
.env("TF_WORKSPACE", workspace)
.args(args)
.arg("-var-file")
.arg(varfile)
.status();
}

fn single_threaded_exec(args: Vec<String>, cmd: Commands) {
fn single_threaded_exec(args: Vec<String>, cmd: Commands, executable: String) {
if cmd.varfiles.is_empty() {
println!("terraform {}", args.join(" "));
let _status = Command::new("terraform").args(args).status();
println!("{} {}", executable, args.join(" "));
let _status = Command::new(executable).args(args).status();
return;
}
for f in cmd.varfiles {
exec(args.clone(), f, cmd.workspaceformat.clone());
exec(
args.clone(),
f,
cmd.workspaceformat.clone(),
executable.clone(),
);
}
}

fn multi_threads_exec(args: Vec<String>, cmd: Commands) {
fn multi_threads_exec(args: Vec<String>, cmd: Commands, executable: String) {
let threads: Vec<_> = cmd
.varfiles
.into_iter()
.map(|f| {
let args = args.clone();
let workspaceformat = cmd.workspaceformat.clone();
let executable = executable.clone();
thread::spawn(move || {
exec(args, f, workspaceformat);
exec(args, f, workspaceformat, executable);
})
})
.collect();
Expand Down

0 comments on commit 71dfd11

Please sign in to comment.