Files
atlantis/server/core/runtime/show_step_runner.go
Eng Zer Jun 38cf7b0141 refactor: move from io/ioutil to io and os package (#1843)
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-10-07 00:37:42 -04:00

60 lines
1.6 KiB
Go

package runtime
import (
"os"
"path/filepath"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
)
const minimumShowTfVersion string = "0.12.0"
func NewShowStepRunner(executor TerraformExec, defaultTFVersion *version.Version) (Runner, error) {
runner := &PlanTypeStepRunnerDelegate{
defaultRunner: &ShowStepRunner{
TerraformExecutor: executor,
DefaultTFVersion: defaultTFVersion,
},
remotePlanRunner: NullRunner{},
}
return NewMinimumVersionStepRunnerDelegate(minimumShowTfVersion, defaultTFVersion, runner)
}
// ShowStepRunner runs terraform show on an existing plan file and outputs it to a json file
type ShowStepRunner struct {
TerraformExecutor TerraformExec
DefaultTFVersion *version.Version
}
func (p *ShowStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string, envs map[string]string) (string, error) {
tfVersion := p.DefaultTFVersion
if ctx.TerraformVersion != nil {
tfVersion = ctx.TerraformVersion
}
planFile := filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName))
showResultFile := filepath.Join(path, ctx.GetShowResultFileName())
output, err := p.TerraformExecutor.RunCommandWithVersion(
ctx.Log,
path,
[]string{"show", "-no-color", "-json", filepath.Clean(planFile)},
envs,
tfVersion,
ctx.Workspace,
)
if err != nil {
return "", errors.Wrap(err, "running terraform show")
}
if err := os.WriteFile(showResultFile, []byte(output), os.ModePerm); err != nil {
return "", errors.Wrap(err, "writing terraform show result")
}
return output, nil
}