Created
June 2, 2020 07:40
-
-
Save btipling/e1a8bafcfe39d70a6d0ec0610c12bdfd to your computer and use it in GitHub Desktop.
A little toy key value store in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS C:\Users\swart\projects\powershell_scripts> .\name_value_store.ps1 | |
PS C:\Users\swart\projects\powershell_scripts> Add-Data -Name "Foo" -Value 2 | |
PS C:\Users\swart\projects\powershell_scripts> Add-Data -Name "Bar" -Value 8 | |
PS C:\Users\swart\projects\powershell_scripts> Read-Data | |
Name Value | |
---- ----- | |
Foo 2 | |
Bar 8 | |
PS C:\Users\swart\projects\powershell_scripts> Find-Data -Name "Bar" | |
8 | |
PS C:\Users\swart\projects\powershell_scripts> (Find-Data -Name "Bar") + (Find-Data -Name "Foo") | |
10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$global:dataFileName = "__data_store.csv" | |
function global:Get-DataPath() { | |
return Join-Path -Path (Get-Location) -ChildPath $global:dataFileName | |
} | |
function global:Read-Data() { | |
$filePath = Get-DataPath | |
if (-Not (Test-Path -Path $filePath)) { | |
return $null | |
} | |
return Import-Csv -Path $filePath | |
} | |
function global:Get-Data() { | |
$objects = Read-Data | |
$data = @{ } | |
if ($null -eq $objects) { | |
return $data | |
} | |
$objects | ForEach-Object { $data[$_.Name] = $_.Value } | |
return $data | |
} | |
function global:Set-Data([hashtable] $Data) { | |
$filePath = Get-DataPath | |
$objects = $Data.GetEnumerator() | ForEach-Object { [PSCustomObject]@{Name = $_.Key; Value = $_.Value } } | |
$objects | export-csv -Path $filePath | |
} | |
function global:Add-Data([string] $Name, [int] $Value) { | |
$data = Get-Data | |
$data[$Name] = $Value | |
Set-Data -Data $data | |
} | |
function global:Find-Data([string]$Name) { | |
$data = Get-Data | |
[int] $data[$Name] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment