Skip to content

Add proxy support in kvm #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion kvm.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
param(
[parameter(Position=0)]
[string] $command,
[string] $proxy = "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to specify a default value here. Also, when checking you want to do

if (!$proxy) {
   $proxy = $env:http_proxy
}

[switch] $verbosity = $false,
[alias("g")][switch] $global = $false,
[alias("p")][switch] $persistent = $false,
Expand All @@ -26,12 +27,13 @@ K Runtime Environment Version Manager - Build 509

USAGE: kvm <command> [options]

kvm upgrade [-x86][-x64] [-svr50][-svrc50] [-g|-global]
kvm upgrade [-x86][-x64] [-svr50][-svrc50] [-g|-global] [-proxy <ADDRESS>]
install latest KRE from feed
set 'default' alias to installed version
add KRE bin to user PATH environment variable
-g|-global install to machine-wide location
-f|-force upgrade even if latest is already installed
-proxy <ADDRESS> use given address as proxy when accessing remote server

kvm install <semver>|<alias>|<nupkg> [-x86][-x64] [-svr50][-svrc50] [-g|-global]
install requested KRE from feed
Expand Down Expand Up @@ -124,6 +126,25 @@ function Kvm-Upgrade {
Kvm-Alias-Set "default" $version
}

function Add-Proxy-If-Specified {
param(
[System.Net.WebClient] $wc
)
if ([string]::IsNullOrEmpty($proxy)) {
$proxy = [Environment]::GetEnvironmentVariable("http_proxy")
}
if (-NOT [string]::IsNullOrEmpty($proxy)) {
$wp = New-Object System.Net.WebProxy($proxy)
$pb = New-Object UriBuilder($proxy)
if ([string]::IsNullOrEmpty($pb.UserName)) {
$wp.Credentials = [System.Net.CredentialCache]::DefaultCredentials
} else {
$wp.Credentials = New-Object System.Net.NetworkCredential($pb.UserName, $pb.Password)
}
$wc.Proxy = $wp
}
}

function Kvm-Find-Latest {
param(
[string] $platform,
Expand All @@ -135,6 +156,7 @@ param(

$wc = New-Object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c")
Add-Proxy-If-Specified([ref]$wc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super sure if you need the [ref] here. If it works without it, might be cleaner to remove it. Also in theory you could do
Add-Proxy-If-Specified $wc (without the round brackets) and it should work. Not particularly important though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested and seems I don't need [ref] here. They are removed in latest commit.
I keep the round brackets for consistency with all other invocations in this file. But thank you for the hint, it's good for people new to PowerShell script like me 😄

[xml]$xml = $wc.DownloadString($url)

$version = Select-Xml "//d:Version" -Namespace @{d='http://schemas.microsoft.com/ado/2007/08/dataservices'} $xml
Expand Down Expand Up @@ -181,6 +203,7 @@ param(

$wc = New-Object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c")
Add-Proxy-If-Specified([ref]$wc)
$wc.DownloadFile($url, $tempKreFile)

Do-Kvm-Unpack $tempKreFile $kreTempDownload
Expand Down