Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions pkg/app/piped/cloudprovider/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (t *Terraform) Init(ctx context.Context, w io.Writer) error {
for _, f := range t.varFiles {
args = append(args, fmt.Sprintf("-var-file=%s", f))
}
args = append(args, "-lock=false", ".")

cmd := exec.CommandContext(ctx, t.execPath, args...)
cmd.Dir = t.dir
Expand All @@ -80,7 +79,6 @@ func (t *Terraform) SelectWorkspace(ctx context.Context, workspace string) error
"workspace",
"select",
workspace,
".",
}
cmd := exec.CommandContext(ctx, t.execPath, args...)
cmd.Dir = t.dir
Expand All @@ -103,19 +101,30 @@ func (r PlanResult) NoChanges() bool {
return r.Adds == 0 && r.Changes == 0 && r.Destroys == 0
}

func GetExitCode(err error) int {
if err == nil {
return 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder which value is better to set here. 0 or 2.
Because we are using the -detailed-exitcode flag, so with the new version of terraform, we expect err will not be nill, right?
So IMO, when err is nil, returning 2 is safer than 0.
How do you think about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even with -detailed-exitcode the err will be nil if the plan commands succeeds and produces plan with no changes.

-detailed-exitcode only gives us ability to differentiate between actual plan error (status code 1) and plan witch changes (status code 2).

So I would keep this as is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explaination.
I got your point. 💯

}
if exitErr, ok := err.(*exec.ExitError); ok {
return exitErr.ExitCode()
}
return 1
}

func (t *Terraform) Plan(ctx context.Context, w io.Writer) (PlanResult, error) {
args := []string{
"plan",
// TODO: Remove this -no-color flag after parsePlanResult supports parsing the message containing color codes.
"-no-color",
"-lock=false",
"-detailed-exitcode",
}
for _, v := range t.vars {
args = append(args, fmt.Sprintf("-var=%s", v))
}
for _, f := range t.varFiles {
args = append(args, fmt.Sprintf("-var-file=%s", f))
}
args = append(args, "-lock=false", ".")

var buf bytes.Buffer
stdout := io.MultiWriter(w, &buf)
Expand All @@ -126,11 +135,15 @@ func (t *Terraform) Plan(ctx context.Context, w io.Writer) (PlanResult, error) {
cmd.Stderr = stdout

io.WriteString(w, fmt.Sprintf("terraform %s", strings.Join(args, " ")))
if err := cmd.Run(); err != nil {
err := cmd.Run()
switch GetExitCode(err) {
case 0:
return PlanResult{}, nil
case 2:
return parsePlanResult(buf.String())
default:
return PlanResult{}, err
}

return parsePlanResult(buf.String())
}

var (
Expand Down Expand Up @@ -185,7 +198,6 @@ func (t *Terraform) Apply(ctx context.Context, w io.Writer) error {
for _, f := range t.varFiles {
args = append(args, fmt.Sprintf("-var-file=%s", f))
}
args = append(args, ".")

cmd := exec.CommandContext(ctx, t.execPath, args...)
cmd.Dir = t.dir
Expand Down