Skip to content

Commit

Permalink
Merge pull request #40 from DailenG/main
Browse files Browse the repository at this point in the history
Added support for removing source based on URI
  • Loading branch information
regg00 authored Jun 26, 2024
2 parents 9615951 + 6fa44a6 commit 3523df4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ChocoMan.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\ChocoMan.psm1'

# Version number of this module.
ModuleVersion = '1.2.5'
ModuleVersion = '1.2.6'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
66 changes: 48 additions & 18 deletions Public/Remove/Remove-ChocoSource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ Function Remove-ChocoSource {
Removes a chocolatey source.
.DESCRIPTION
Removes a chocolatey source.
.PARAMETER Name
The name of the source.
.PARAMETER Source
The name or uri of the source to remove.
.EXAMPLE
Remove-ChocoSource -Name test
Remove-ChocoSource -Source test
Name Status
---- ------
test Removed
.EXAMPLE
Remove-ChocoSource -Source https://chocolatey.org/api/v2
Name Status
---- ------
test Removed
Expand All @@ -19,48 +23,74 @@ Function Remove-ChocoSource {
[CmdletBinding(SupportsShouldProcess)]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory = $true)]
[String] $Name

[Parameter(Mandatory = $true, Position = 0)]
[String] $Source
)
begin {

if ((Test-ChocoInstalled) -And (Confirm-IsAdmin)) {

[String[]]$Arguments = "source", "remove", "-n=$Name"
if($Source -like "http*") {
$Server = $Source
} else {
$Name = $Source
}

[String[]]$Arguments = "source", "remove"

if($Name) {
$target = $Name
$targetType = "Name"
} else {
$target = $Server
$targetType = "Server"
}
}
}
process {
try {

if ($PSCmdlet.ShouldProcess($Name, "Remove-ChocoSource")) {
if ($PSCmdlet.ShouldProcess($target, "Remove-ChocoSource")) {

# Determine the target for removal based on provided parameters
if ($Name) {
$Server = (Get-ChocoSource | Where-Object Name -eq $Name).Name
} elseif ($Server) {
$Name = (Get-ChocoSource | Where-Object Uri -eq $Server).Name
}

if ($Name -and $Server) {
$Arguments += "-n=$Name"
$Arguments += "-s=$Server"

if ((Get-ChocoSource -Name test).Name) {
$CommandOutput = Invoke-ChocoCommand $Arguments

if ($CommandOutput.RawOutput -like "Removed $Name") {
Return [PSCustomObject]@{
Name = $Name
Target = $Target
TargetType = $targetType
Status = "Removed"
}
}
}
else {
Return [PSCustomObject]@{
Name = $Name
Target = $Target
TargetType = $targetType
Status = "Something went wrong"
}
}
}

}
else {
Return [PSCustomObject]@{
Name = $Name
Target = $target
TargetType = $targetType
Status = "Source does not exist"
}
}
}


}
}

if ($WhatIfPreference) {
$CommandOutput = Invoke-ChocoCommand ($Arguments + "--whatif")
Expand Down

0 comments on commit 3523df4

Please sign in to comment.