2014-05-24 21:04:43 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-10-24 21:27:38 +02:00
|
|
|
"sort"
|
2014-05-24 21:04:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// VersionCommand is a Command implementation prints the version.
|
|
|
|
type VersionCommand struct {
|
2014-07-13 19:42:18 +02:00
|
|
|
Meta
|
|
|
|
|
2014-05-24 21:04:43 +02:00
|
|
|
Revision string
|
|
|
|
Version string
|
|
|
|
VersionPrerelease string
|
2014-10-13 23:05:29 +02: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 21:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionCommand) Help() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:42:18 +02:00
|
|
|
func (c *VersionCommand) Run(args []string) int {
|
2014-05-24 21:04:43 +02:00
|
|
|
var versionString bytes.Buffer
|
2020-04-01 21:01:08 +02:00
|
|
|
args = c.Meta.process(args)
|
2014-05-24 21:04:43 +02:00
|
|
|
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
|
|
|
if c.VersionPrerelease != "" {
|
2015-02-07 16:55:59 +01:00
|
|
|
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
|
2014-05-24 21:04:43 +02:00
|
|
|
|
|
|
|
if c.Revision != "" {
|
|
|
|
fmt.Fprintf(&versionString, " (%s)", c.Revision)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(versionString.String())
|
2014-10-13 23:05:29 +02:00
|
|
|
|
2017-10-24 21:27:38 +02:00
|
|
|
// We'll also attempt to print out the selected plugin versions. We can
|
|
|
|
// do this only if "terraform init" was already run and thus we've committed
|
|
|
|
// to a specific set of plugins. If not, the plugins lock will be empty
|
|
|
|
// and so we'll show _no_ providers.
|
|
|
|
//
|
|
|
|
// Generally-speaking this is a best-effort thing that will give us a good
|
|
|
|
// result in the usual case where the user successfully ran "terraform init"
|
|
|
|
// and then hit a problem running _another_ command.
|
2020-03-31 00:30:56 +02:00
|
|
|
providerInstaller := c.providerInstaller()
|
|
|
|
providerSelections, err := providerInstaller.SelectedPackages()
|
2017-10-24 21:27:38 +02:00
|
|
|
var pluginVersions []string
|
2020-03-31 00:30:56 +02:00
|
|
|
if err != nil {
|
|
|
|
// we'll just ignore it and show no plugins at all, then.
|
|
|
|
providerSelections = nil
|
|
|
|
}
|
|
|
|
for providerAddr, cached := range providerSelections {
|
|
|
|
version := cached.Version.String()
|
|
|
|
if version == "0.0.0" {
|
|
|
|
pluginVersions = append(pluginVersions, fmt.Sprintf("+ provider %s (unversioned)", providerAddr))
|
2017-10-24 21:27:38 +02:00
|
|
|
} else {
|
2020-03-31 00:30:56 +02:00
|
|
|
pluginVersions = append(pluginVersions, fmt.Sprintf("+ provider %s v%s", providerAddr, version))
|
2017-10-24 21:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(pluginVersions) != 0 {
|
|
|
|
sort.Strings(pluginVersions)
|
|
|
|
for _, str := range pluginVersions {
|
|
|
|
c.Ui.Output(str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 23:05:29 +02:00
|
|
|
// If we have a version check function, then let's check for
|
|
|
|
// the latest version as well.
|
|
|
|
if c.CheckFunc != nil {
|
|
|
|
|
|
|
|
// Check the latest version
|
|
|
|
info, err := c.CheckFunc()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2019-07-06 15:18:13 +02:00
|
|
|
"\nError checking latest version: %s", err))
|
2014-10-13 23:05:29 +02:00
|
|
|
}
|
|
|
|
if info.Outdated {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2019-07-06 15:18:13 +02:00
|
|
|
"\nYour version of Terraform is out of date! The latest version\n"+
|
2019-11-09 01:10:39 +01:00
|
|
|
"is %s. You can update by downloading from https://www.terraform.io/downloads.html",
|
2014-10-13 23:05:29 +02:00
|
|
|
info.Latest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 21:04:43 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VersionCommand) Synopsis() string {
|
|
|
|
return "Prints the Terraform version"
|
|
|
|
}
|