/
use.go
114 lines (95 loc) · 3.29 KB
/
use.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
package use
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/tfversion/tfversion/pkg/alias"
"github.com/tfversion/tfversion/pkg/download"
"github.com/tfversion/tfversion/pkg/helpers"
"github.com/tfversion/tfversion/pkg/list"
)
// UseVersion activates the specified Terraform version or one of the latest versions
func UseVersion(versionOrAlias string) {
// find the version (via alias or directly)
var version string
if alias.IsAlias(versionOrAlias) {
version = alias.GetVersion(versionOrAlias)
} else {
version = versionOrAlias
}
// check if the version is installed
if !download.IsAlreadyDownloaded(version) {
err := fmt.Errorf("terraform version %s not found, run %s to install", helpers.ColoredVersion(version), helpers.ColoredInstallHelper(version))
helpers.ExitWithError("using", err)
}
useLocation := getUseLocation()
// inform the user that they need to update their PATH
path := os.Getenv("PATH")
if !strings.Contains(path, useLocation) {
fmt.Printf("%s not found in your shell PATH\n", color.CyanString(useLocation))
fmt.Printf("Please run %s to make this version available in your shell\n", color.CyanString("`export PATH=%s:$PATH`", useLocation))
fmt.Printf("Additionally, consider adding this line to your shell profile (e.g., .bashrc, .zshrc or fish config) for persistence.\n")
os.Exit(1)
}
// ensure the symlink target is available
binaryTargetPath := filepath.Join(useLocation, download.TerraformBinaryName)
_, err := os.Lstat(binaryTargetPath)
if err == nil {
err = os.Remove(binaryTargetPath)
if err != nil {
helpers.ExitWithError("removing symlink", err)
}
}
// create the symlink
binaryVersionPath := download.GetBinaryLocation(version)
err = os.Symlink(binaryVersionPath, binaryTargetPath)
if err != nil {
helpers.ExitWithError("creating symlink", err)
}
fmt.Printf("Activated Terraform version %s\n", helpers.ColoredVersion(version))
}
// UseLatestVersion activates the latest Terraform version
func UseLatestVersion(preRelease bool) {
version := list.FindLatestVersion(preRelease)
UseVersion(version)
}
// UseRequiredVersion activates the required Terraform version from the .tf files in the current directory
func UseRequiredVersion() {
terraformFiles, err := helpers.FindTerraformFiles()
if err != nil {
helpers.ExitWithError("finding Terraform files", err)
}
var foundVersion string
availableVersions := list.GetAvailableVersions()
for _, file := range terraformFiles {
requiredVersion, err := helpers.FindRequiredVersionInFile(file, availableVersions)
if err != nil {
helpers.ExitWithError("finding required version", err)
}
if requiredVersion != "" {
foundVersion = requiredVersion
break
}
}
if len(foundVersion) == 0 {
err := fmt.Errorf("no required version found in current directory")
helpers.ExitWithError("installing required version", err)
}
UseVersion(foundVersion)
}
func getUseLocation() string {
user, err := os.UserHomeDir()
if err != nil {
helpers.ExitWithError("user home directory", err)
}
useLocation := filepath.Join(user, download.ApplicationDir, download.UseDir)
if _, err := os.Stat(useLocation); os.IsNotExist(err) {
err := os.MkdirAll(useLocation, 0755)
if err != nil {
helpers.ExitWithError("creating use directory", err)
}
}
return useLocation
}