Skip to content

Commit

Permalink
[chocolatey#156] cChocoSource can now update source URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bozho committed May 4, 2021
1 parent da95770 commit a38264f
Showing 1 changed file with 70 additions and 75 deletions.
145 changes: 70 additions & 75 deletions DSCResources/cChocoSource/cChocoSource.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,29 @@ function Set-TargetResource
)
Write-Verbose "Start Set-TargetResource"

if($Ensure -eq "Present")
{
if($Credentials -eq $null)
{
if($priority -eq $null)
{
choco sources add -n"$name" -s"$source"
}
else
{
choco sources add -n"$name" -s"$source" --priority=$priority
}
}
else
{
$username = $Credentials.UserName
$password = $Credentials.GetNetworkCredential().Password

if($priority -eq $null)
{
choco sources add -n"$name" -s"$source" -u="$username" -p="$password"
}
else
{
choco sources add -n"$name" -s"$source" -u="$username" -p="$password" --priority=$priority
}
}
}
else
{
choco sources remove -n"$name"
}
# Remove source if we'removing or updating it.
# If the source does not exists, this is a noop.
choco sources remove -n"$name"

if($Ensure -eq "Present")
{
$args = @("sources", "add", "-n`"$name`"", "-s`"$source`"")

if($null -ne $priority)
{
$args += "--priority=$priority"
}

if($null -ne $Credentials)
{
$username = $Credentials.UserName
$password = $Credentials.GetNetworkCredential().Password

$args += @("-u=`"$username`"", "-p=`"$password`"")
}

& choco $args
}
}

function Test-TargetResource
Expand Down Expand Up @@ -134,53 +125,57 @@ function Test-TargetResource

Write-Verbose "Start Test-TargetResource"

if($env:ChocolateyInstall -eq "" -or $env:ChocolateyInstall -eq $null)
{
$exe = (get-command choco).Source
$chocofolder = $exe.Substring(0,$exe.LastIndexOf("\"))

if( $chocofolder.EndsWith("bin") )
{
$chocofolder = $chocofolder.Substring(0,$chocofolder.LastIndexOf("\"))
}
}
else
{
$chocofolder = $env:ChocolateyInstall
}
$configfolder = "$chocofolder\config"
$configfile = Get-ChildItem $configfolder | Where-Object {$_.Name -match "chocolatey.config$"}

$xml = [xml](Get-Content $configfile.FullName)
$sources = $xml.chocolatey.sources.source

foreach($chocosource in $sources)
{
if($chocosource.id -eq $name -and $ensure -eq 'Present')
{
if ($chocosource.priority -eq $Priority)
if($env:ChocolateyInstall -eq "" -or $null -eq $env:ChocolateyInstall)
{
$exe = (get-command choco).Source
$chocofolder = $exe.Substring(0,$exe.LastIndexOf("\"))

if( $chocofolder.EndsWith("bin") )
{
$chocofolder = $chocofolder.Substring(0,$chocofolder.LastIndexOf("\"))
}
}
else
{
$chocofolder = $env:ChocolateyInstall
}
$configfolder = "$chocofolder\config"
$configfile = Get-ChildItem $configfolder | Where-Object {$_.Name -match "chocolatey.config$"}

$xml = [xml](Get-Content $configfile.FullName)
$sources = $xml.chocolatey.sources.source

foreach($chocosource in $sources)
{
if($chocosource.id -eq $name -and $ensure -eq 'Present')
{
$configMatches = $true
if ($chocosource.value -ne $Source)
{
return $true
$configMatches = $false
}
else

if ($chocosource.priority -ne $Priority)
{
return $false
$configMatches = $false
}
}
elseif($chocosource.id -eq $name -and $ensure -eq 'Absent')
{
return $false
}
}

if($Ensure -eq 'Present')
{
return $false
}
else
{
return $true
}

return $configMatches
}
elseif($chocosource.id -eq $name -and $ensure -eq 'Absent')
{
return $false
}
}

if($Ensure -eq 'Present')
{
return $false
}
else
{
return $true
}
}


Expand Down

0 comments on commit a38264f

Please sign in to comment.