Files
atlantis/server/core/runtime/show_step_runner.go
Ken Kaizu c77149396a fix: atlantis import on workspaces (#2937)
* atlantis import not works on workspace

* extract workspace_step_runner_delegate from plan_step_runner

* import step runner requires workspace delegate
2023-01-11 17:53:45 -06:00

58 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/command"
)
const minimumShowTfVersion string = "0.12.0"
func NewShowStepRunner(executor TerraformExec, defaultTFVersion *version.Version) (Runner, error) {
showStepRunner := &showStepRunner{
terraformExecutor: executor,
defaultTFVersion: defaultTFVersion,
}
remotePlanRunner := NullRunner{}
runner := NewPlanTypeStepRunnerDelegate(showStepRunner, remotePlanRunner)
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 command.ProjectContext, 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,
path,
[]string{"show", "-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
}