Skip to content

Commit

Permalink
new state example
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Jan 9, 2024
1 parent f34fe86 commit eeeabc6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Pwsh/Modules/ScriptWide-State.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[hashtable]$script:CachedValue = @{}
[hashtable]$script:AppConfig = @{
Log = Join-Path 'temp:' 'foo.log'
# ... other app-wite properties
}
function CachedGetCommand {
[CmdletBinding()]
param( [string]$CommandName, [switch]$Force )
$state = $script:CachedValue
$start = [Datetime]::Now

if($Force -or ( -not $state.ContainsKey( $CommandName ) )) {
'fetching....' | write-verbose -verbose
$query = Get-Command -CommandType Application -Name $CommandName -TotalCount 1
if( -not $query) { return }
$state[ $CommandName ] = $query
}

$delta = [Datetime]::Now - $Start
'Lookup {0} took {1}' -f @(
$CommandName
[Datetime]::Now - $Start | Join-string -f '{0:n2} ms' TotalMilliseconds
) | Write-Verbose

$state[ $CommandName ]
}
function LogIt {
param(
$InputObject,

[ArgumentCompletions('Error', 'Warning', 'Info')]
[string]$Severity,

[switch]$PassThru
)
process {
$Now = Get-Date
$InputObject
| ConvertTo-JSon -depth 4 -compress
| Join-String -f "${now}: ${Severity}: "
| Add-Content -Path $Script:AppConfig.Log -PassThru:$PassThru
}
}


CachedGetCommand 'pwsh' -Verbose -Force
CachedGetCommand 'pwsh' -Verbose
CachedGetCommand 'pwsh' -Verbose

###
LogIt -in 'App Init...' -Severity Info

#
gc (gi $script:AppConfig.Log)

0 comments on commit eeeabc6

Please sign in to comment.