Skip to content

Commit

Permalink
Copy from jaykul/DevOps2023-Practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed May 1, 2023
1 parent 7742cc2 commit 75a6e55
Show file tree
Hide file tree
Showing 22 changed files with 1,308 additions and 0 deletions.
1 change: 1 addition & 0 deletions Joel-Bennett-Tempting-Fate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export/
13 changes: 13 additions & 0 deletions Joel-Bennett-Tempting-Fate/.markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

default: true
MD003:
style: "atx"
MD022:
lines_above: 1
lines_below: 1
MD025: false
MD013: false
MD033: false
MD007:
indent: 4
no-hard-tabs: false
427 changes: 427 additions & 0 deletions Joel-Bennett-Tempting-Fate/LICENSE

Large diffs are not rendered by default.

642 changes: 642 additions & 0 deletions Joel-Bennett-Tempting-Fate/ReadMe.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/00. exception.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# These three functions show how sometimes an -ErrorAction Ignore can surpress an exception ...
# But have it bubble up again later when someone tries a trap or try/catch
function divide {
[CmdletBinding()]
param($numerator = 1, $denominator = 0)
$numerator / $denominator
}

function nevercrash {
[CmdletBinding()]
param($numerator = 1, $denominator = 0)
divide @PSBoundParameters -ErrorAction Ignore
}

function surprise {
[CmdletBinding()]
param($ammount = 1)
try {
nevercrash $ammount
} catch {
throw $_
}
}
21 changes: 21 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/01. template.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Import-Configuration {
<#
.SYNOPSIS
TODO
.EXAMPLE
TODO
#>
[CmdletBinding()]
param(
# TODO
[Parameter(ValueFromPipelineByPropertyName)]
$TODO
)
process {
try {
# TODO
} catch {
throw $_
}
}
}
49 changes: 49 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/02. demo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# A basic prompt
function prompt {
"`e[36m$($MyInvocation.HistoryId)`e[37m $pwd`e[0m`n$([char]0x276f)"
}

# When there are errors in your prompt
function prompt {
Write-Error "Typo";
"`e[36m$($MyInvocation.HistoryId)`e[37m $pwd`e[0m`n$([char]0x276f)"
}

# When there's an exception in your prompt
function prompt {
Write-Error "Typo"
"`e[36m$($MyInvocation.HistoryId)`e[37m $pwd`e[0m`n$([char]0x276f)"
throw "grenade"
}

# You should know to check ...
$Error[0..2]

# Let's look at how PowerLine handles it:
Set-PowerLinePrompt

# Your prompt is an array of scriptblocks
$prompt

# We can add an error to it
Add-PowerLineBlock { Write-Error "Typo"}

# We can hide that warning output:
Set-PowerLinePrompt -HideErrors

# Even if it's an exception!
Add-PowerLineBlock { throw "grenades" }

# Hiding it is probably a bad idea
Set-PowerLinePrompt -HideErrors:$False

# So let's look at the errors
# We can see which block caused them
$PromptErrors

# And look more closely at the exception
$PromptErrors[1] | fl * -fo

# Then we can put our prompt back
Remove-PowerLineBlock { Write-Error "Typo" }
Remove-PowerLineBlock { throw "grenades" }
16 changes: 16 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/03. logging.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[CmdletBinding()]
param(
[switch]$PleaseDontThrow
)

try {
Write-Information "Enter $($MyInvocation.MyCommand)" -Tag Trace
if (!$PleaseDontThrow) {
Write-Information "`$PleaseDontThrow was $PleaseDontThrow, so ..." -Tag Trace
throw "horseshoes"
}
} catch {
Write-Information $_ -Tag Exception
throw $_
}
Write-Information "Exit $($MyInvocation.Line)" -Tag Trace
35 changes: 35 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/04. Import-Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Import-Configuration {
<#
.SYNOPSIS
A command to load configuration for a module
.EXAMPLE
$Config = Import-Configuration
Load THIS module's configuration from a command
.EXAMPLE
$Config = Import-Configuration
$Config.AuthToken = $ShaToken
$Config | Export-Configuration
Update a single setting in the configuration
.EXAMPLE
$Config = Get-Module PowerLine | Import-Configuration
$Config.PowerLineConfig.DefaultAddIndex = 2
Get-Module PowerLine | Export-Configuration $Config
Update a single setting in the configuration
#>
[CmdletBinding()]
param(
# The module to import configuration from
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)]
[System.Management.Automation.PSModuleInfo]$Module
)
process {
try {
# TODO
} catch {
throw $_
}
}
}
15 changes: 15 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/05. ArgumentTransformation.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ModuleInfoAttribute : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object] $inputData) {
$ModuleInfo = $null
if ($inputData -is [string] -and -not [string]::IsNullOrWhiteSpace($inputData)) {
$ModuleInfo = Get-Module $inputData -ErrorAction SilentlyContinue
if (-not $ModuleInfo) {
$ModuleInfo = @(Get-Module $inputData -ErrorAction SilentlyContinue -ListAvailable)[0]
}
}
if (-not $ModuleInfo) {
throw ([System.ArgumentException]"$inputData module could not be found, please try passing the output of 'Get-Module $InputData' instead")
}
return $ModuleInfo
}
}
64 changes: 64 additions & 0 deletions Joel-Bennett-Tempting-Fate/demo/06. Import-Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using namespace System.Management.Automation

class ModuleInfoAttribute : ArgumentTransformationAttribute {
[object] Transform([EngineIntrinsics]$engineIntrinsics, [object] $inputData) {
$ModuleInfo = $null
if ($inputData -is [string] -and -not [string]::IsNullOrWhiteSpace($inputData)) {
$ModuleInfo = Get-Module $inputData -ErrorAction SilentlyContinue
if (-not $ModuleInfo) {
$ModuleInfo = @(Get-Module $inputData -ErrorAction SilentlyContinue -ListAvailable)[0]
}
}
if (-not $ModuleInfo) {
throw ([System.ArgumentException]"$inputData module could not be found, please try passing the output of 'Get-Module $InputData' instead")
}
return $ModuleInfo
}
}

function Import-Configuration {
<#
.SYNOPSIS
A command to load configuration for a module
.EXAMPLE
$Config = Import-Configuration
Load THIS module's configuration from a command
.EXAMPLE
$Config = Import-Configuration
$Config.AuthToken = $ShaToken
$Config | Export-Configuration
Update a single setting in the configuration
.EXAMPLE
$Config = Get-Module PowerLine | Import-Configuration
$Config.PowerLineConfig.DefaultAddIndex = 2
Get-Module PowerLine | Export-Configuration $Config
Update a single setting in the configuration
.EXAMPLE
$Config = Import-Configuration -Name Powerline -CompanyName HuddledMasses.org
Load the specififed module's configuration by hand
#>
[CmdletBinding()]
param(
# The module to import configuration from
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)]
[ModuleInfo()]
[PSModuleInfo]$Module
)
process {
try {

$Path = Join-Path $Env:APPDATA (
Join-Path $Module.CompanyName $Module.Name
)

Import-LocalizedData -BaseDirectory $Path -FileName Configuration.psd1

} catch {
throw $_
}
}
}
Binary file added Joel-Bennett-Tempting-Fate/images/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/cc-by-nc-sa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/ccbysa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/pshsummit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Joel-Bennett-Tempting-Fate/images/pshsummit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Joel-Bennett-Tempting-Fate/images/summit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 75a6e55

Please sign in to comment.