import is from '@sindresorhus/is' ; import { TerraformProviderDatasource } from '../../datasource/terraform-provider' ; import { logger } from '../../logger' ; import { SkipReason } from '../../types' ; import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { TerraformDependencyTypes } from './common' ; import type { ProviderLock } from './lockfile/types' ; import type { ExtractionResult } from './types' ; import { getLockedVersion , keyValueExtractionRegex , massageProviderLookupName , } from './util' ; export const sourceExtractionRegex = regEx ( /^(?:(?<hostname>(?:[a-zA-Z0-9]+\.+)+[a-zA-Z0-9]+)\/)?(?:(?<namespace>[^/]+)\/)?(?<type>[^/]+)/ ) ; export function extractTerraformProvider ( startingLine , lines , moduleName ) { let lineNumber = startingLine ; const deps = [ ] ; const dep = { managerData : { moduleName , terraformDependencyType : TerraformDependencyTypes . provider , } , } ; let braceCounter = 0 ; do { if ( lineNumber > lines . length - 1 ) { logger . debug ( `Malformed Terraform file detected.` ) ; } const line = lines [ lineNumber ] ; if ( is . string ( line ) ) { const openBrackets = ( line . match ( regEx ( /\{/g ) ) || [ ] ) . length ; const closedBrackets = ( line . match ( regEx ( /\}/g ) ) || [ ] ) . length ; braceCounter = braceCounter + openBrackets - closedBrackets ; if ( braceCounter === 1 ) { const kvMatch = keyValueExtractionRegex . exec ( line ) ; if ( kvMatch ) { if ( kvMatch . groups . key === 'version' ) { dep . currentValue = kvMatch . groups . value ; } else if ( kvMatch . groups . key === 'source' ) { dep . managerData . source = kvMatch . groups . value ; dep . managerData . sourceLine = lineNumber ; } } } } else { braceCounter = 0 ; } lineNumber += 1 ; } while ( braceCounter !== 0 ) ; deps . push ( dep ) ; lineNumber -= 1 ; return { lineNumber , dependencies : deps } ; } export function analyzeTerraformProvider ( dep , locks ) { dep . depType = 'provider' ; dep . depName = dep . managerData . moduleName ; dep . datasource = TerraformProviderDatasource . id ; if ( is . nonEmptyString ( dep . managerData . source ) ) { const source = sourceExtractionRegex . exec ( dep . managerData . source ) ; if ( ! source ) { dep . skipReason = SkipReason . UnsupportedUrl ; return ; } if ( source . groups . namespace === 'terraform-providers' ) { dep . registryUrls = [ `https://releases.hashicorp.com` ] ; } else if ( source . groups . hostname ) { dep . registryUrls = [ `https://${ source . groups . hostname }` ] ; dep . lookupName = `${ source . groups . namespace }/${ source . groups . type }` ; } else { dep . lookupName = dep . managerData . source ; } } massageProviderLookupName ( dep ) ; dep . lockedVersion = getLockedVersion ( dep , locks ) ; if ( ! dep . currentValue ) { dep . skipReason = SkipReason . NoVersion ; } }