forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_step_runner.go
50 lines (44 loc) · 1.42 KB
/
run_step_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package runtime
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
)
// RunStepRunner runs custom commands.
type RunStepRunner struct {
DefaultTFVersion *version.Version
}
func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command []string, path string) (string, error) {
if len(command) < 1 {
return "", errors.New("no commands for run step")
}
cmd := exec.Command("sh", "-c", strings.Join(command, " ")) // #nosec
cmd.Dir = path
tfVersion := r.DefaultTFVersion.String()
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion.String()
}
baseEnvVars := os.Environ()
customEnvVars := []string{
fmt.Sprintf("WORKSPACE=%s", ctx.Workspace),
fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", tfVersion),
fmt.Sprintf("DIR=%s", path),
fmt.Sprintf("PLANFILE=%s", filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))),
}
finalEnvVars := append(baseEnvVars, customEnvVars...)
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()
commandStr := strings.Join(command, " ")
if err != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, commandStr, path, out)
ctx.Log.Debug("error: %s", err)
return string(out), err
}
ctx.Log.Info("successfully ran %q in %q", commandStr, path)
return string(out), nil
}