Skip to content

Commit e453d2b

Browse files
Implement core Context integration functions
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
1 parent 78edce9 commit e453d2b

11 files changed

+703
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function Remove-PowerShellGalleryContext {
2+
<#
3+
.SYNOPSIS
4+
Remove a PowerShell Gallery context.
5+
6+
.DESCRIPTION
7+
Remove a PowerShell Gallery context from the vault.
8+
9+
.EXAMPLE
10+
Remove-PowerShellGalleryContext -ID 'MyAccount'
11+
12+
Removes the PowerShell Gallery context with ID 'MyAccount'.
13+
#>
14+
[OutputType([void])]
15+
[CmdletBinding(SupportsShouldProcess)]
16+
param(
17+
# The ID of the context to remove.
18+
[Parameter(Mandatory)]
19+
[string] $ID
20+
)
21+
22+
begin {
23+
Write-Debug '[Remove-PowerShellGalleryContext] - Start'
24+
$null = Get-PowerShellGalleryConfig
25+
}
26+
27+
process {
28+
if ($PSCmdlet.ShouldProcess("Context [$ID]", 'Remove')) {
29+
Write-Verbose "Removing context: [$ID]"
30+
Remove-Context -ID $ID -Vault $script:PowerShellGallery.ContextVault
31+
32+
# If this was the default context, clear the default
33+
if ($script:PowerShellGallery.Config.DefaultContext -eq $ID) {
34+
Write-Verbose 'Clearing default context'
35+
$script:PowerShellGallery.Config.DefaultContext = $null
36+
$null = Set-Context -ID $script:PowerShellGallery.DefaultConfig.ID -Context $script:PowerShellGallery.Config -Vault $script:PowerShellGallery.ContextVault
37+
}
38+
}
39+
}
40+
41+
end {
42+
Write-Debug '[Remove-PowerShellGalleryContext] - End'
43+
}
44+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function Set-PowerShellGalleryContext {
2+
<#
3+
.SYNOPSIS
4+
Sets the PowerShell Gallery context and stores it in the context vault.
5+
6+
.DESCRIPTION
7+
This function sets the PowerShell Gallery context and stores it in the context vault.
8+
The context is used to authenticate with the PowerShell Gallery API.
9+
10+
.EXAMPLE
11+
$context = @{
12+
ID = 'MyAccount'
13+
Name = 'MyAccount'
14+
ApiKey = $secureApiKey
15+
GalleryUrl = 'https://www.powershellgallery.com'
16+
ConnectedAt = Get-Date
17+
}
18+
Set-PowerShellGalleryContext -Context $context
19+
20+
Sets the PowerShell Gallery context with the specified settings as a hashtable.
21+
#>
22+
[OutputType([System.Object])]
23+
[CmdletBinding(SupportsShouldProcess)]
24+
param(
25+
# The ID of the context.
26+
[Parameter(Mandatory)]
27+
[string] $ID,
28+
29+
# The PowerShell Gallery context to save in the vault.
30+
[Parameter(Mandatory)]
31+
[hashtable] $Context,
32+
33+
# Set as the default context.
34+
[Parameter()]
35+
[switch] $Default,
36+
37+
# Pass the context through the pipeline.
38+
[Parameter()]
39+
[switch] $PassThru
40+
)
41+
42+
begin {
43+
Write-Debug '[Set-PowerShellGalleryContext] - Start'
44+
$null = Get-PowerShellGalleryConfig
45+
}
46+
47+
process {
48+
Write-Debug "Setting context: [$ID]"
49+
$contextObj = @{} + $Context
50+
$contextObj['ID'] = $ID
51+
52+
if ($PSCmdlet.ShouldProcess("Context [$ID]", 'Set')) {
53+
$result = Set-Context -ID $ID -Context $contextObj -Vault $script:PowerShellGallery.ContextVault -PassThru
54+
55+
if ($Default) {
56+
Write-Debug "Setting [$ID] as default context"
57+
$script:PowerShellGallery.Config.DefaultContext = $ID
58+
$null = Set-Context -ID $script:PowerShellGallery.DefaultConfig.ID -Context $script:PowerShellGallery.Config -Vault $script:PowerShellGallery.ContextVault
59+
}
60+
61+
if ($PassThru) {
62+
return $result
63+
}
64+
}
65+
}
66+
67+
end {
68+
Write-Debug '[Set-PowerShellGalleryContext] - End'
69+
}
70+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Get-PowerShellGalleryConfig {
2+
<#
3+
.SYNOPSIS
4+
Get the PowerShell Gallery configuration.
5+
6+
.DESCRIPTION
7+
Get the PowerShell Gallery configuration. Initializes if not already loaded.
8+
9+
.EXAMPLE
10+
Get-PowerShellGalleryConfig
11+
12+
Gets the PowerShell Gallery configuration.
13+
#>
14+
[OutputType([System.Object])]
15+
[CmdletBinding()]
16+
param()
17+
18+
begin {
19+
Write-Debug '[Get-PowerShellGalleryConfig] - Start'
20+
}
21+
22+
process {
23+
if ($null -eq $script:PowerShellGallery.Config) {
24+
Write-Debug 'Config not initialized, initializing...'
25+
Initialize-PowerShellGalleryConfig
26+
}
27+
return $script:PowerShellGallery.Config
28+
}
29+
30+
end {
31+
Write-Debug '[Get-PowerShellGalleryConfig] - End'
32+
}
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Initialize-PowerShellGalleryConfig {
2+
<#
3+
.SYNOPSIS
4+
Initialize the PowerShellGallery module configuration.
5+
6+
.DESCRIPTION
7+
Initialize the PowerShellGallery module configuration.
8+
9+
.EXAMPLE
10+
Initialize-PowerShellGalleryConfig
11+
12+
Initializes the PowerShellGallery module configuration.
13+
14+
.EXAMPLE
15+
Initialize-PowerShellGalleryConfig -Force
16+
17+
Forces the initialization of the PowerShellGallery module configuration.
18+
#>
19+
[OutputType([void])]
20+
[CmdletBinding()]
21+
param (
22+
# Force the initialization of the PowerShellGallery config.
23+
[switch] $Force
24+
)
25+
26+
begin {
27+
Write-Debug '[Initialize-PowerShellGalleryConfig] - Start'
28+
}
29+
30+
process {
31+
Write-Debug "Force: [$Force]"
32+
if ($Force) {
33+
Write-Debug 'Forcing initialization of PowerShellGalleryConfig.'
34+
$config = Set-Context -Context $script:PowerShellGallery.DefaultConfig -Vault $script:PowerShellGallery.ContextVault -PassThru
35+
$script:PowerShellGallery.Config = $config
36+
return
37+
}
38+
39+
if ($null -ne $script:PowerShellGallery.Config) {
40+
Write-Debug 'PowerShellGalleryConfig already initialized and available in memory.'
41+
return
42+
}
43+
44+
Write-Debug 'Attempt to load the stored PowerShellGalleryConfig from ContextVault'
45+
$config = Get-Context -ID $script:PowerShellGallery.DefaultConfig.ID -Vault $script:PowerShellGallery.ContextVault
46+
if ($config) {
47+
Write-Debug 'PowerShellGalleryConfig loaded into memory.'
48+
$script:PowerShellGallery.Config = $config
49+
return
50+
}
51+
52+
Write-Debug 'Initializing PowerShellGalleryConfig from defaults'
53+
$config = Set-Context -Context $script:PowerShellGallery.DefaultConfig -Vault $script:PowerShellGallery.ContextVault -PassThru
54+
$script:PowerShellGallery.Config = $config
55+
}
56+
57+
end {
58+
Write-Debug '[Initialize-PowerShellGalleryConfig] - End'
59+
}
60+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
function Connect-PowerShellGallery {
2+
<#
3+
.SYNOPSIS
4+
Connects to the PowerShell Gallery by storing an API key context.
5+
6+
.DESCRIPTION
7+
Connects to the PowerShell Gallery by storing an API key in a secure context vault.
8+
When called without an API key, it opens the user's browser to the PowerShell Gallery
9+
API key management page and prompts for the key.
10+
11+
.EXAMPLE
12+
Connect-PowerShellGallery -Name 'MyAccount'
13+
14+
Opens browser to API key page and prompts for API key, then stores it with name 'MyAccount'.
15+
16+
.EXAMPLE
17+
Connect-PowerShellGallery -Name 'CIAccount' -ApiKey $secureApiKey -Silent
18+
19+
Stores the provided API key without prompting or informational output.
20+
21+
.EXAMPLE
22+
$context = Connect-PowerShellGallery -Name 'MyAccount' -PassThru
23+
24+
Stores the API key and returns the context object.
25+
#>
26+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
27+
'PSAvoidUsingConvertToSecureStringWithPlainText', '',
28+
Justification = 'The API key is received as clear text from user input.'
29+
)]
30+
[CmdletBinding(DefaultParameterSetName = 'Interactive')]
31+
[OutputType([System.Object])]
32+
param(
33+
# A friendly name/identifier for this connection context
34+
[Parameter(Mandatory)]
35+
[string] $Name,
36+
37+
# The API key as a SecureString. If not provided, user will be prompted.
38+
[Parameter(ParameterSetName = 'Interactive')]
39+
[Parameter(Mandatory, ParameterSetName = 'NonInteractive')]
40+
[SecureString] $ApiKey,
41+
42+
# Return the context object after creation
43+
[Parameter()]
44+
[switch] $PassThru,
45+
46+
# Suppress informational output
47+
[Parameter()]
48+
[switch] $Silent
49+
)
50+
51+
begin {
52+
Write-Debug '[Connect-PowerShellGallery] - Start'
53+
$null = Get-PowerShellGalleryConfig
54+
}
55+
56+
process {
57+
# Open browser to API key page if interactive
58+
if ($PSCmdlet.ParameterSetName -eq 'Interactive' -and -not $ApiKey) {
59+
if (-not $Silent) {
60+
Write-Host '🌐 Opening PowerShell Gallery API key management page...' -ForegroundColor Cyan
61+
}
62+
63+
$apiKeyUrl = 'https://www.powershellgallery.com/account/apikeys'
64+
65+
# Try to open browser
66+
try {
67+
if ($IsWindows -or $PSVersionTable.PSVersion.Major -le 5) {
68+
Start-Process $apiKeyUrl
69+
} elseif ($IsMacOS) {
70+
Start-Process 'open' -ArgumentList $apiKeyUrl
71+
} elseif ($IsLinux) {
72+
Start-Process 'xdg-open' -ArgumentList $apiKeyUrl
73+
}
74+
} catch {
75+
Write-Warning "Unable to open browser automatically. Please visit: $apiKeyUrl"
76+
}
77+
78+
if (-not $Silent) {
79+
Write-Host ''
80+
Write-Host 'Please create or copy your API key from the PowerShell Gallery.' -ForegroundColor Yellow
81+
Write-Host ''
82+
}
83+
}
84+
85+
# Prompt for API key if not provided
86+
if (-not $ApiKey) {
87+
$ApiKey = Read-Host -Prompt 'Enter your PowerShell Gallery API key' -AsSecureString
88+
}
89+
90+
# Validate API key is not empty
91+
if ($null -eq $ApiKey) {
92+
Write-Error 'API key is required to connect to PowerShell Gallery.'
93+
return
94+
}
95+
96+
# Create context object
97+
$context = @{
98+
ID = $Name
99+
Name = $Name
100+
ApiKey = $ApiKey
101+
GalleryUrl = 'https://www.powershellgallery.com'
102+
ApiUrl = 'https://www.powershellgallery.com/api/v2'
103+
ConnectedAt = Get-Date
104+
}
105+
106+
# Store context
107+
Write-Verbose "Storing context with ID: [$Name]"
108+
$result = Set-PowerShellGalleryContext -ID $Name -Context $context -Default -PassThru
109+
110+
if (-not $Silent) {
111+
Write-Host "✓ Successfully connected to PowerShell Gallery as [$Name]" -ForegroundColor Green
112+
}
113+
114+
if ($PassThru) {
115+
return $result
116+
}
117+
}
118+
119+
end {
120+
Write-Debug '[Connect-PowerShellGallery] - End'
121+
}
122+
}

0 commit comments

Comments
 (0)