From b5e74e561a0a581eda0ca1b5935c77c8b6deaf01 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 13:38:39 +0100 Subject: [PATCH 01/13] Update LINTER_ARRAY value for PowerShell (Core) --- lib/linter.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linter.sh b/lib/linter.sh index e4d64c3c..c83d032f 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -80,7 +80,7 @@ CLOJURE_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CLOJURE_FILE_NAME" LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck" "pylint" "perl" "rubocop" "coffeelint" "eslint" "standard" "ansible-lint" "/dockerfilelint/bin/dockerfilelint" "golangci-lint" "tflint" - "stylelint" "dotenv-linter" "powershell" "ktlint" "protolint" "clj-kondo" + "stylelint" "dotenv-linter" "pwsh" "ktlint" "protolint" "clj-kondo" "spectral" "cfn-lint") From 0b7f7ea2dc4b528bbbba7fef0190c3e92ea842a5 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 13:47:35 +0100 Subject: [PATCH 02/13] Update PSScriptAnalyzer installation command --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 0af0a65e..ad854548 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,6 +39,8 @@ RUN apk add --no-cache \ ######################################### # Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 # Slightly modified to always retrieve latest stable Powershell version +# Specify PSScriptAnalyzer Repository and Version for stability and set scope to CurrentUser +ARG PSSA_VERSION='1.19.0' RUN mkdir -p /opt/microsoft/powershell/7 \ && curl -s https://api.github.com/repos/powershell/powershell/releases/latest \ | grep browser_download_url \ @@ -47,7 +49,7 @@ RUN mkdir -p /opt/microsoft/powershell/7 \ | xargs -n 1 wget -O - \ | tar -xzC /opt/microsoft/powershell/7 \ && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'install-module psscriptanalyzer -force' + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope CurrentUser -Force' ##################### # Run Pip3 Installs # From 6f1b121b3f8264827e84f7d6d77af85296570854 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 14:09:17 +0100 Subject: [PATCH 03/13] Test adding Import-Module for PSScriptAnalyzer --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ad854548..073881de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,7 +49,8 @@ RUN mkdir -p /opt/microsoft/powershell/7 \ | xargs -n 1 wget -O - \ | tar -xzC /opt/microsoft/powershell/7 \ && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope CurrentUser -Force' + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope CurrentUser -Force' \ + && pwsh -c 'Import-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Force' ##################### # Run Pip3 Installs # From e8571c04ef233948ec1a0f52a4b820b93b5b6e5e Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 16:12:40 +0100 Subject: [PATCH 04/13] Remove CurrentUser scope for PSScriptAnalyzer --- Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 073881de..cebdf2a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,7 +39,7 @@ RUN apk add --no-cache \ ######################################### # Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 # Slightly modified to always retrieve latest stable Powershell version -# Specify PSScriptAnalyzer Repository and Version for stability and set scope to CurrentUser +# Specify PSScriptAnalyzer Repository and Version for stability ARG PSSA_VERSION='1.19.0' RUN mkdir -p /opt/microsoft/powershell/7 \ && curl -s https://api.github.com/repos/powershell/powershell/releases/latest \ @@ -49,8 +49,7 @@ RUN mkdir -p /opt/microsoft/powershell/7 \ | xargs -n 1 wget -O - \ | tar -xzC /opt/microsoft/powershell/7 \ && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope CurrentUser -Force' \ - && pwsh -c 'Import-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Force' + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Force' ##################### # Run Pip3 Installs # From 9b13062ca02c252fc4ad4910e8d39ecd9d9f9a2e Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 20:32:42 +0100 Subject: [PATCH 05/13] Update PowerShell checks and command --- lib/linter.sh | 2 +- lib/worker.sh | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/linter.sh b/lib/linter.sh index c83d032f..86d39f4a 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -1095,7 +1095,7 @@ if [ "$VALIDATE_POWERSHELL" == "true" ]; then # Lint the powershell files # ############################# # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" - LintCodebase "POWERSHELL" "pwsh" "pwsh -c Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" + LintCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" fi ################### diff --git a/lib/worker.sh b/lib/worker.sh index 287215dc..726b6349 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -35,9 +35,9 @@ function LintCodebase() PRINT_ARRAY+=("----------------------------------------------") PRINT_ARRAY+=("----------------------------------------------") - ####################################### - # Validate we have jsonlint installed # - ####################################### + ##################################### + # Validate we have linter installed # + ##################################### # shellcheck disable=SC2230 VALIDATE_INSTALL_CMD=$(command -v "$LINTER_NAME" 2>&1) @@ -62,6 +62,40 @@ 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) + echo "VALIDATE_PSSA_MODULE: $VALIDATE_PSSA_MODULE" + fi + # 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) + echo "VALIDATE_PSSA_CMD: $VALIDATE_PSSA_CMD" + fi + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # Failed + echo "ERROR! Failed to import [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 imported module [$VALIDATE_PSSA_MODULE]" + echo "Successfully found command in system [$VALIDATE_PSSA_CMD]" + fi + fi + ########################## # Initialize empty Array # ########################## @@ -169,7 +203,7 @@ function LintCodebase() # Lint the file with the rules # ################################ # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell - LINT_CMD=$(cd "$GITHUB_WORKSPACE" || exit; $LINTER_COMMAND "$FILE"; exit $? 2>&1) + LINT_CMD=$(cd "$GITHUB_WORKSPACE" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ # Lint the file with the rules # @@ -358,7 +392,7 @@ function TestCodebase() # Lint the file with the rules # ################################ # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell - LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; $LINTER_COMMAND "$FILE"; exit $? 2>&1) + LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; pwsh -c "$LINTER_COMMAND $FILE"; exit $? 2>&1) else ################################ # Lint the file with the rules # From 5f2cd423a428fb8b9842b218ee4ba61c4695d10a Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Tue, 30 Jun 2020 20:57:43 +0100 Subject: [PATCH 06/13] Update PowerShell module validation --- lib/worker.sh | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/worker.sh b/lib/worker.sh index 726b6349..b56b17ef 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -57,7 +57,7 @@ function LintCodebase() else # Success if [[ "$ACTIONS_RUNNER_DEBUG" == "true" ]]; then - echo "Successfully found binary in system" + echo "Successfully found binary for [$LINTER_NAME] in system" echo "Location:[$VALIDATE_INSTALL_CMD]" fi fi @@ -67,32 +67,30 @@ function LintCodebase() ############################################################### if [[ "$FILE_TYPE" == "POWERSHELL" ]]; then VALIDATE_PSSA_MODULE=$(pwsh -c "(Get-Module -Name PSScriptAnalyzer -ListAvailable | Select-Object -First 1).Name" 2>&1) - echo "VALIDATE_PSSA_MODULE: $VALIDATE_PSSA_MODULE" - fi - # 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) - echo "VALIDATE_PSSA_CMD: $VALIDATE_PSSA_CMD" - fi + # 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) + fi - ####################### - # Load the error code # - ####################### - ERROR_CODE=$? + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? - ############################## - # Check the shell for errors # - ############################## - if [ $ERROR_CODE -ne 0 ]; then - # Failed - echo "ERROR! Failed to import [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 imported module [$VALIDATE_PSSA_MODULE]" - echo "Successfully found command in system [$VALIDATE_PSSA_CMD]" + ############################## + # 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 From 509f767b71beb7ad0b95709d1aa62f175d5d2996 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 08:27:29 +0100 Subject: [PATCH 07/13] Update TestCodebase() for PowerShell --- lib/worker.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/worker.sh b/lib/worker.sh index b56b17ef..179d417f 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -390,7 +390,7 @@ function TestCodebase() # Lint the file with the rules # ################################ # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell - LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; pwsh -c "$LINTER_COMMAND $FILE"; exit $? 2>&1) + LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ # Lint the file with the rules # From 5ef64d0eff426d3817e90890445a4d5bd812c2d9 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 08:34:01 +0100 Subject: [PATCH 08/13] Explicitly set -Scope AllUsers for PSSA Module --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index cebdf2a8..47c46881 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,7 +49,7 @@ RUN mkdir -p /opt/microsoft/powershell/7 \ | xargs -n 1 wget -O - \ | tar -xzC /opt/microsoft/powershell/7 \ && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Force' + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' ##################### # Run Pip3 Installs # From 6831d3735af11d5bdf8ccbac38682bd540c3e42d Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 08:43:09 +0100 Subject: [PATCH 09/13] Updated PSScriptAnalyzer validation for pwsh --- lib/worker.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/worker.sh b/lib/worker.sh index fd0481a0..15d89442 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -69,6 +69,11 @@ function LintCodebase() # 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 ####################### From 138d79152ee30fb83aa5c48851dd9d0ce6c43f36 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 09:02:23 +0100 Subject: [PATCH 10/13] Set version using ARG for PWSH and PSSA --- Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 607fee46..13a1a261 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,16 +40,17 @@ RUN apk add --no-cache \ # Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 # Slightly modified to always retrieve latest stable Powershell version # Specify PSScriptAnalyzer Repository and Version for stability -ARG PSSA_VERSION='1.19.0' +ARG PWSH_VERSION='latest' +ARG PSSA_VERSION='latest' RUN mkdir -p /opt/microsoft/powershell/7 \ - && curl -s https://api.github.com/repos/powershell/powershell/releases/latest \ + && curl -s https://api.github.com/repos/powershell/powershell/releases/${PWSH_VERSION} \ | grep browser_download_url \ | grep linux-alpine-x64 \ | cut -d '"' -f 4 \ | xargs -n 1 wget -O - \ | tar -xzC /opt/microsoft/powershell/7 \ && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ - && pwsh -c 'Install-Module -Name PSScriptAnalyzer -Repository PSGallery -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' + && pwsh -c 'Install-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' ##################### # Run Pip3 Installs # From 4d3117057f61defac34480a0440e6b4b86c7740b Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 09:13:30 +0100 Subject: [PATCH 11/13] Update comment on corner case for PowerShell --- lib/worker.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/worker.sh b/lib/worker.sh index 15d89442..0c5e891f 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -203,7 +203,7 @@ function LintCodebase() ################################ # Lint the file with the rules # ################################ - # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell LINT_CMD=$(cd "$GITHUB_WORKSPACE" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ @@ -370,7 +370,7 @@ function TestCodebase() ################################ # Lint the file with the rules # ################################ - # Need to append "'" to make the pwsh call syntax correct, also exit with exit code from inner subshell + # Need to run PowerShell commands using pwsh -c, also exit with exit code from inner subshell LINT_CMD=$(cd "$GITHUB_WORKSPACE/$TEST_CASE_FOLDER" || exit; pwsh -c "($LINTER_COMMAND $FILE)"; exit $? 2>&1) else ################################ From 6792c8bbfa5e554de9f0e673705a854c0541a9a7 Mon Sep 17 00:00:00 2001 From: Kevin Rowlandson Date: Wed, 1 Jul 2020 11:12:45 +0100 Subject: [PATCH 12/13] Remove version (7) from PowerShell directory path --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 13a1a261..56bc44a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,17 +39,18 @@ RUN apk add --no-cache \ ######################################### # Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 # Slightly modified to always retrieve latest stable Powershell version -# Specify PSScriptAnalyzer Repository and Version for stability +# If changing PWSH_VERSION='latest' to a specific version, use format PWSH_VERSION='tags/v7.0.2' ARG PWSH_VERSION='latest' +ARG PWSH_DIRECTORY='/opt/microsoft/powershell' ARG PSSA_VERSION='latest' -RUN mkdir -p /opt/microsoft/powershell/7 \ +RUN mkdir -p ${PWSH_DIRECTORY} \ && curl -s https://api.github.com/repos/powershell/powershell/releases/${PWSH_VERSION} \ | grep browser_download_url \ | grep linux-alpine-x64 \ | cut -d '"' -f 4 \ | xargs -n 1 wget -O - \ - | tar -xzC /opt/microsoft/powershell/7 \ - && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \ + | tar -xzC ${PWSH_DIRECTORY} \ + && ln -s ${PWSH_DIRECTORY}/pwsh /usr/bin/pwsh -f \ && pwsh -c 'Install-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' ##################### 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 13/13] 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 # ##########################