Skip to content

Commit

Permalink
Include the terraform version in the output of the version comma… (#8)
Browse files Browse the repository at this point in the history
Include the terraform version in the output of the version command, since terrafmt calls out to terraform. Different versions of terraform could have different formatting versions.
  • Loading branch information
gdavison authored and katbyte committed Jan 25, 2020
1 parent 91f419e commit 3b1f133
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cli

import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"

"github.com/andreyvit/diff"
Expand Down Expand Up @@ -273,10 +275,7 @@ func Make() *cobra.Command {
Use: "version",
Short: "Print the version number of terrafmt",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
//nolint errcheck
fmt.Println("terrafmt v" + version.Version + "-" + version.GitCommit)
},
Run: versionCmd,
})

pflags := root.PersistentFlags()
Expand All @@ -302,3 +301,21 @@ func Make() *cobra.Command {

return root
}

func versionCmd(cmd *cobra.Command, args []string) {
// 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 {
common.Log.Warnf("Error running terraform: %s", err)
return
}
terraformVersion := strings.SplitN(stdout.String(), "\n", 2)[0]
// nolint errcheck
fmt.Println(" + " + terraformVersion)
}

0 comments on commit 3b1f133

Please sign in to comment.