From 3b1f133dbb6cb4a7b81eab32181bb3f2298425cb Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Sat, 25 Jan 2020 08:31:10 -0800 Subject: [PATCH] =?UTF-8?q?Include=20the=20`terraform`=20version=20in=20th?= =?UTF-8?q?e=20output=20of=20the=20version=20comma=E2=80=A6=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- cli/cmds.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cli/cmds.go b/cli/cmds.go index cd7900a9..41fb5093 100644 --- a/cli/cmds.go +++ b/cli/cmds.go @@ -2,8 +2,10 @@ package cli import ( "bufio" + "bytes" "fmt" "os" + "os/exec" "strings" "github.com/andreyvit/diff" @@ -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() @@ -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) +}