OneGet/Add-PackageSource.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

<#
choco source add -n=bob -s="https://somewhere/out/there/api/v2/"
choco source add -n=bob -s "'https://somewhere/out/there/api/v2/'" -cert=\Users\bob\bob.pfx
choco source add -n=bob -s "'https://somewhere/out/there/api/v2/'" -u=bob -p=12345
choco source disable -n=bob
choco source enable -n=bob
choco source remove -n=bob
#>


function Add-PackageSource {
    [CmdletBinding()]
    param (
        [string]
        $Name,

        [string]
        $Location,

        [bool]
        $Trusted
    )     

    Write-Debug ($LocalizedData.ProviderDebugMessage -f ' Add-PackageSource')

    if ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Name)) {
        Throw-Error -ExceptionName 'System.ArgumentException' `
                    -ExceptionMessage ($LocalizedData.PackageSourceNameContainsWildCards -f ($Name)) `
                    -ErrorId 'PackageSourceNameContainsWildCards' `
                    -CallerPSCmdlet $PSCmdlet `
                    -ErrorCategory InvalidArgument `
                    -ExceptionObject $Name
        return
    }

    $arguments = @(
        'add'
        "--name='$Name'"
        "--source='$Location'"
    )
    Invoke-Chocolatey -Command 'source' -Arguments $arguments -Force (Get-ForceOption)

    Write-Debug ($LocalizedData.PackageSourceRegistered -f ($Name, $Location))

    Write-Output -InputObject (New-PackageSource -Name $Name -Location $Location -Trusted $true -Registered $true)
}