import * as datasourceGithubTags from '../../datasource/github-tags' ; import { logger } from '../../logger' ; import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { TerraformDependencyTypes } from './common' ; import type { ExtractionResult } from './types' ; import { keyValueExtractionRegex } from './util' ; export function extractTerraformRequiredVersion ( startingLine , lines ) { const deps = [ ] ; let lineNumber = startingLine ; let braceCounter = 0 ; do { if ( lineNumber > lines . length - 1 ) { logger . debug ( `Malformed Terraform file detected.` ) ; } const line = lines [ lineNumber ] ; const openBrackets = ( line . match ( regEx ( /\{/g ) ) || [ ] ) . length ; const closedBrackets = ( line . match ( regEx ( /\}/g ) ) || [ ] ) . length ; braceCounter = braceCounter + openBrackets - closedBrackets ; const kvMatch = keyValueExtractionRegex . exec ( line ) ; if ( kvMatch && kvMatch . groups . key === 'required_version' ) { const dep = { currentValue : kvMatch . groups . value , lineNumber , managerData : { terraformDependencyType : TerraformDependencyTypes . terraform_version , } , } ; deps . push ( dep ) ; return { lineNumber : startingLine , dependencies : deps } ; } lineNumber += 1 ; } while ( braceCounter !== 0 ) ; return null ; } export function analyseTerraformVersion ( dep ) { dep . depType = 'required_version' ; dep . datasource = datasourceGithubTags . id ; dep . depName = 'hashicorp/terraform' ; dep . extractVersion = 'v(?<version>.*)$' ; }