Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
🌟[Major]: Implement Module Contexts and User Contexts#107
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7773f3bc675415a4c29a83a1a3b14b0beaeec5fcad89e3796e2aebffFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| function Get-ActiveModuleContext { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets the name of the active module context for a vault. | ||
| .DESCRIPTION | ||
| Reads the active module context name from the 'active-context' file in the module directory. | ||
| If the file doesn't exist or is invalid, returns 'default'. | ||
| .EXAMPLE | ||
| Get-ActiveModuleContext -VaultPath 'C:\Users\John\.contextvaults\MyVault' | ||
| Returns the name of the active module context, or 'default' if none is set. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| # The path to the vault directory. | ||
| [Parameter(Mandatory)] | ||
| [string] $VaultPath | ||
| ) | ||
| $activeContextFile = Join-Path -Path $VaultPath -ChildPath 'module' | Join-Path -ChildPath 'active-context' | ||
| if (Test-Path $activeContextFile) { | ||
| try { | ||
| $contextName = (Get-ContentNonLocking -Path $activeContextFile).Trim() | ||
| if ([string]::IsNullOrWhiteSpace($contextName)) { | ||
| return 'default' | ||
| } | ||
| return $contextName | ||
| } catch { | ||
| Write-Warning "Failed to read active context file: $_" | ||
| return 'default' | ||
| } | ||
| } | ||
| return 'default' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| function Get-ContextDirectory { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets the appropriate directory path for a context based on its type. | ||
| .DESCRIPTION | ||
| Returns the directory path where contexts of the specified type should be stored. | ||
| For User contexts, returns the 'user' subdirectory. | ||
| For Module contexts, returns the 'module' subdirectory. | ||
| .EXAMPLE | ||
| Get-ContextDirectory -VaultPath 'C:\Users\John\.contextvaults\MyVault' -Type 'User' | ||
| Returns: C:\Users\John\.contextvaults\MyVault\user | ||
| .EXAMPLE | ||
| Get-ContextDirectory -VaultPath 'C:\Users\John\.contextvaults\MyVault' -Type 'Module' | ||
| Returns: C:\Users\John\.contextvaults\MyVault\module | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| # The path to the vault directory. | ||
| [Parameter(Mandatory)] | ||
| [string] $VaultPath, | ||
| # The type of context: 'User' or 'Module'. | ||
| [Parameter(Mandatory)] | ||
| [ValidateSet('User', 'Module')] | ||
| [string] $Type | ||
| ) | ||
| $subdirectory = if ($Type -eq 'Module') { 'module' } else { 'user' } | ||
| return Join-Path -Path $VaultPath -ChildPath $subdirectory | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| function Set-ActiveModuleContext { | ||
| <# | ||
| .SYNOPSIS | ||
| Sets the active module context for a vault. | ||
| .DESCRIPTION | ||
| Writes the active module context name to the 'active-context' file in the module directory. | ||
| .EXAMPLE | ||
| Set-ActiveModuleContext -VaultPath 'C:\Users\John\.contextvaults\MyVault' -ContextName 'staging' | ||
| Sets 'staging' as the active module context. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| # The path to the vault directory. | ||
| [Parameter(Mandatory)] | ||
| [string] $VaultPath, | ||
| # The name of the context to set as active. | ||
| [Parameter(Mandatory)] | ||
| [string] $ContextName | ||
| ) | ||
| $moduleDir = Join-Path -Path $VaultPath -ChildPath 'module' | ||
| $activeContextFile = Join-Path -Path $moduleDir -ChildPath 'active-context' | ||
| # Ensure module directory exists | ||
| if (-not (Test-Path $moduleDir)) { | ||
| $null = New-Item -Path $moduleDir -ItemType Directory -Force | ||
| } | ||
| try { | ||
| Set-Content -Path $activeContextFile -Value $ContextName -NoNewline | ||
| Write-Verbose "Set active module context to: $ContextName" | ||
| } catch { | ||
| throw "Failed to set active context: $_" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| function Get-ActiveModuleContextName { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets the name of the currently active module context for a vault. | ||
| .DESCRIPTION | ||
| Returns the name of the currently active module context for the specified vault. | ||
| If no active context is set or the file is missing, returns 'default'. | ||
| .EXAMPLE | ||
| Get-ActiveModuleContextName -Vault 'MyModule' | ||
| Returns the name of the active module context for 'MyModule' vault. | ||
| .OUTPUTS | ||
| [System.String] | ||
| .NOTES | ||
| This function reads the active context name from the vault's metadata, | ||
| falling back to 'default' if not set. | ||
| .LINK | ||
| https://psmodule.io/Context/Functions/Get-ActiveModuleContextName/ | ||
| #> | ||
| [OutputType([string])] | ||
| [CmdletBinding()] | ||
| param( | ||
| # The name of the vault to check. | ||
| [Parameter(Mandatory)] | ||
| [string] $Vault | ||
| ) | ||
| begin { | ||
| $stackPath = Get-PSCallStackPath | ||
| Write-Debug "[$stackPath] - Begin" | ||
| } | ||
| process { | ||
| $vaultObject = Get-ContextVault -Name $Vault | ||
| $activeContextName = Get-ActiveModuleContext -VaultPath $vaultObject.Path | ||
| Write-Verbose "[$stackPath] - Active module context for vault '$Vault': '$activeContextName'" | ||
| return $activeContextName | ||
| } | ||
| end { | ||
| Write-Debug "[$stackPath] - End" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -87,7 +87,12 @@ | ||
| # The name of the vault to remove contexts from. | ||
| [Parameter()] | ||
| [string] $Vault | ||
| [string] $Vault, | ||
| # The type of context to remove: 'User' or 'Module'. | ||
| [Parameter()] | ||
| [ValidateSet('User', 'Module')] | ||
| [string] $Type = 'User' | ||
| ) | ||
| begin { | ||
| @@ -96,14 +101,34 @@ | ||
| } | ||
| process { | ||
| $contextInfo = Get-ContextInfo -ID $ID -Vault $Vault | ||
| $contextInfo = Get-ContextInfo -ID $ID -Vault $Vault -Type $Type | ||
| foreach ($contextInfo in $contextInfo) { | ||
| $contextId = $contextInfo.ID | ||
| if ($PSCmdlet.ShouldProcess("Context '$contextId'", 'Remove')) { | ||
| Write-Verbose "[$stackPath] - Removing context [$contextId]" | ||
| Write-Verbose "[$stackPath] - Removing context [$contextId] of type [$Type]" | ||
| # Special handling for module contexts - don't allow removing 'default' if it's the only one | ||
| if ($Type -eq 'Module' -and $contextId -eq 'default') { | ||
| $allModuleContexts = Get-ContextInfo -ID '*' -Vault $Vault -Type 'Module' | ||
| if ($allModuleContexts.Count -le 1) { | ||
| Write-Warning "Cannot remove the default module context when it's the only module context." | ||
| continue | ||
| } | ||
| } | ||
MariusStorhaug marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| $contextInfo.Path | Remove-Item -Force -ErrorAction Stop | ||
| Write-Verbose "[$stackPath] - Removed item: $contextId" | ||
| # If we removed the active module context, reset to 'default' | ||
| if ($Type -eq 'Module') { | ||
| $vaultObject = Get-ContextVault -Name $Vault | ||
| $activeContext = Get-ActiveModuleContext -VaultPath $vaultObject.Path | ||
| if ($activeContext -eq $contextId -and $contextId -ne 'default') { | ||
| Write-Verbose "[$stackPath] - Resetting active module context to 'default'" | ||
| Set-ActiveModuleContext -VaultPath $vaultObject.Path -ContextName 'default' | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.