Skip to content

Instantly share code, notes, and snippets.

@dathanb
dathanb / unix2dos.ps1
Created June 23, 2024 16:31
Convert line endings from unix to windows in Powershell
((Get-Content $FilePath) -join "`r`n") + "`r`n" | Set-Content -NoNewline $FilePath
@dathanb
dathanb / pre-commit
Last active June 18, 2024 04:54
Git pre-commit hook to stop commit if changes contain sensitive values
#!/usr/bin/env bash
# pre-commit hook to prevent a commit if the commit includes changes that
# match a set of stop strings
FORBIDDEN='DO NOT COMMIT'
git diff --cached | grep -q -E "${FORBIDDEN}"
ret=$?
if [[ $ret = 0 ]]; then
@dathanb
dathanb / build.gradle
Created May 12, 2023 17:26
Build PlantUML files from Gradle
apply plugin:'groovy'
repositories {
mavenCentral()
}
dependencies{
implementation 'org.codehaus.groovy:groovy:3.0.4'
implementation 'net.sourceforge.plantuml:plantuml:8059'
}
@dathanb
dathanb / genpass.sh
Created March 24, 2022 22:11
Generate a random password from the command line
length=40
forbiddenChars="+*/^"
openssl rand 4096 | \
LC_ALL=C tr -cd '[:alnum:][:punct:]' | \
tr -d "${forbiddenChars}" | \
fold -w "${length}" | \
head -n 1
@dathanb
dathanb / find-bin-dir.sh
Created March 4, 2022 23:06
Detect whether ~/bin is in the PATH
# Install to ~/bin if it's already on the path; otherwise install to /usr/local/bin
case ":$PATH:" in
*":$HOME/bin/:"*|*":$HOME/bin:"*)
INSTALL_DIR="$HOME/bin"
;;
*)
INSTALL_DIR=/usr/local/bin
;;
esac
@dathanb
dathanb / lines_as_array.sh
Created November 5, 2021 18:41
Read lines to array in Bash
# Mac OS ships with Bash 3.2, which lacks the read -a option, which makes reading into an array easy
# So this is a replacement
lines="first line
second line
third line"
linesArray=()
while read -r line; do
linesArray+=("$line")
done <<< "${lines}"
@dathanb
dathanb / gist:fccc74fc15834129e9ae4f841826c88a
Created October 28, 2021 03:40
Redirect stdout and stderr from within a script
logFile=~/log
# close stdout and stderr and redirect them to the log file
rm -f "${logFile}"
exec 1<&-
exec 2<&-
exec 1<>"${logFile}"
exec 2>&1
@dathanb
dathanb / JSON-ish IntelliJ toString template
Last active December 21, 2021 22:32
JSON-ish IntelliJ toString template
public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return "{\"_class\":\"$classname\", " +
#foreach( $member in $members )
#set ( $i = $i + 1 )
#if ( $i == $members.size() )
#set ( $postfix = "+" )
#else
#set ( $postfix = "+ "", "" + " )
@dathanb
dathanb / start-postgres.sh
Created September 23, 2021 20:46
Run dockerized postgres locally
docker run -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
# then connect to localhost:5432, database postgres, user postgres, password pasword
@dathanb
dathanb / retryableCurl.sh
Created August 12, 2021 21:35
Retryable curl
# Try hard to make idempotent calls to services that are a little flaky
function retryableCurl() {
for i in `seq 1 5`; do
code="$(curl -s -o /dev/null -w "%{http_code}" "$@")"
if [[ $? -eq 0 && code -lt 300 && code -ge 200 ]]; then
return
fi
done
printf "Failed to curl with arguments $*\n"
}