STEP# | CODE | COMMENT |
---|---|---|
01 | echo -n "Are you sure (Y/N)? " |
print prompt just once with echo |
02 | answered= |
initiate an empty test binding |
03 | while [[ ! $answered ]]; do |
loop until test binding is populated |
04 | read -r -n 1 -s answer |
get input, supress escape, only 1 char, supress input |
05 | if [[ $answer = [Yy] ]]; then |
test if input is y or Y |
06 | answered="yes" |
..if true, set test binding to yes → step #12 |
07 | elif [[ $answer = [Nn] ]]; then |
else, test is input is n or N |
08 | answered="no" |
..if trye, set test binding to no → step #12 |
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
function match-branch ([switch]$local_only, [switch]$remote_only, [switch]$both) { | |
$localBranches = ((git branch -l) -replace "\*", "") -replace " ", "" | |
$remoteBranches = (((git branch -r) -replace "\*", "") -replace " ", "") -replace "origin/", "" | |
$branch_comparison = Compare-Object -ReferenceObject $localBranches -DifferenceObject $remoteBranches -IncludeEqual | |
| Select-Object @{Label = "branch"; Expression = { $_.InputObject } }, | |
@{Label = ”both”; Expression = { $_.SideIndicator -eq "==" } }, | |
@{Label = ”remote_only”; Expression = { $_.SideIndicator -eq "=>" } }, | |
@{Label = ”local_only”; Expression = { $_.SideIndicator -eq "<=" } } | |
if ($local_only) { $branch_comparison | Where-Object -Property "local_only" -EQ "true" | Select-Object branch } | |
elseif ($remote_only) {$branch_comparison | Where-Object -Property "remote_only" -EQ "true" | Select-Object branch} |
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
Get-ChildItem -Recurse -Directory | | |
Select-Object Name, @{ | |
Name="FileCount"; | |
Expression={(Get-ChildItem $_ -File | Measure-Object).Count } | |
} |
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
dir *.mp4 -rec -file | | |
select-object @{ | |
Name="folder"; | |
Expression = {Split-Path (Split-Path $_.Fullname -parent) -leaf} | |
},name |
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
#file: foobar.py | |
import argparse | |
def function_proper(param1, param2) -> None: | |
#CODE... | |
def init_argparse() -> argparse.ArgumentParser: | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--param1") | |
parser.add_argument("--param2") |
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
echo -n "Are you sure (Y/N)? " #01 | |
answered= #02 | |
while [[ ! $answered ]]; do #03 | |
read -r -n 1 -s answer #04 | |
if [[ $answer = [Yy] ]]; then #05 | |
answered="yes" #06 | |
elif [[ $answer = [Nn] ]]; then #07 | |
answered="no" #08 | |
fi #09 | |
done #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
moduleMissingError = 'Missing dependency module: Run "pip install -r requirements.txt" → Retry' | |
try: | |
import #<module1> | |
import #<package2> | |
import #<subpackage1> | |
except ImportError: | |
sys.exit(moduleMissingError) |
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
# Sometimes URL is copied from a browser and arrives as | |
# https://consul.foo.com/client/ami_size/edit | |
# Othertimes URL is copied from Consul CLI and arrives as | |
# client/ami_size | |
# And I just need the actual key, being 'ami_size' for the script | |
$url = ($url -match "^https://") ? | |
($url -replace "https://consul.foo.com/client/", "") -replace "/edit", "": | |
($url -replace "client/", "") |
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
### func1 | |
function query-consul () { | |
param ( | |
[String[]]$consul_keys, | |
[String]$queryString, | |
) | |
foreach ($consul_key in $consul_keys) { | |
consul kv get -recurse $consul_key_ | | |
Select-String $queryString |
NR | CODE | COMMENT |
---|---|---|
#1 | response = requests.get(url,...) |
within a try block, create variable with a response object |
#2 | response.raise_for_status() |
raises exception 4xx & 5xx HTTP Statuses/errors |
#3 | except requests.exceptions.RequestException as e: |
exception base class for all other exceptions |
#4 | print(e, file=sys.stderr) |
print exception object into stderr stream |