@@ -41,19 +41,22 @@ import (
4141)
4242
4343const (
44- hashiURL = "https://releases.hashicorp.com/terraform/"
45- defaultBin = "/usr/local/bin/terraform" //default bin installation dir
46- tfvFilename = ".terraform-version"
47- rcFilename = ".tfswitchrc"
48- tomlFilename = ".tfswitch.toml"
44+ hashiURL = "https://releases.hashicorp.com/terraform/"
45+ defaultBin = "/usr/local/bin/terraform" //default bin installation dir
46+ defaultLatest = ""
47+ tfvFilename = ".terraform-version"
48+ rcFilename = ".tfswitchrc"
49+ tomlFilename = ".tfswitch.toml"
4950)
5051
51- var version = "0.9 .0\n "
52+ var version = "0.10 .0\n "
5253
5354func main () {
54-
55- custBinPath := getopt .StringLong ("bin" , 'b' , defaultBin , "Custom binary path. For example: /Users/username/bin/terraform" )
55+ custBinPath := getopt .StringLong ("bin" , 'b' , defaultBin , "Custom binary path. Ex: /Users/username/bin/terraform" )
5656 listAllFlag := getopt .BoolLong ("list-all" , 'l' , "List all versions of terraform - including beta and rc" )
57+ latestPre := getopt .StringLong ("latest-pre" , 'p' , defaultLatest , "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)" )
58+ latestStable := getopt .StringLong ("latest-stable" , 's' , defaultLatest , "Latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest)" )
59+ latestFlag := getopt .BoolLong ("latest" , 'u' , "Get latest stable version" )
5760 versionFlag := getopt .BoolLong ("version" , 'v' , "Displays the version of tfswitch" )
5861 helpFlag := getopt .BoolLong ("help" , 'h' , "Displays help message" )
5962 _ = versionFlag
@@ -73,10 +76,10 @@ func main() {
7376 os .Exit (1 )
7477 }
7578
76- curr_tfvfile := dir + fmt .Sprintf ("/%s" , tfvFilename ) //settings for .terraform-version file in current directory (tfenv compatible)
77- curr_rcfile := dir + fmt .Sprintf ("/%s" , rcFilename ) //settings for .tfswitchrc file in current directory (backward compatible purpose)
78- curr_tomlconfigfile := dir + fmt .Sprintf ("/%s" , tomlFilename ) //settings for .tfswitch.toml file in current directory (option to specify bin directory)
79- home_tomlconfigfile := homedir + fmt .Sprintf ("/%s" , tomlFilename ) //settings for .tfswitch.toml file in home directory (option to specify bin directory)
79+ TFVersionFile := dir + fmt .Sprintf ("/%s" , tfvFilename ) //settings for .terraform-version file in current directory (tfenv compatible)
80+ RCFile := dir + fmt .Sprintf ("/%s" , rcFilename ) //settings for .tfswitchrc file in current directory (backward compatible purpose)
81+ TOMLConfigFile := dir + fmt .Sprintf ("/%s" , tomlFilename ) //settings for .tfswitch.toml file in current directory (option to specify bin directory)
82+ HomeTOMLConfigFile := homedir + fmt .Sprintf ("/%s" , tomlFilename ) //settings for .tfswitch.toml file in home directory (option to specify bin directory)
8083
8184 switch {
8285 case * versionFlag :
@@ -92,11 +95,11 @@ func main() {
9295 * If you provide a custom binary path with the -b option, this will override the bin value in the toml file
9396 * If you provide a version on the command line, this will override the version value in the toml file
9497 */
95- case fileExists (curr_tomlconfigfile ) || fileExists (home_tomlconfigfile ):
98+ case fileExists (TOMLConfigFile ) || fileExists (HomeTOMLConfigFile ):
9699
97100 version := ""
98101 binPath := * custBinPath
99- if fileExists (curr_tomlconfigfile ) { //read from toml from current directory
102+ if fileExists (TOMLConfigFile ) { //read from toml from current directory
100103 version , binPath = getParamsTOML (binPath , dir )
101104 } else { // else read from toml from home directory
102105 version , binPath = getParamsTOML (binPath , homedir )
@@ -106,15 +109,23 @@ func main() {
106109 case * listAllFlag :
107110 listAll := true //set list all true - all versions including beta and rc will be displayed
108111 installOption (listAll , & binPath )
112+ case * latestPre != "" :
113+ preRelease := true
114+ installLatestImplicitVersion (* latestPre , custBinPath , preRelease )
115+ case * latestStable != "" :
116+ preRelease := false
117+ installLatestImplicitVersion (* latestStable , custBinPath , preRelease )
118+ case * latestFlag :
119+ installLatestVersion (custBinPath )
109120 case len (args ) == 1 :
110121 installVersion (args [0 ], & binPath )
111- case fileExists (curr_rcfile ) && len (args ) == 0 :
122+ case fileExists (RCFile ) && len (args ) == 0 :
112123 readingFileMsg (rcFilename )
113- tfversion := retrieveFileContents (curr_rcfile )
124+ tfversion := retrieveFileContents (RCFile )
114125 installVersion (tfversion , & binPath )
115- case fileExists (curr_tfvfile ) && len (args ) == 0 :
126+ case fileExists (TFVersionFile ) && len (args ) == 0 :
116127 readingFileMsg (tfvFilename )
117- tfversion := retrieveFileContents (curr_tfvfile )
128+ tfversion := retrieveFileContents (TFVersionFile )
118129 installVersion (tfversion , & binPath )
119130 case checkTFModuleFileExist (dir ) && len (args ) == 0 :
120131 installTFProvidedModule (dir , & binPath )
@@ -125,24 +136,38 @@ func main() {
125136 installOption (listAll , & binPath )
126137 }
127138
128- /* list all versions, // show all terraform version including betas and RCs*/
139+ /* show all terraform version including betas and RCs*/
129140 case * listAllFlag :
130141 installWithListAll (custBinPath )
131142
143+ /* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
144+ case * latestPre != "" :
145+ preRelease := true
146+ installLatestImplicitVersion (* latestPre , custBinPath , preRelease )
147+
148+ /* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
149+ case * latestStable != "" :
150+ preRelease := false
151+ installLatestImplicitVersion (* latestStable , custBinPath , preRelease )
152+
153+ /* latest stable version */
154+ case * latestFlag :
155+ installLatestVersion (custBinPath )
156+
132157 /* version provided on command line as arg */
133158 case len (args ) == 1 :
134159 installVersion (args [0 ], custBinPath )
135160
136161 /* provide an tfswitchrc file */
137- case fileExists (curr_rcfile ) && len (args ) == 0 :
162+ case fileExists (RCFile ) && len (args ) == 0 :
138163 readingFileMsg (rcFilename )
139- tfversion := retrieveFileContents (curr_rcfile )
164+ tfversion := retrieveFileContents (RCFile )
140165 installVersion (tfversion , custBinPath )
141166
142167 /* if .terraform-version file found */
143- case fileExists (curr_tfvfile ) && len (args ) == 0 :
168+ case fileExists (TFVersionFile ) && len (args ) == 0 :
144169 readingFileMsg (tfvFilename )
145- tfversion := retrieveFileContents (curr_tfvfile )
170+ tfversion := retrieveFileContents (TFVersionFile )
146171 installVersion (tfversion , custBinPath )
147172
148173 /* if versions.tf file found */
@@ -164,6 +189,22 @@ func installWithListAll(custBinPath *string) {
164189 installOption (listAll , custBinPath )
165190}
166191
192+ // install latest stable tf version
193+ func installLatestVersion (custBinPath * string ) {
194+ tfversion , _ := lib .GetTFLatest (hashiURL )
195+ lib .Install (tfversion , * custBinPath )
196+ }
197+
198+ // install latest - argument (version) must be provided
199+ func installLatestImplicitVersion (requestedVersion string , custBinPath * string , preRelease bool ) {
200+ if lib .ValidMinorVersionFormat (requestedVersion ) {
201+ tfversion , _ := lib .GetTFLatestImplicit (hashiURL , preRelease , requestedVersion )
202+ lib .Install (tfversion , * custBinPath )
203+ } else {
204+ printInvalidMinorTFVersion ()
205+ }
206+ }
207+
167208// install with provided version as argument
168209func installVersion (arg string , custBinPath * string ) {
169210 if lib .ValidVersionFormat (arg ) {
@@ -187,7 +228,12 @@ func installVersion(arg string, custBinPath *string) {
187228
188229// Print invalid TF version
189230func printInvalidTFVersion () {
190- fmt .Println ("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions" )
231+ fmt .Println ("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # are numbers and @ are word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions" )
232+ }
233+
234+ // Print invalid TF version
235+ func printInvalidMinorTFVersion () {
236+ fmt .Println ("Invalid minor terraform version format. Format should be #.# where # are numbers. For example, 0.11 is valid version" )
191237}
192238
193239//retrive file content of regular file
@@ -295,7 +341,7 @@ func installOption(listAll bool, custBinPath *string) {
295341 os .Exit (0 )
296342}
297343
298- // installation when
344+ // install when tf file is provided
299345func installTFProvidedModule (dir string , custBinPath * string ) {
300346 tfversion := ""
301347 module , _ := tfconfig .LoadModule (dir )
0 commit comments