diff --git a/PS-GiteaUtilities/PS-GiteaUtilities.psm1 b/PS-GiteaUtilities/PS-GiteaUtilities.psm1 index 8226903..6747365 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." @@ -275,6 +305,103 @@ Function Get-GiteaLFSConfiguration { } } +function Get-GiteaLFSFile { + [CmdletBinding()] + param( + [string]$giteaURL, + [Parameter(ValueFromPipelineByPropertyName)] + [string]$repoOwner, + [Parameter(ValueFromPipelineByPropertyName)] + [string]$repoName, + [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] + [Alias('FullName')] + [string[]]$Path, + [Parameter(ValueFromPipelineByPropertyName)] + [string]$branch = "main", + [string]$token + ) + + begin { + # Initialize results array + $results = @() + + # Use configuration if parameters aren't provided + if (-not $PSBoundParameters.ContainsKey('giteaURL') -or + -not $PSBoundParameters.ContainsKey('repoOwner') -or + -not $PSBoundParameters.ContainsKey('repoName') -or + -not $PSBoundParameters.ContainsKey('branch') -or + -not $PSBoundParameters.ContainsKey('token')) { + + $config = Get-GiteaConfiguration + if ($config) { + if (-not $PSBoundParameters.ContainsKey('giteaURL')) { $giteaURL = $config.giteaURL } + if (-not $PSBoundParameters.ContainsKey('repoOwner')) { $repoOwner = $config.defaultOwner } + if (-not $PSBoundParameters.ContainsKey('repoName')) { $repoName = $config.defaultRepo } + if (-not $PSBoundParameters.ContainsKey('branch')) { $branch = $config.defaultBranch } + if (-not $PSBoundParameters.ContainsKey('token')) { $token = $config.token } + } + } + + # Validate that we have all required parameters + $missingParams = @() + if (-not $giteaURL) { $missingParams += "giteaURL" } + if (-not $repoOwner) { $missingParams += "repoOwner" } + if (-not $repoName) { $missingParams += "repoName" } + if (-not $token) { $missingParams += "token" } + + if ($missingParams.Count -gt 0) { + throw "Missing required parameters: $($missingParams -join ', '). Either provide them directly or set them with Set-GiteaConfiguration." + } + + Write-Verbose "Parameters:" + Write-Verbose "giteaURL: $giteaURL" + Write-Verbose "repoOwner: $repoOwner" + Write-Verbose "repoName: $repoName" + Write-Verbose "Path: $Path" + Write-Verbose "token: $token" + + $headers = @{ + "Authorization" = "token $token" + "Accept" = "application/json" + } + } + + process { + $paths = $path + foreach ($path in $paths) { + Write-Verbose "Processing path: $path" + # Normalize the path format - replace backslashes with forward slashes and trim trailing slashes + $normalizedPath = $path -replace '\\', '/' -replace '/$', '' + $encodedPath = [System.Uri]::EscapeDataString($normalizedPath) + Write-Verbose "Normalized path: $normalizedPath" + Write-Verbose "Encoded path: $encodedPath" + $url = "$giteaURL" + $url += "/api/v1/repos" + $url += "/$repoOwner" + $url += "/$repoName" + $url += "/media" + # Only add the path component if it's not empty + if (-not [string]::IsNullOrWhiteSpace($normalizedPath)) { + $url += "/$encodedPath" + } + $url += "?ref=$branch" + Write-Verbose "URL: $url" + + try { + $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ResponseHeadersVariable ResponseHeaders - + } + catch { + + } + } + } + + end { + return $ResponseHeaders + } + +} + Function Invoke-GiteaFileDownload { <# .SYNOPSIS @@ -823,4 +950,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