From 137519c4b8310908a3d10d2a94647f8a360fa3f2 Mon Sep 17 00:00:00 2001 From: Jason Williscroft Date: Mon, 12 Aug 2024 18:26:04 +0800 Subject: [PATCH] [GH-12] excluded terraform init from workspace switch --- src/cli/metastructure/index.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/cli/metastructure/index.ts b/src/cli/metastructure/index.ts index 26dc55c..5557868 100644 --- a/src/cli/metastructure/index.ts +++ b/src/cli/metastructure/index.ts @@ -170,18 +170,27 @@ const cli = new Command() }); // Check workspace & switch if necessary. - const { stdout } = await $`terraform workspace show`; - if (stdout !== workspace) { - console.log(chalk.black.bold('Switching workspace...\n')); - await $({ - stdio: 'inherit', - })`terraform workspace select -or-create=true ${workspace}`; + const flatCmd = command.join(' '); + + const cleanCmd = flatCmd.toLowerCase().replace(/\s+/g, ' '); + + if (!cleanCmd.includes('terraform init')) { + const { stdout } = await $`terraform workspace show`; + + if (stdout !== workspace) { + console.log(chalk.black.bold('Switching workspace...\n')); + await $({ + stdio: 'inherit', + })`terraform workspace select -or-create=true ${workspace}`; + } } console.log(chalk.black.bold('Running command...\n')); + await $({ stdio: 'inherit', - })(command.join(' ')); + })(flatCmd); + console.log(chalk.green.bold('\nDone!\n')); } catch (error) { console.log(chalk.red.bold('\nCommand failed!\n'));