forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
version.go
123 lines (107 loc) · 3.27 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package command
import (
"bytes"
"fmt"
"sort"
)
// VersionCommand is a Command implementation prints the version.
type VersionCommand struct {
Meta
Revision string
Version string
VersionPrerelease string
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
}
func (c *VersionCommand) Help() string {
return ""
}
func (c *VersionCommand) Run(args []string) int {
var versionString bytes.Buffer
args, err := c.Meta.process(args, false)
if err != nil {
return 1
}
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
if c.VersionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
if c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
}
}
c.Ui.Output(versionString.String())
// 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.
providerPlugins := c.providerPluginSet()
pluginsLockFile := c.providerPluginsLock()
pluginsLock := pluginsLockFile.Read()
var pluginVersions []string
for meta := range providerPlugins {
name := meta.Name
wantHash, wanted := pluginsLock[name]
if !wanted {
// Ignore providers that aren't used by the current config at all
continue
}
gotHash, err := meta.SHA256()
if err != nil {
// if we can't read the file to hash it, ignore it.
continue
}
if !bytes.Equal(gotHash, wantHash) {
// Not the plugin we've locked, so ignore it.
continue
}
// If we get here then we've found a selected plugin, so we'll print
// out its details.
if meta.Version == "0.0.0" {
pluginVersions = append(pluginVersions, fmt.Sprintf("+ provider.%s (unversioned)", name))
} else {
pluginVersions = append(pluginVersions, fmt.Sprintf("+ provider.%s v%s", name, meta.Version))
}
}
if len(pluginVersions) != 0 {
sort.Strings(pluginVersions)
for _, str := range pluginVersions {
c.Ui.Output(str)
}
}
// 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"+
"is %s. You can update by downloading from www.terraform.io/downloads.html",
info.Latest))
}
}
return 0
}
func (c *VersionCommand) Synopsis() string {
return "Prints the Terraform version"
}