@@ -46,7 +46,10 @@ var version = "0.5.0\n"
4646func main () {
4747
4848 dir := lib .GetCurrentDirectory ()
49+ homedir := lib .GetHomeDirectory ()
50+
4951 custBinPath := getopt .StringLong ("bin" , 'b' , lib .ConvertExecutableExt (defaultBin ), "Custom binary path. Ex: tgswitch -b " + lib .ConvertExecutableExt ("/Users/username/bin/terragrunt" ))
52+ installPath := getopt .StringLong ("install" , 'i' , homedir , "Custom install path. Ex: tfswitch -i /Users/username" )
5053 versionFlag := getopt .BoolLong ("version" , 'v' , "displays the version of tgswitch" )
5154 helpFlag := getopt .BoolLong ("help" , 'h' , "displays help message" )
5255 chDirPath := getopt .StringLong ("chdir" , 'c' , dir , "Switch to a different working directory before executing the given command. Ex: tgswitch --chdir terragrunt dir will run tgswitch in the directory" )
@@ -61,8 +64,6 @@ func main() {
6164 os .Exit (1 )
6265 }
6366
64- homedir := lib .GetHomeDirectory ()
65-
6667 TOMLConfigFile := filepath .Join (* chDirPath , tomlFilename ) //settings for .tgswitch.toml file in current directory (option to specify bin directory)
6768 HomeTOMLConfigFile := filepath .Join (homedir , tomlFilename ) //settings for .tgswitch.toml file in home directory (option to specify bin directory)
6869 RCFile := filepath .Join (* chDirPath , rcFilename ) //settings for .tgswitchrc file in current directory (backward compatible purpose)
@@ -84,9 +85,9 @@ func main() {
8485 version := ""
8586 binPath := * custBinPath
8687 if lib .FileExists (TOMLConfigFile ) { //read from toml from current directory
87- version , binPath = GetParamsTOML (binPath , * chDirPath )
88+ version , binPath , * installPath = GetParamsTOML (binPath , * installPath , * chDirPath )
8889 } else { // else read from toml from home directory
89- version , binPath = GetParamsTOML (binPath , homedir )
90+ version , binPath , * installPath = GetParamsTOML (binPath , * installPath , homedir )
9091 }
9192
9293 /* GIVEN A TOML FILE, */
@@ -99,7 +100,7 @@ func main() {
99100 exist := lib .VersionExist (requestedVersion , listOfVersions )
100101
101102 if exist {
102- installLocation := lib .Install (requestedVersion , binPath , terragruntURL )
103+ installLocation := lib .Install (requestedVersion , binPath , * installPath , terragruntURL )
103104 fmt .Println ("Install Location:" , installLocation )
104105 }
105106 } else {
@@ -110,26 +111,26 @@ func main() {
110111 case lib .FileExists (RCFile ) && len (args ) == 0 :
111112 lib .ReadingFileMsg (rcFilename )
112113 tgversion := lib .RetrieveFileContents (RCFile )
113- installVersion (tgversion , & binPath )
114+ installVersion (tgversion , & binPath , installPath )
114115 /* if .terragrunt-version file found (IN ADDITION TO A TOML FILE) */
115116 case lib .FileExists (TGVersionFile ) && len (args ) == 0 :
116117 lib .ReadingFileMsg (TGVersionFile )
117118 tgversion := lib .RetrieveFileContents (TGVersionFile )
118- installVersion (tgversion , & binPath )
119+ installVersion (tgversion , & binPath , installPath )
119120 /* if terragrunt.hcl file found (IN ADDITION TO A TOML FILE) */
120121 case lib .FileExists (TGHACLFile ) && checkVersionDefinedHCL (& TGHACLFile ) && len (args ) == 0 :
121- installTGHclFile (& TGHACLFile , binPath , proxyUrl )
122+ installTGHclFile (& TGHACLFile , binPath , * installPath , proxyUrl )
122123 /* if terragrunt Version environment variable is set (IN ADDITION TO A TOML FILE)*/
123124 case checkTGEnvExist () && len (args ) == 0 && version == "" :
124125 tgversion := os .Getenv ("TG_VERSION" )
125126 fmt .Printf ("Terragrunt version environment variable: %s\n " , tgversion )
126- installVersion (tgversion , & binPath )
127+ installVersion (tgversion , & binPath , installPath )
127128 /* if version is specified in the .toml file */
128129 case version != "" :
129- lib .Install (version , binPath , terragruntURL )
130+ lib .Install (version , binPath , * installPath , terragruntURL )
130131 /* show dropdown */
131132 default :
132- installFromList (& binPath )
133+ installFromList (& binPath , installPath )
133134 }
134135
135136 case len (args ) == 1 :
@@ -140,7 +141,7 @@ func main() {
140141 exist := lib .VersionExist (requestedVersion , listOfVersions )
141142
142143 if exist {
143- installLocation := lib .Install (requestedVersion , * custBinPath , terragruntURL )
144+ installLocation := lib .Install (requestedVersion , * custBinPath , * installPath , terragruntURL )
144145 fmt .Println ("Install Location:" , installLocation )
145146 }
146147 } else {
@@ -151,22 +152,22 @@ func main() {
151152 case lib .FileExists (RCFile ) && len (args ) == 0 :
152153 lib .ReadingFileMsg (rcFilename )
153154 tgversion := lib .RetrieveFileContents (RCFile )
154- installVersion (tgversion , custBinPath )
155+ installVersion (tgversion , custBinPath , installPath )
155156 case lib .FileExists (TGVersionFile ) && len (args ) == 0 :
156157 lib .ReadingFileMsg (TGVersionFile )
157158 tgversion := lib .RetrieveFileContents (TGVersionFile )
158- installVersion (tgversion , custBinPath )
159+ installVersion (tgversion , custBinPath , installPath )
159160 /* if terragrunt.hcl file found */
160161 case lib .FileExists (TGHACLFile ) && checkVersionDefinedHCL (& TGHACLFile ) && len (args ) == 0 :
161- installTGHclFile (& TGHACLFile , * custBinPath , proxyUrl )
162+ installTGHclFile (& TGHACLFile , * custBinPath , * installPath , proxyUrl )
162163 /* if terragrunt Version environment variable is set*/
163164 case checkTGEnvExist () && len (args ) == 0 :
164165 tgversion := os .Getenv ("TG_VERSION" )
165166 fmt .Printf ("Terragrunt version environment variable: %s\n " , tgversion )
166- installVersion (tgversion , custBinPath )
167+ installVersion (tgversion , custBinPath , installPath )
167168 /* show dropdown */
168169 default :
169- installFromList (custBinPath )
170+ installFromList (custBinPath , installPath )
170171 os .Exit (0 )
171172 }
172173
@@ -179,7 +180,7 @@ func usageMessage() {
179180}
180181
181182/* parses everything in the toml file, return required version and bin path */
182- func GetParamsTOML (binPath string , dir string ) (string , string ) {
183+ func GetParamsTOML (binPath string , installPath string , dir string ) (string , string , string ) {
183184 path := lib .GetHomeDirectory ()
184185 if dir == path {
185186 path = "home directory"
@@ -207,14 +208,19 @@ func GetParamsTOML(binPath string, dir string) (string, string) {
207208 version = ""
208209 }
209210
210- return version .(string ), binPath
211+ install := viper .Get ("install" ) //attempt to get the install path if it's provided in the toml
212+ if install != nil {
213+ installPath = os .ExpandEnv (install .(string ))
214+ }
215+
216+ return version .(string ), binPath , installPath
211217}
212218
213219/* installFromList : displays & installs tf version */
214- func installFromList (custBinPath * string ) {
220+ func installFromList (custBinPath * string , installPath * string ) {
215221
216222 listOfVersions := lib .GetAppList (proxyUrl )
217- recentVersions , _ := lib .GetRecentVersions () //get recent versions from RECENT file
223+ recentVersions , _ := lib .GetRecentVersions (* installPath ) //get recent versions from RECENT file
218224 listOfVersions = append (recentVersions , listOfVersions ... ) //append recent versions to the top of the list
219225 listOfVersions = lib .RemoveDuplicateVersions (listOfVersions ) //remove duplicate version
220226
@@ -230,23 +236,23 @@ func installFromList(custBinPath *string) {
230236 os .Exit (1 )
231237 }
232238
233- lib .Install (tgversion , * custBinPath , terragruntURL )
239+ lib .Install (tgversion , * custBinPath , * installPath , terragruntURL )
234240 os .Exit (0 )
235241}
236242
237243// install with provided version as argument
238- func installVersion (arg string , custBinPath * string ) {
244+ func installVersion (arg string , custBinPath * string , installPath * string ) {
239245 if lib .ValidVersionFormat (arg ) {
240246 requestedVersion := arg
241247
242248 //check to see if the requested version has been downloaded before
243- installLocation := lib .GetInstallLocation ()
249+ installLocation := lib .GetInstallLocation (* installPath )
244250 installFileVersionPath := lib .ConvertExecutableExt (filepath .Join (installLocation , versionPrefix + requestedVersion ))
245251 recentDownloadFile := lib .CheckFileExist (installFileVersionPath )
246252 if recentDownloadFile {
247253 lib .ChangeSymlink (installFileVersionPath , * custBinPath )
248254 fmt .Printf ("Switched terragrunt to version %q \n " , requestedVersion )
249- lib .AddRecent (requestedVersion ) //add to recent file for faster lookup
255+ lib .AddRecent (requestedVersion , * installPath ) //add to recent file for faster lookup
250256 os .Exit (0 )
251257 }
252258
@@ -257,7 +263,7 @@ func installVersion(arg string, custBinPath *string) {
257263 exist := lib .VersionExist (requestedVersion , listOfVersions )
258264
259265 if exist {
260- installLocation := lib .Install (requestedVersion , * custBinPath , terragruntURL )
266+ installLocation := lib .Install (requestedVersion , * custBinPath , * installPath , terragruntURL )
261267 fmt .Println ("Install Location:" , installLocation )
262268 }
263269
@@ -274,19 +280,19 @@ func checkTGEnvExist() bool {
274280}
275281
276282// install using a version constraint
277- func installFromConstraint (tgconstraint * string , custBinPath , mirrorURL string ) {
283+ func installFromConstraint (tgconstraint * string , custBinPath , installPath , mirrorURL string ) {
278284
279285 tgversion , err := lib .GetSemver (tgconstraint , mirrorURL )
280286 if err == nil {
281- lib .Install (tgversion , custBinPath , mirrorURL )
287+ lib .Install (tgversion , custBinPath , installPath , mirrorURL )
282288 }
283289 fmt .Println (err )
284290 fmt .Println ("No version found to match constraint. Follow the README.md instructions for setup. https://github.com/warrensbox/tgswitch/blob/master/README.md" )
285291 os .Exit (1 )
286292}
287293
288294// Install using version constraint from terragrunt file
289- func installTGHclFile (tgFile * string , custBinPath , mirrorURL string ) {
295+ func installTGHclFile (tgFile * string , custBinPath , installPath , mirrorURL string ) {
290296 fmt .Printf ("Terragrunt file found: %s\n " , * tgFile )
291297 parser := hclparse .NewParser ()
292298 file , diags := parser .ParseHCLFile (* tgFile ) //use hcl parser to parse HCL file
@@ -296,7 +302,7 @@ func installTGHclFile(tgFile *string, custBinPath, mirrorURL string) {
296302 }
297303 var version terragruntVersionConstraints
298304 gohcl .DecodeBody (file .Body , nil , & version )
299- installFromConstraint (& version .TerragruntVersionConstraint , custBinPath , mirrorURL )
305+ installFromConstraint (& version .TerragruntVersionConstraint , custBinPath , installPath , mirrorURL )
300306}
301307
302308// check if version is defined in hcl file /* lazy-emergency fix - will improve later */
@@ -312,7 +318,7 @@ func checkVersionDefinedHCL(tgFile *string) bool {
312318}
313319
314320// Install using version constraint from terragrunt file
315- // func installTGHclFile(tgFile *string, custBinPath *string) {
321+ // func installTGHclFile(tgFile *string, custBinPath *string, installPath *string ) {
316322// fmt.Printf("Terragrunt file found: %s\n", *tgFile)
317323// parser := hclparse.NewParser()
318324// file, diags := parser.ParseHCLFile(*tgFile) //use hcl parser to parse HCL file
@@ -323,7 +329,7 @@ func checkVersionDefinedHCL(tgFile *string) bool {
323329// var version terragruntVersionConstraints
324330// gohcl.DecodeBody(file.Body, nil, &version)
325331// //TODO figure out sermver
326- // //installFromConstraint(&version.TerragruntVersionConstraint, custBinPath)
332+ // //installFromConstraint(&version.TerragruntVersionConstraint, custBinPath, installPath )
327333// }
328334
329335type terragruntVersionConstraints struct {
0 commit comments