Skip to content

Instantly share code, notes, and snippets.

@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"
}
@dathanb
dathanb / UnsafeUrlConnection.java
Created April 26, 2021 23:36
Disable SSL verification in Java
// copied from https://nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@dathanb
dathanb / commands.md
Last active January 6, 2021 23:05
Bash to powershell crib sheet

List files recursively

bashpowershell
find .
ls -rec

Find file by partial filename