Skip to content

Commit

Permalink
🩹 [Patch]: Support both Windows PowerShell and PowerShell (#23)
Browse files Browse the repository at this point in the history
## Description

- Remove requirement on PowerShell 7

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [ ] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [x] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Aug 11, 2024
1 parent b9a0793 commit 039557f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ This module is designed to be a simple way to store and retrieve secrets and var

## Prerequisites

This module relies on [Microsoft.PowerShell.SecretManagement] and [Microsoft.PowerShell.SecretStore] by default. You can use other secret vault
This module relies on [Microsoft.PowerShell.SecretManagement](https://github.com/powershell/SecretManagement) and
[Microsoft.PowerShell.SecretStore](https://github.com/PowerShell/SecretStore) by default. You can use other secret vault
providers by installing them and setting them as the provider when calling the function.

## Installation
Expand Down Expand Up @@ -81,3 +82,9 @@ Please see the issues tab on this project and submit a new issue that matches yo

If you do code, we'd love to have your contributions. Please read the [Contribution guidelines](CONTRIBUTING.md) for more information.
You can either help by picking up an existing issue or submit a new one if you have an idea for a new feature or improvement.

## Links

- SecretManagement | [GitHub](https://github.com/powershell/SecretManagement) | [Docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.secretmanagement/?view=ps-modules)
- SecretStore | [GitHub](https://github.com/PowerShell/SecretStore) | [Docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.secretstore/?view=ps-modules)
- [Overview of the SecretManagement and SecretStore modules | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/utility-modules/secretmanagement/overview?view=ps-modules)
2 changes: 1 addition & 1 deletion src/init/Store.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$script:Store = @{}
$script:Store = [PSCustomObject]@{}
12 changes: 11 additions & 1 deletion src/private/Get-StoreVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
.SYNOPSIS
Get a variable from the store.
.EXAMPLE
Get-StoreVariable
Gets all the variables in the store.
.EXAMPLE
Get-StoreVariable -Name 'Name'
Expand All @@ -11,8 +16,13 @@
[CmdletBinding()]
[OutputType([object])]
param(
[Parameter()]
[string] $Name
)

$script:Store[$Name]
if (-not $Name) {
$script:Store
} else {
$script:Store.$Name
}
}
3 changes: 1 addition & 2 deletions src/private/Initialize-SecretStore.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#Requires -Version 7.0
#Requires -Modules Microsoft.PowerShell.SecretManagement
#Requires -Modules Microsoft.PowerShell.SecretManagement
#Requires -Modules Microsoft.PowerShell.SecretStore

function Initialize-SecretStore {
Expand Down
4 changes: 2 additions & 2 deletions src/private/Initialize-VariableStore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
)

$folderName = ".$($Name -replace '^\.')".ToLower()
$configFilePath = Join-Path -Path $HOME -ChildPath $folderName 'config.json'
$configFilePath = Join-Path -Path $HOME -ChildPath "$folderName/config.json"

if (-not (Test-Path -Path $configFilePath)) {
$null = New-Item -Path $configFilePath -ItemType File -Force
Set-StoreVariable -Name 'ConfigFilePath' -Value $configFilePath
Set-StoreVariable -Name 'Name' -Value $Name
}

$script:Store = Get-Content -Path $configFilePath | ConvertFrom-Json -AsHashtable
$script:Store = Get-Content -Path $configFilePath | ConvertFrom-Json

}
6 changes: 3 additions & 3 deletions src/private/Set-StoreVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

if ($PSCmdlet.ShouldProcess("Set variable '$Name' to '$Value'")) {
if ($null -eq $Value) {
$script:Store.Remove($Name)
$script:Store.PSObject.Properties.Remove($Name)
} else {
$script:Store[$Name] = $Value
$script:Store | Add-Member -MemberType NoteProperty -Name $Name -Value $Value -Force
}
$script:Store | ConvertTo-Json -Depth 100 | Set-Content -Path $script:Store['ConfigFilePath'] -Force
$script:Store | ConvertTo-Json -Depth 100 | Set-Content -Path $script:Store.ConfigFilePath -Force
}
}
18 changes: 16 additions & 2 deletions src/public/Get-StoreConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function Get-StoreConfig {
#Requires -Modules Microsoft.PowerShell.SecretManagement

function Get-StoreConfig {
<#
.SYNOPSIS
Get configuration value.
Expand All @@ -15,14 +17,26 @@
[CmdletBinding()]
param (
# Choose a configuration name to get.
[Parameter(Mandatory)]
[Parameter()]
[string] $Name,

# Return the value as plain text if it is a secret.
[Parameter()]
[switch] $AsPlainText
)

if (-not $Name) {
return [pscustomobject]@{
Secrets = Get-SecretInfo | ForEach-Object {
[pscustomobject]@{
Name = $_.Name
Value = Get-Secret -Name $_.Name -AsPlainText:$AsPlainText -Vault $script:Store.SecretVaultName
}
}
Variables = $script:Store
}
}

$value = Get-StoreVariable -Name $Name

if (($null -eq $value) -and ((Get-SecretInfo -Vault $script:Store.SecretVaultName).Name -contains $Name)) {
Expand Down
4 changes: 3 additions & 1 deletion src/public/Set-StoreConfig.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function Set-StoreConfig {
#Requires -Modules Microsoft.PowerShell.SecretManagement

function Set-StoreConfig {
<#
.SYNOPSIS
Set a configuration variables or secret.
Expand Down
8 changes: 6 additions & 2 deletions tests/Store.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ Describe 'Store' {
It 'Should be available' {
Get-Command -Name 'Get-StoreConfig' | Should -Not -BeNullOrEmpty
}
It 'Should be able to run' {
It 'Should be able to run without parameters' {
Write-Verbose (Get-StoreConfig | ConvertTo-Json) -Verbose
{ Get-StoreConfig } | Should -Not -Throw
}
It 'Should be able to run with parameters' {
{ Get-StoreConfig -Name 'Name' } | Should -Not -Throw
}
It 'Should be able to get its own name' {
Expand All @@ -35,7 +39,7 @@ Describe 'Store' {
}
It 'Should be able to get its own path' {
$configFilePath = Get-StoreConfig -Name 'ConfigFilePath'
$configFilePath | Should -Be (Join-Path -Path $HOME -ChildPath '.github' 'config.json')
$configFilePath | Should -Be (Join-Path -Path $HOME -ChildPath '.github/config.json')
}
It 'Should be able to get the secret vault name' {
$secretVaultName = Get-StoreConfig -Name 'SecretVaultName'
Expand Down

0 comments on commit 039557f

Please sign in to comment.