diff --git a/PS-GiteaUtilities/PS-GiteaUtilities.psm1 b/PS-GiteaUtilities/PS-GiteaUtilities.psm1 index 8226903..1447807 100644 --- a/PS-GiteaUtilities/PS-GiteaUtilities.psm1 +++ b/PS-GiteaUtilities/PS-GiteaUtilities.psm1 @@ -31,12 +31,42 @@ Function Set-GiteaConfiguration { Function Get-GiteaConfiguration { [CmdletBinding()] - param() + param( + [switch]$LoadVariables, + [switch]$Force + ) $configPath = Join-Path -Path $env:USERPROFILE -ChildPath ".giteautils\config.xml" if (Test-Path -Path $configPath) { - return Import-Clixml -Path $configPath + $config = Import-Clixml -Path $configPath + + # If LoadVariables switch is used, set each config value as a variable in the global scope + if ($LoadVariables) { + foreach ($key in $config.Keys) { + # Check if variable exists in global scope + $variableExists = $false + try { + $existingVar = Get-Variable -Name $key -Scope Global -ErrorAction Stop + $variableExists = $true + } + catch { + $variableExists = $false + } + + # Set variable if it doesn't exist or if Force is used + if (-not $variableExists -or $Force) { + Write-Verbose "Loading configuration variable: $key = $($config[$key])" + Set-Variable -Name $key -Value $config[$key] -Scope Global + Write-Host "Created global variable: `$$key" -ForegroundColor Green + } + else { + Write-Verbose "Skipping existing variable: $key (use -Force to override)" + } + } + } + + return $config } else { Write-Warning "Gitea configuration not found. Use Set-GiteaConfiguration to set up." @@ -139,7 +169,7 @@ Function Get-GiteaFileContent { Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoName: $repoName" Write-Verbose "branch: $branch" - Write-Verbose "token: $token" + Write-Debug "Token: $token" Write-Verbose "decode: $decode" $headers = @{ @@ -249,7 +279,7 @@ Function Get-GiteaLFSConfiguration { Write-Verbose "giteaURL: $giteaURL" Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoName: $repoName" - Write-Verbose "token: $token" + Write-Debug "Token: $token" $filePath = ".gitattributes" @@ -359,7 +389,7 @@ Function Invoke-GiteaFileDownload { } Write-Verbose "Parameters:" - Write-Verbose "token: $token (length: $(if($token){$token.Length}else{0}))" + Write-Debug "Token: $token (length: $(if($token){$token.Length}else{0}))" Write-Verbose "PreserveRelativePath: $PreserveRelativePath" # Create a WebClient to be reused @@ -615,7 +645,7 @@ Function Get-GiteaChildItem { Write-Verbose "repoOwner: $repoOwner" Write-Verbose "repoName: $repoName" Write-Verbose "Path: $Path" - Write-Verbose "token: $token" + Write-Debug "Token: $token" Write-Verbose "File: $File" $headers = @{ @@ -668,21 +698,31 @@ Function Get-GiteaChildItem { } if ($isLFS) { $item.type = "lfs" + # Save the original download URL for LFS pointers to be used to populate the size and sha fields + $GitLFSPointerURL = $item.download_url + # Use the Gitea API to get the LFS pointer details + # For LFS files, we need to get the pointer file and parse it for details + $lfsPointerContent = Invoke-RestMethod -Uri $GitLFSPointerURL -Method Get -Headers $headers -ErrorAction Stop + Write-Debug "LFS Pointer Content: $($lfsPointerContent)" + + # Parse the LFS pointer to extract size and SHA + if ($lfsPointerContent -match 'oid sha256:([a-f0-9]+)') { + $item.sha = $matches[1] + } + if ($lfsPointerContent -match 'size (\d+)') { + $item.size = [long]$matches[1] + } + # Set the download URL to the media endpoint for LFS files to download the actual file $item.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($item.path)" } $itemObj = [PSCustomObject]@{ - filePath = $item.path - Path = $item.path - repoOwner = $repoOwner - repoName = $repoName - giteaURL = $giteaURL - downloadURL = $item.download_url - branch = $branch - type = $item.type name = $item.name + Path = $item.path + type = $item.type size = $item.size sha = $item.sha + downloadURL = $item.download_url Success = $true Error = $null Level = 0 @@ -741,21 +781,31 @@ Function Get-GiteaChildItem { } if ($isLFS) { $subItem.type = "lfs" + # Save the original download URL for LFS pointers to be used to populate the size and sha fields + $GitLFSPointerURL = $subItem.download_url + # Use the Gitea API to get the LFS pointer details + # For LFS files, we need to get the pointer file and parse it for details + $lfsPointerContent = Invoke-RestMethod -Uri $GitLFSPointerURL -Method Get -Headers $headers -ErrorAction Stop + Write-Debug "LFS Pointer Content: $($lfsPointerContent)" + + # Parse the LFS pointer to extract size and SHA + if ($lfsPointerContent -match 'oid sha256:([a-f0-9]+)') { + $subItem.sha = $matches[1] + } + if ($lfsPointerContent -match 'size (\d+)') { + $subItem.size = [long]$matches[1] + } + # Set the download URL to the media endpoint for LFS files to download the actual file $subItem.download_url = "$giteaURL/api/v1/repos/$repoOwner/$repoName/media/$($subItem.path)" } $subItemObj = [PSCustomObject]@{ - filePath = $subItem.path - Path = $subItem.path - repoOwner = $repoOwner - repoName = $repoName - giteaURL = $giteaURL - downloadURL = $subItem.download_url - branch = $branch - type = $subItem.type name = $subItem.name + Path = $subItem.path + type = $subItem.type size = $subItem.size sha = $subItem.sha + downloadURL = $subItem.download_url Success = $true Error = $null Level = $currentLevel @@ -793,7 +843,6 @@ Function Get-GiteaChildItem { Write-Error "Failed to retrieve '$normalizedPath' from Gitea: $($_.Exception.Message)`n$($errorDetails | ConvertTo-Json -Depth 1 -Compress)" $results += [PSCustomObject]@{ - filePath = $normalizedPath Path = $normalizedPath repoOwner = $repoOwner repoName = $repoName @@ -823,4 +872,4 @@ Function Get-GiteaChildItem { } } -Export-ModuleMember -Function Set-GiteaConfiguration, Get-GiteaConfiguration, Get-GiteaFileContent, Invoke-GiteaFileDownload, Get-GiteaChildItem \ No newline at end of file +Export-ModuleMember -Function Set-GiteaConfiguration, Get-GiteaConfiguration, Get-GiteaFileContent, Invoke-GiteaFileDownload, Get-GiteaChildItem, Get-GiteaLFSFile \ No newline at end of file