2014-05-24 14:04:43 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VersionCommand is a Command implementation prints the version.
|
|
|
|
type VersionCommand struct {
|
2014-07-13 12:42:18 -05:00
|
|
|
Meta
|
|
|
|
|
2014-05-24 14:04:43 -05:00
|
|
|
Revision string
|
|
|
|
Version string
|
|
|
|
VersionPrerelease string
|
2014-10-13 16:05:29 -05:00
|
|
|
CheckFunc VersionCheckFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionCheckFunc is the callback called by the Version command to
|
|
|
|
// check if there is a new version of Terraform.
|
|
|
|
type VersionCheckFunc func() (VersionCheckInfo, error)
|
|
|
|
|
|
|
|
// VersionCheckInfo is the return value for the VersionCheckFunc callback
|
|
|
|
// and tells the Version command information about the latest version
|
|
|
|
// of Terraform.
|
|
|
|
type VersionCheckInfo struct {
|
|
|
|
Outdated bool
|
|
|
|
Latest string
|
|
|
|
Alerts []string
|
2014-05-24 14:04:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionCommand) Help() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-07-13 12:42:18 -05:00
|
|
|
func (c *VersionCommand) Run(args []string) int {
|
2014-05-24 14:04:43 -05:00
|
|
|
var versionString bytes.Buffer
|
2017-03-07 22:09:48 -06:00
|
|
|
args, err := c.Meta.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-13 12:42:18 -05:00
|
|
|
|
2014-05-24 14:04:43 -05:00
|
|
|
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
|
|
|
if c.VersionPrerelease != "" {
|
2015-02-07 09:55:59 -06:00
|
|
|
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
|
2014-05-24 14:04:43 -05:00
|
|
|
|
|
|
|
if c.Revision != "" {
|
|
|
|
fmt.Fprintf(&versionString, " (%s)", c.Revision)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(versionString.String())
|
2014-10-13 16:05:29 -05:00
|
|
|
|
|
|
|
// If we have a version check function, then let's check for
|
|
|
|
// the latest version as well.
|
|
|
|
if c.CheckFunc != nil {
|
|
|
|
// Separate the prior output with a newline
|
|
|
|
c.Ui.Output("")
|
|
|
|
|
|
|
|
// Check the latest version
|
|
|
|
info, err := c.CheckFunc()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error checking latest version: %s", err))
|
|
|
|
}
|
|
|
|
if info.Outdated {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"Your version of Terraform is out of date! The latest version\n"+
|
2017-09-15 13:54:44 -05:00
|
|
|
"is %s. You can update by downloading from www.terraform.io/downloads.html",
|
2014-10-13 16:05:29 -05:00
|
|
|
info.Latest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 14:04:43 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionCommand) Synopsis() string {
|
|
|
|
return "Prints the Terraform version"
|
|
|
|
}
|