kontenhumas-be/add_clientid_extraction.ps1

67 lines
2.1 KiB
PowerShell

# PowerShell script to add clientId extraction logic to service methods
# Usage: .\add_clientid_extraction.ps1 <file_path>
param(
[Parameter(Mandatory=$true)]
[string]$FilePath
)
if (-not (Test-Path $FilePath)) {
Write-Error "File not found: $FilePath"
exit 1
}
Write-Host "Adding clientId extraction logic to: $FilePath"
# Read file content
$Content = Get-Content $FilePath -Raw
# Pattern to find method definitions that need clientId extraction
$MethodPattern = 'func \(_i \*(\w+)Service\) (\w+)\(authToken string, ([^)]+)\)'
# Find all method matches
$Matches = [regex]::Matches($Content, $MethodPattern)
foreach ($Match in $Matches) {
$ServiceName = $Match.Groups[1].Value
$MethodName = $Match.Groups[2].Value
$Parameters = $Match.Groups[3].Value
Write-Host "Processing method: $MethodName"
# Skip methods that already have clientId extraction logic
if ($Content -match "func \(_i \*${ServiceName}Service\) ${MethodName}\(authToken string, ${Parameters}\) \{[^}]*Extract clientId from authToken") {
Write-Host " Skipping $MethodName - already has clientId extraction"
continue
}
# Create the clientId extraction logic
$ExtractionLogic = @"
// Extract clientId from authToken
var clientId *uuid.UUID
if authToken != "" {
user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepository, authToken)
if user != nil && user.ClientId != nil {
clientId = user.ClientId
_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
}
}
if clientId == nil {
return nil, errors.New("clientId not found in auth token")
}
"@
# Replace the method opening with extraction logic
$OldMethodStart = "func (_i *${ServiceName}Service) ${MethodName}(authToken string, ${Parameters}) {"
$NewMethodStart = "func (_i *${ServiceName}Service) ${MethodName}(authToken string, ${Parameters}) {${ExtractionLogic}"
$Content = $Content -replace [regex]::Escape($OldMethodStart), $NewMethodStart
}
# Write updated content
Set-Content $FilePath $Content -NoNewline
Write-Host "ClientId extraction logic added to service methods."