From 903e28c8ac744a16877753468f46d575c26c5943 Mon Sep 17 00:00:00 2001 From: p0tr3c Date: Sun, 27 Jun 2021 22:32:40 +0100 Subject: [PATCH 1/2] fix: remove obsolete dot parameter Terraform directory parameter has to be specified via optional -chdir flag. This commit removed the explicit '.' which is invalid since terraform 1.0.0 --- pkg/app/piped/cloudprovider/terraform/terraform.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/app/piped/cloudprovider/terraform/terraform.go b/pkg/app/piped/cloudprovider/terraform/terraform.go index e2fa3baf6b..8fd58244ef 100644 --- a/pkg/app/piped/cloudprovider/terraform/terraform.go +++ b/pkg/app/piped/cloudprovider/terraform/terraform.go @@ -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 @@ -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 @@ -115,7 +113,6 @@ func (t *Terraform) Plan(ctx context.Context, w io.Writer) (PlanResult, error) { 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) @@ -185,7 +182,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 From 84127a081547aa1a031089fa0708e7708f2d5cc3 Mon Sep 17 00:00:00 2001 From: p0tr3c Date: Sun, 27 Jun 2021 22:35:07 +0100 Subject: [PATCH 2/2] refactor: status code to determine changes Terraform provides detailed status code which can be used in plan commands to determine if plan contains any changes. This commit adds explicit status check to before sending the results for parsing --- .../cloudprovider/terraform/terraform.go | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/app/piped/cloudprovider/terraform/terraform.go b/pkg/app/piped/cloudprovider/terraform/terraform.go index 8fd58244ef..18b8a05a38 100644 --- a/pkg/app/piped/cloudprovider/terraform/terraform.go +++ b/pkg/app/piped/cloudprovider/terraform/terraform.go @@ -101,11 +101,23 @@ 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 + } + 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)) @@ -123,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 (