From 7bc0f9b8bb8edb5ef4bbf5b7d6e8daed59309072 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Wed, 1 Jul 2020 07:49:17 -0500 Subject: [PATCH] Made it a function and put in better place --- lib/linter.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++++- lib/worker.sh | 37 ------------------------------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/lib/linter.sh b/lib/linter.sh index d0a5be13..9b6d9dd6 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -85,7 +85,6 @@ LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck" "stylelint" "dotenv-linter" "pwsh" "ktlint" "protolint" "clj-kondo" "spectral" "cfn-lint") - ############################# # Language array for prints # ############################# @@ -642,6 +641,48 @@ GetGitHubVars() fi } ################################################################################ +#### Function ValidatePowershellModules ######################################## +function ValidatePowershellModules() +{ + VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) + # If module found, ensure Invoke-ScriptAnalyzer command is available + if [[ "$VALIDATE_PSSA_MODULE" == "PSScriptAnalyzer" ]]; then + VALIDATE_PSSA_CMD=$(pwsh -c "(Get-Command Invoke-ScriptAnalyzer | Select-Object -First 1).Name" 2>&1) + else + # Failed to find module + exit 1 + fi + + ######################################### + # validate we found the script analyzer # + ######################################### + if [[ "$VALIDATE_PSSA_CMD" != "Invoke-ScriptAnalyzer" ]]; then + # Failed to find module + exit 1 + fi + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # Failed + echo "ERROR! Failed find module [PSScriptAnalyzer] for [$LINTER_NAME] in system!" + echo "ERROR:[PSSA_MODULE $VALIDATE_PSSA_MODULE] [PSSA_CMD $VALIDATE_PSSA_CMD]" + exit 1 + else + # Success + if [[ "$ACTIONS_RUNNER_DEBUG" == "true" ]]; then + echo "Successfully found module [$VALIDATE_PSSA_MODULE] in system" + echo "Successfully found command [$VALIDATE_PSSA_CMD] in system" + fi + fi +} +################################################################################ #### Function Footer ########################################################### Footer() { @@ -1088,6 +1129,11 @@ fi # POWERSHELL LINTING # ###################### if [ "$VALIDATE_POWERSHELL" == "true" ]; then + ############################################################### + # For POWERSHELL, ensure PSScriptAnalyzer module is available # + ############################################################### + ValidatePowershellModules + ############################# # Lint the powershell files # ############################# diff --git a/lib/worker.sh b/lib/worker.sh index 0c5e891f..82aac52b 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -61,43 +61,6 @@ function LintCodebase() fi fi - ############################################################### - # For POWERSHELL, ensure PSScriptAnalyzer module is available # - ############################################################### - if [[ "$FILE_TYPE" == "POWERSHELL" ]]; then - VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) - # If module found, ensure Invoke-ScriptAnalyzer command is available - if [[ "$VALIDATE_PSSA_MODULE" == "PSScriptAnalyzer" ]]; then - VALIDATE_PSSA_CMD=$(pwsh -c "(Get-Command Invoke-ScriptAnalyzer | Select-Object -First 1).Name" 2>&1) - else - exit 1 - fi - if [[ "$VALIDATE_PSSA_CMD" != "Invoke-ScriptAnalyzer" ]]; then - exit 1 - fi - - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? - - ############################## - # Check the shell for errors # - ############################## - if [ $ERROR_CODE -ne 0 ]; then - # Failed - echo "ERROR! Failed find module [PSScriptAnalyzer] for [$LINTER_NAME] in system!" - echo "ERROR:[PSSA_MODULE $VALIDATE_PSSA_MODULE] [PSSA_CMD $VALIDATE_PSSA_CMD]" - exit 1 - else - # Success - if [[ "$ACTIONS_RUNNER_DEBUG" == "true" ]]; then - echo "Successfully found module [$VALIDATE_PSSA_MODULE] in system" - echo "Successfully found command [$VALIDATE_PSSA_CMD] in system" - fi - fi - fi - ########################## # Initialize empty Array # ##########################