Repository: diggerhq/digger
Pull request #254 (2023-05-15 10:42:03)

fix(deps): update module github.com/caarlos0/env/v7 to v8
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/caarlos0/env/v7](https://togithub.com/caarlos0/env) | require | major | `v7.1.0` -> `v8.0.0` |

---

### Release Notes

<details>
<summary>caarlos0/env</summary>

### [`v8.0.0`](https://togithub.com/caarlos0/env/releases/tag/v8.0.0)

[Compare Source](https://togithub.com/caarlos0/env/compare/v7.1.0...v8.0.0)

#### Changelog

##### Other work

-   [`224d85f`](https://togithub.com/caarlos0/env/commit/224d85fd8e951dea1fe7cab6c51f390afd4b7efa): build: fix goreleaser flags ([@&#8203;caarlos0](https://togithub.com/caarlos0))
-   [`fac0414`](https://togithub.com/caarlos0/env/commit/fac0414d0b77f17582fba4aaa5d435820c5a39f0): build: test on go stable and oldstable ([#&#8203;259](https://togithub.com/caarlos0/env/issues/259)) ([@&#8203;caarlos0](https://togithub.com/caarlos0))
-   [`4e0f915`](https://togithub.com/caarlos0/env/commit/4e0f9156cfcf67667dd263ad13f0f9110cbe1798): feat!: refactor parse and parse with options ([#&#8203;256](https://togithub.com/caarlos0/env/issues/256)) ([@&#8203;caarlos0](https://togithub.com/caarlos0))

#### Updating from v7

-   if you use `env.Parse` with no options, no change is required.
-   if you use `env.Parse` with one `Options{}`, call `env.ParseWithOptions` instead.
-   if you use `env.Parse` with options, and pass more than one `Options{}`, merge them into a single one, and use `env.ParseWithOptions` instead.
-   if you use `env.ParseWithFuncs`, use `env.ParseWithOptions` instead, and put your parsers into `Options{ FuncMap: <here> }`.

***

*Released with [GoReleaser Pro](https://goreleaser.com/pro)!*

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/diggerhq/digger).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS43OS4xIiwidXBkYXRlZEluVmVyIjoiMzUuODkuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->



Patch:
diff --git a/action.yml b/action.yml
index ecaf1082..4d380823 100644
--- a/action.yml
+++ b/action.yml
@@ -56,6 +56,14 @@ inputs:
   upload-plan-destination:
     description: Destination to upload the plan to. gcp and github are currently supported
     required: false
+  setup-checkov:
+    description: Setup Checkov
+    required: false
+    default: 'false'
+  checkov-version:
+    description: Checkov version
+    required: false
+    default: '2.3.245'
 
 outputs:
   output:
@@ -126,11 +134,22 @@ runs:
         terragrunt_version: ${{ inputs.terragrunt-version }}
       if: inputs.setup-terragrunt == 'true'
 
+    - name: Setup Checkov
+      run: |
+        python3 -m venv .venv
+        source .venv/bin/activate
+        pip3 install --upgrade pip
+        pip3 install --upgrade setuptools
+        pip3 install -U checkov==${{ inputs.checkov-version }}
+      shell: bash
+      if: inputs.setup-checkov == 'true'
+
     - name: build and run digger
       if: ${{ !startsWith(github.action_ref, 'v') }}
       shell: bash
       env:
         PLAN_UPLOAD_DESTINATION: ${{ inputs.upload-plan-destination }}
+        ACTIVATE_VENV: ${{ inputs.setup-checkov == 'true' }}
       run: |
           cd ${{ github.action_path }}
           go build -o digger ./cmd/digger
diff --git a/go.mod b/go.mod
index 3ac5e594..bc6763c9 100644
--- a/go.mod
+++ b/go.mod
@@ -9,6 +9,7 @@ require (
 	github.com/aws/aws-sdk-go v1.44.264
 	github.com/bmatcuk/doublestar/v4 v4.6.0
 	github.com/caarlos0/env/v7 v7.1.0
+	github.com/caarlos0/env/v8 v8.0.0
 	github.com/google/go-github/v51 v51.0.0
 	github.com/google/uuid v1.3.0
 	github.com/stretchr/testify v1.8.2
diff --git a/go.sum b/go.sum
index 0e6050cb..a7d55255 100644
--- a/go.sum
+++ b/go.sum
@@ -38,6 +38,7 @@ github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTS
 github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
 github.com/caarlos0/env/v7 v7.1.0 h1:9lzTF5amyQeWHZzuZeKlCb5FWSUxpG1js43mhbY8ozg=
 github.com/caarlos0/env/v7 v7.1.0/go.mod h1:LPPWniDUq4JaO6Q41vtlyikhMknqymCLBw0eX4dcH1E=
+github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
 github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
diff --git a/pkg/digger/digger.go b/pkg/digger/digger.go
index 1259bf6f..d2c6144d 100644
--- a/pkg/digger/digger.go
+++ b/pkg/digger/digger.go
@@ -11,6 +11,7 @@ import (
 	"errors"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"log"
 	"os"
 	"os/exec"
@@ -373,17 +374,34 @@ type DiggerExecutor struct {
 }
 
 type CommandRun interface {
-	Run(workingDir string, shell string, command string) (string, string, error)
+	Run(workingDir string, shell string, commands []string) (string, string, error)
 }
 
 type CommandRunner struct {
 }
 
-func (c CommandRunner) Run(workingDir string, shell string, command string) (string, string, error) {
+func (c CommandRunner) Run(workingDir string, shell string, commands []string) (string, string, error) {
+	var args []string
 	if shell == "" {
 		shell = "bash"
+		args = []string{"-eo", "pipefail"}
 	}
-	cmd := exec.Command(shell, "-c", command)
+
+	scriptFile, err := ioutil.TempFile("", "run-script")
+	if err != nil {
+		return "", "", fmt.Errorf("error creating script file: %v", err)
+	}
+	defer os.Remove(scriptFile.Name())
+
+	for _, command := range commands {
+		_, err := scriptFile.WriteString(command + "\n")
+		if err != nil {
+			return "", "", fmt.Errorf("error writing to script file: %v", err)
+		}
+	}
+	args = append(args, scriptFile.Name())
+
+	cmd := exec.Command(shell, args...)
 	cmd.Dir = workingDir
 
 	var stdout, stderr bytes.Buffer
@@ -391,7 +409,7 @@ func (c CommandRunner) Run(workingDir string, shell string, command string) (str
 	mwerr := io.MultiWriter(os.Stderr, &stderr)
 	cmd.Stdout = mwout
 	cmd.Stderr = mwerr
-	err := cmd.Run()
+	err = cmd.Run()
 
 	if err != nil {
 		return stdout.String(), stderr.String(), fmt.Errorf("error: %v", err)
@@ -458,8 +476,13 @@ func (d DiggerExecutor) Plan(prNumber int) error {
 				d.CIService.PublishComment(prNumber, comment)
 			}
 			if step.Action == "run" {
-				stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
-				log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
+				var commands []string
+				if os.Getenv("ACTIVATE_VENV") == "true" {
+					commands = append(commands, fmt.Sprintf("source %v/.venv/bin/activate", os.Getenv("GITHUB_WORKSPACE")))
+				}
+				commands = append(commands, step.Value)
+				log.Printf("Running %v for **%v**\n", step.Value, d.ProjectLock.LockId())
+				_, _, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, commands)
 				if err != nil {
 					return fmt.Errorf("error running command: %v", err)
 				}
@@ -522,8 +545,13 @@ func (d DiggerExecutor) Apply(prNumber int) error {
 					}
 				}
 				if step.Action == "run" {
-					stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
-					log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
+					var commands []string
+					if os.Getenv("ACTIVATE_VENV") == "true" {
+						commands = append(commands, fmt.Sprintf("source %v/.venv/bin/activate", os.Getenv("GITHUB_WORKSPACE")))
+					}
+					commands = append(commands, step.Value)
+					log.Printf("Running %v for **%v**\n", step.Value, d.ProjectLock.LockId())
+					_, _, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, commands)
 					if err != nil {
 						return fmt.Errorf("error running command: %v", err)
 					}


Test patch:
diff --git a/pkg/digger/digger_test.go b/pkg/digger/digger_test.go
index 3743e734..a6621ced 100644
--- a/pkg/digger/digger_test.go
+++ b/pkg/digger/digger_test.go
@@ -21,8 +21,8 @@ type MockCommandRunner struct {
 	Commands []RunInfo
 }
 
-func (m *MockCommandRunner) Run(workDir string, shell string, command string) (string, string, error) {
-	m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + command, time.Now()})
+func (m *MockCommandRunner) Run(workDir string, shell string, commands []string) (string, string, error) {
+	m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + strings.Join(commands, " "), time.Now()})
 	return "", "", nil
 }
 
@@ -182,7 +182,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) {
 
 	commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)
 
-	assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n  <summary>Apply for ****</summary>\n\n  ```terraform\n\n  ```\n</details>", "Run   echo", "LockId "}, commandStrings)
+	assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n  <summary>Apply for ****</summary>\n\n  ```terraform\n\n  ```\n</details>", "LockId ", "Run   echo"}, commandStrings)
 }
 
 func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {
@@ -224,7 +224,7 @@ func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {
 
 	commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)
 
-	assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n  <summary>Plan for ****</summary>\n\n  ```terraform\n\n  ```\n</details>", "Run   echo", "LockId "}, commandStrings)
+	assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n  <summary>Plan for ****</summary>\n\n  ```terraform\n\n  ```\n</details>", "LockId ", "Run   echo"}, commandStrings)
 }
 
 func allCommandsInOrderWithParams(terraformExecutor *MockTerraformExecutor, commandRunner *MockCommandRunner, prManager *MockPRManager, lock *MockProjectLock, planStorage *MockPlanStorage) []string {