Skip to content

Commit

Permalink
Use terraform-exec to manage Terraform executable (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison authored Dec 14, 2020
1 parent 7a58454 commit e32ee66
Show file tree
Hide file tree
Showing 1,046 changed files with 322,444 additions and 116 deletions.
31 changes: 11 additions & 20 deletions cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cli

import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -299,23 +298,8 @@ func allFiles(fs afero.Fs, path string, pattern string) ([]string, error) {
}

func versionCmd(cmd *cobra.Command, args []string) {
log := common.CreateLogger(cmd.ErrOrStderr())

// nolint errcheck
fmt.Println("terrafmt v" + version.Version + "-" + version.GitCommit)

stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
tfCmd := exec.Command("terraform", "version")
tfCmd.Stdout = stdout
tfCmd.Stderr = stderr
if err := tfCmd.Run(); err != nil {
log.Warnf("Error running terraform: %s", err)
return
}
terraformVersion := strings.SplitN(stdout.String(), "\n", 2)[0]
// nolint errcheck
fmt.Println(" + " + terraformVersion)
}

type textBlockWriter struct {
Expand Down Expand Up @@ -534,6 +518,13 @@ func formatFile(fs afero.Fs, log *logrus.Logger, filename string, fmtverbs, fixF
}

func upgrade012File(fs afero.Fs, log *logrus.Logger, filename string, fmtverbs, verbose bool, stdin io.Reader, stdout, stderr io.Writer) (*blocks.Reader, error) {
ctx := context.Background()

tfBin, err := upgrade012.InstallTerraform(ctx)
if err != nil {
return nil, err
}

blocksFormatted := 0
br := blocks.Reader{
Log: log,
Expand All @@ -542,9 +533,9 @@ func upgrade012File(fs afero.Fs, log *logrus.Logger, filename string, fmtverbs,
var fb string
var err error
if fmtverbs {
fb, err = upgrade012.Upgrade12VerbBlock(log, b)
fb, err = upgrade012.Upgrade12VerbBlock(ctx, tfBin, log, b)
} else {
fb, err = upgrade012.Block(log, b)
fb, err = upgrade012.Block(ctx, tfBin, log, b)
}

if err != nil {
Expand All @@ -558,7 +549,7 @@ func upgrade012File(fs afero.Fs, log *logrus.Logger, filename string, fmtverbs,
return nil
},
}
err := br.DoTheThing(fs, filename, stdin, stdout)
err = br.DoTheThing(fs, filename, stdin, stdout)
if err != nil {
return &br, err
}
Expand Down
11 changes: 5 additions & 6 deletions cli/upgrade012_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var upgradeTestcases = []struct {
noDiff: true,
fmtcompat: false,
errMsg: []string{
"block 1 @ %s:8 failed to process with: cmd.Run() failed in terraform init with exit status 1:",
"block 3 @ %s:30 failed to process with: cmd.Run() failed in terraform init with exit status 1:",
"block 1 @ %s:8 failed to process with: terraform init failed:",
"block 3 @ %s:30 failed to process with: terraform init failed:",
},
lineCount: 41,
totalBlockCount: 3,
Expand All @@ -64,7 +64,7 @@ var upgradeTestcases = []struct {
sourcefile: "testdata/bad_terraform.go",
resultfile: "testdata/bad_terraform_upgrade012.go",
errMsg: []string{
"block 2 @ %s:16 failed to process with: cmd.Run() failed in terraform init with exit status 1:",
"block 2 @ %s:16 failed to process with: terraform init failed:",
},
errorBlockCount: 1,
lineCount: 20,
Expand All @@ -76,7 +76,7 @@ var upgradeTestcases = []struct {
sourcefile: "testdata/unsupported_fmt.go",
noDiff: true,
errMsg: []string{
"block 1 @ %s:8 failed to process with: cmd.Run() failed in terraform init with exit status 1:",
"block 1 @ %s:8 failed to process with: terraform init failed:",
},
errorBlockCount: 1,
lineCount: 21,
Expand All @@ -88,8 +88,7 @@ var upgradeTestcases = []struct {
noDiff: true,
fmtcompat: true,
errMsg: []string{
// "block 1 @ %s:8 failed to process with: cmd.Run() failed in terraform init with exit status 1:",
"block 1 @ %s:8 failed to process with: cmd.Run() failed in terraform 0.12upgrade",
"block 1 @ %s:8 failed to process with: terraform 0.12upgrade failed:",
},
errorBlockCount: 1,
lineCount: 21,
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/google/go-cmp v0.5.1 // indirect
github.com/gookit/color v1.2.6
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/terraform-exec v0.11.1-0.20201127183041-b76393f0a6fb
github.com/kylelemons/godebug v1.1.0
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/spf13/afero v1.3.2
github.com/spf13/afero v1.4.1
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
github.com/zclconf/go-cty v1.5.1 // indirect
golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666 // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/ini.v1 v1.57.0 // indirect
gopkg.in/yaml.v2 v2.3.0
)
Loading

0 comments on commit e32ee66

Please sign in to comment.