Files
atlantis/server/core/runtime/show_step_runner.go
Sarvar Muminov 90e92e3a13 chore: move CommandContext and CommandResult to models (#193) (#2093)
* Moved CommandContext and CommandResult to models (#193)

* Moved CommandContext and CommandResult to models

* move from models to command

rename CommandContext -> Context
rename CommandResult -> Result

* moved command related helpers into command package

* move ProjectCommandContext and ProjectResult to command/project package

* move project command context and project result

* revert unrelated code

* move tests

* fix left over

* fix linting

* fix tests

* remove unused import

* fix project context dependencies

* fix depenedecies

* fix typo
2022-03-21 10:36:13 -07: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/command"
)
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 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
}