@@ -40,6 +40,13 @@ export class TerraformFramework implements IFramework {
4040 return 'terraform' ;
4141 }
4242
43+ /**
44+ * Name of the framework in logs
45+ */
46+ protected get logName ( ) : string {
47+ return 'Terrform' ;
48+ }
49+
4350 /**
4451 * Can this class handle the current project
4552 * @returns
@@ -51,7 +58,7 @@ export class TerraformFramework implements IFramework {
5158
5259 if ( ! r ) {
5360 Logger . verbose (
54- `[Terraform ] This is not a Terraform project. There are no *.tf files in ${ path . resolve ( '.' ) } folder.` ,
61+ `[${ this . logName } ] This is not a Terraform or OpenTofu project. There are no *.tf files in ${ path . resolve ( '.' ) } folder.` ,
5562 ) ;
5663 }
5764
@@ -69,7 +76,7 @@ export class TerraformFramework implements IFramework {
6976 const lambdas = this . extractLambdaInfo ( state ) ;
7077
7178 Logger . verbose (
72- '[Terraform ] Found Lambdas:' ,
79+ `[ ${ this . logName } ] Found Lambdas:` ,
7380 JSON . stringify ( lambdas , null , 2 ) ,
7481 ) ;
7582
@@ -78,7 +85,7 @@ export class TerraformFramework implements IFramework {
7885 const tsOutDir = await this . getTsConfigOutDir ( ) ;
7986
8087 if ( tsOutDir ) {
81- Logger . verbose ( '[Terraform ] tsOutDir:' , tsOutDir ) ;
88+ Logger . verbose ( `[ ${ this . logName } ] tsOutDir:` , tsOutDir ) ;
8289 }
8390
8491 for ( const func of lambdas ) {
@@ -143,7 +150,7 @@ export class TerraformFramework implements IFramework {
143150 packageJsonPath,
144151 esBuildOptions : undefined ,
145152 metadata : {
146- framework : 'terraform' ,
153+ framework : this . name ,
147154 } ,
148155 } ;
149156
@@ -164,7 +171,7 @@ export class TerraformFramework implements IFramework {
164171 for ( const resource of resources ) {
165172 if ( resource . type === 'aws_lambda_function' ) {
166173 Logger . verbose (
167- '[Terraform ] Found Lambda:' ,
174+ `[ ${ this . logName } ] Found Lambda:` ,
168175 JSON . stringify ( resource , null , 2 ) ,
169176 ) ;
170177
@@ -235,42 +242,49 @@ export class TerraformFramework implements IFramework {
235242 return lambdas ;
236243 }
237244
245+ /**
246+ * Get Terraform state CI command
247+ */
248+ protected get stateCommand ( ) : string {
249+ return 'terraform show --json' ;
250+ }
251+
238252 protected async readTerraformState ( ) : Promise < TerraformResource [ ] > {
239253 // Is there a better way to get the Terraform state???
240254
241255 let output : any ;
242256
243257 // get state by running "terraform show --json" command
244258 try {
245- output = await execAsync ( 'terraform show --json' ) ;
259+ output = await execAsync ( this . stateCommand ) ;
246260 } catch ( error : any ) {
247261 throw new Error (
248- `Failed to get Terraform state from 'terraform show --json ' command: ${ error . message } ` ,
262+ `[ ${ this . logName } ] Failed to getstate from '${ this . stateCommand } ' command: ${ error . message } ` ,
249263 { cause : error } ,
250264 ) ;
251265 }
252266
253267 if ( output . stderr ) {
254268 throw new Error (
255- `Failed to get Terraform state from 'terraform show --json ' command: ${ output . stderr } ` ,
269+ `[ ${ this . logName } ] Failed to get state from '${ this . stateCommand } ' command: ${ output . stderr } ` ,
256270 ) ;
257271 }
258272
259273 if ( ! output . stdout ) {
260274 throw new Error (
261- " Failed to get Terraform state from 'terraform show --json ' command" ,
275+ `[ ${ this . logName } ] Failed to get state from '${ this . stateCommand } ' command` ,
262276 ) ;
263277 }
264278
265279 let jsonString : string | undefined = output . stdout ;
266280
267- Logger . verbose ( 'Terraform state:' , jsonString ) ;
281+ Logger . verbose ( `[ ${ this . logName } ] State:` , jsonString ) ;
268282
269283 jsonString = jsonString ?. split ( '\n' ) . find ( ( line ) => line . startsWith ( '{' ) ) ;
270284
271285 if ( ! jsonString ) {
272286 throw new Error (
273- ' Failed to get Terraform state. JSON string not found in the output.' ,
287+ `[ ${ this . logName } ] Failed to get state. JSON string not found in the output.` ,
274288 ) ;
275289 }
276290
@@ -288,10 +302,10 @@ export class TerraformFramework implements IFramework {
288302 return [ ...rootResources , ...childResources ] as TerraformResource [ ] ;
289303 } catch ( error : any ) {
290304 //save state to file
291- await fs . writeFile ( 'terraform -state.json' , jsonString ) ;
292- Logger . error ( ' Failed to parse Terraform state JSON:' , error ) ;
305+ await fs . writeFile ( ` ${ this . name } -state.json` , jsonString ) ;
306+ Logger . error ( `[ ${ this . logName } ] Failed to parse state JSON:` , error ) ;
293307 throw new Error (
294- `Failed to parse Terraform state JSON: ${ error . message } ` ,
308+ `Failed to parse ${ this . logName } state JSON: ${ error . message } ` ,
295309 { cause : error } ,
296310 ) ;
297311 }
@@ -314,11 +328,11 @@ export class TerraformFramework implements IFramework {
314328 }
315329 }
316330 if ( ! tsConfigPath ) {
317- Logger . verbose ( '[Terraform ] tsconfig.json not found' ) ;
331+ Logger . verbose ( `[ ${ this . logName } ] tsconfig.json not found` ) ;
318332 return undefined ;
319333 }
320334
321- Logger . verbose ( '[Terraform ] tsconfig.json found:' , tsConfigPath ) ;
335+ Logger . verbose ( `[ ${ this . logName } ] tsconfig.json found:` , tsConfigPath ) ;
322336 const configFile = ts . readConfigFile ( tsConfigPath , ts . sys . readFile ) ;
323337 const compilerOptions = ts . parseJsonConfigFileContent (
324338 configFile . config ,
0 commit comments