1- import { CreatePlan , ExampleConfig , Resource , ResourceSettings , SpawnStatus , getPty } from '@codifycli/plugin-core' ;
1+ import {
2+ CreatePlan ,
3+ ExampleConfig ,
4+ Resource ,
5+ ResourceSettings ,
6+ SpawnStatus ,
7+ getPty ,
8+ Utils , FileUtils
9+ } from '@codifycli/plugin-core' ;
210import { OS , StringIndexedObject } from '@codifycli/schemas' ;
311import fs from 'node:fs/promises' ;
412import os from 'node:os' ;
513import path from 'node:path' ;
614import semver from 'semver' ;
715
8- import { FileUtils } from '../../utils/file-utils.js' ;
9- import { Utils } from '../../utils/index.js' ;
1016import Schema from './terraform-schema.json' ;
1117import { HashicorpReleaseInfo , HashicorpReleasesAPIResponse , TerraformVersionInfo } from './terraform-types.js' ;
18+ import { codifySpawn } from '../../utils/codify-spawn.js' ;
1219
1320const TERRAFORM_RELEASES_API_URL = 'https://api.releases.hashicorp.com/v1/releases/terraform' ;
1421const TERRAFORM_RELEASE_INFO_API_URL = ( version : string ) => `https://api.releases.hashicorp.com/v1/releases/terraform/${ version } ` ;
@@ -116,18 +123,24 @@ ${JSON.stringify(releaseInfo, null, 2)}
116123 // Create a temporary tmp dir
117124 const temporaryDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , 'terraform-' ) ) ;
118125
126+ // Ensure unzip is available (not installed by default on some Linux distros)
127+ const unzipCheck = await $ . spawnSafe ( 'which unzip' ) ;
128+ if ( unzipCheck . status === SpawnStatus . ERROR ) {
129+ await Utils . installViaPkgMgr ( 'unzip' ) ;
130+ }
131+
119132 // Download and unzip the terraform binary
120133 await $ . spawn ( `curl -fsSL ${ downloadUrl } -o terraform.zip` , { cwd : temporaryDir } ) ;
121134 await $ . spawn ( 'unzip -q terraform.zip' , { cwd : temporaryDir } ) ;
122135
123136 // Ensure that /usr/local/bin exists. If not then create it
124- await ( directory === '/usr/local/bin' ? Utils . createBinDirectoryIfNotExists ( ) : Utils . createDirectoryIfNotExists ( directory ) ) ;
137+ await ( directory === '/usr/local/bin' ? this . createBinDirectoryIfNotExists ( ) : this . createDirectoryIfNotExists ( directory ) ) ;
125138
126139 await $ . spawn ( `mv ./terraform ${ directory } ` , { cwd : temporaryDir , requiresRoot : true } )
127140 await $ . spawn ( `rm -rf ${ temporaryDir } ` )
128141
129142 if ( ! ( await Utils . isDirectoryOnPath ( directory ) ) ) {
130- await FileUtils . addToStartupFile ( `export PATH=$PATH:${ directory } ` ) ;
143+ await FileUtils . addToShellRc ( `export PATH=$PATH:${ directory } ` ) ;
131144 }
132145 }
133146
@@ -145,7 +158,7 @@ ${JSON.stringify(releaseInfo, null, 2)}
145158 }
146159
147160 await $ . spawn ( `rm ${ installLocationQuery . data } ` , { requiresRoot : true } ) ;
148- await FileUtils . removeLineFromStartupFile ( `export PATH=$PATH:${ installLocationQuery . data } ` ) ;
161+ await FileUtils . removeLineFromShellRc ( `export PATH=$PATH:${ installLocationQuery . data } ` ) ;
149162 }
150163
151164 private async getLatestTerraformInfo ( ) : Promise < HashicorpReleaseInfo > {
@@ -184,4 +197,38 @@ ${JSON.stringify(releaseInfo, null, 2)}
184197
185198 return build . url ;
186199 }
200+
201+ private async createBinDirectoryIfNotExists ( ) : Promise < void > {
202+ let lstat = null ;
203+ try {
204+ lstat = await fs . lstat ( '/usr/local/bin' )
205+ } catch { }
206+
207+ if ( lstat && lstat . isDirectory ( ) ) {
208+ return ;
209+ }
210+
211+ if ( lstat && ! lstat . isDirectory ( ) ) {
212+ throw new Error ( 'Found file at /usr/local/bin. Cannot create a directory there' )
213+ }
214+
215+ await codifySpawn ( 'sudo mkdir -p -m 775 /usr/local/bin' )
216+ }
217+
218+ async createDirectoryIfNotExists ( path : string ) : Promise < void > {
219+ let lstat = null ;
220+ try {
221+ lstat = await fs . lstat ( path )
222+ } catch { }
223+
224+ if ( lstat && lstat . isDirectory ( ) ) {
225+ return ;
226+ }
227+
228+ if ( lstat && ! lstat . isDirectory ( ) ) {
229+ throw new Error ( `Found file at ${ path } . Cannot create a directory there` )
230+ }
231+
232+ await fs . mkdir ( path , { recursive : true } )
233+ }
187234}
0 commit comments