Skip to content
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

(GH-156) cChocoSource can now update source URL #157

Merged
merged 1 commit into from
May 16, 2022
Merged
Changes from all commits
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
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