From 5a78d9d8000b45edf165940eba35b6f196e97bf5 Mon Sep 17 00:00:00 2001
From: Anoop Mazhavancheri <anoopvlcy@gmail.com>
Date: Fri, 5 Jul 2024 11:56:03 +0000
Subject: [PATCH] feat: optional installation of Go tools

In case Go tools used in the template (govulncheck, gocover-cobertura, go-junit-report, go-mod-outdated) are already available in the container image, the template skips their installation
---
 templates/gitlab-ci-golang.yml | 42 ++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/templates/gitlab-ci-golang.yml b/templates/gitlab-ci-golang.yml
index 092792c..7c1e5e4 100644
--- a/templates/gitlab-ci-golang.yml
+++ b/templates/gitlab-ci-golang.yml
@@ -342,9 +342,12 @@ stages:
       log_info "--- \\e[32mCoverage report(s) found\\e[0m (\\e[33;1m${coverage_out}\\e[0m): output"
       percent=$(go tool cover -func="$coverage_out" | tail -1 | awk -F" " '{print $NF}')
       echo "${percent} covered"
-
-      go get github.com/boumenot/gocover-cobertura
-      GOFLAGS="$GO_COBERTURA_FLAGS" go run github.com/boumenot/gocover-cobertura < "$coverage_out" > reports/go-coverage.cobertura.xml
+      if ! command -v gocover-cobertura  > /dev/null
+      then
+        log_info "Installing gocover-cobertura.."
+        go install github.com/boumenot/gocover-cobertura@latest
+      fi
+      GOFLAGS="$GO_COBERTURA_FLAGS" gocover-cobertura < "$coverage_out" > reports/go-coverage.cobertura.xml
     else
       log_info "--- \\e[32mCoverage report(s) not found\\e[0m: skip"
     fi
@@ -419,7 +422,7 @@ stages:
 
     # produce JUnit report (for GitLab)
     install_go_junit_report
-    "$GOBIN/go-junit-report" < "$go_text_report" > reports/go-test.xunit.xml
+    go-junit-report < "$go_text_report" > reports/go-test.xunit.xml
 
     # produce JSON report (for SonarQube)
     go tool test2json < "$go_text_report" > reports/go-test.native.json
@@ -429,17 +432,25 @@ stages:
   }
 
   function install_go_junit_report() {
-    cd "$(mktemp -d)"
-    go mod init go-junit-report
-    go install github.com/jstemmer/go-junit-report@latest
-    cd -
+    if ! command -v go-junit-report  > /dev/null
+    then
+      cd "$(mktemp -d)"
+      go mod init go-junit-report
+      log_info "Installing go-junit-report.."
+      go install github.com/jstemmer/go-junit-report@latest
+      cd -
+    fi
   }
 
   function install_go_mod_outdated() {
-    cd "$(mktemp -d)"
-    go mod init go-mod-outdated
-    go install github.com/psampaz/go-mod-outdated@latest
-    cd -
+    if ! command -v go-mod-outdated  > /dev/null
+    then
+      cd "$(mktemp -d)"
+      go mod init go-mod-outdated
+      log_info "Installing go-mod-outdated.."
+      go install github.com/psampaz/go-mod-outdated@latest
+      cd -
+    fi  
   }
 
   function install_go_govulncheck() {
@@ -483,6 +494,7 @@ stages:
       fi
     - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
     - cd ${GO_PROJECT_DIR}
+    - export PATH=$GOBIN:$PATH
 
 go-generate:
   extends: .go-base
@@ -601,9 +613,9 @@ go-mod-outdated:
     - go $GO_LIST_ARGS > reports/go-list.native.json
     - install_go_mod_outdated
     # console output (no fail)
-    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS < reports/go-list.native.json
+    - go-mod-outdated $GO_MOD_OUTDATED_ARGS < reports/go-list.native.json
     # text report (-ci fails)
-    - $GOBIN/go-mod-outdated $GO_MOD_OUTDATED_ARGS -ci < reports/go-list.native.json > reports/go-mod-outdated.native.txt
+    - go-mod-outdated $GO_MOD_OUTDATED_ARGS -ci < reports/go-list.native.json > reports/go-mod-outdated.native.txt
   artifacts:
     name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
     expire_in: 1 day
@@ -662,7 +674,7 @@ go-govulncheck:
   script:
     - mkdir -p -m 777 reports  
     - install_go_govulncheck
-    - $GOBIN/govulncheck ${GO_VULNCHECK_ARGS}
+    - govulncheck ${GO_VULNCHECK_ARGS}
   rules:
     # exclude if GO_CI_LINT_DISABLED set
     - if: '$GO_VULNCHECK_DISABLED == "true"'
-- 
GitLab