Skip to content

Instantly share code, notes, and snippets.

@dillonstreator
dillonstreator / crumby.js
Last active January 29, 2022 16:10
Javascript generate breadcrumb array from url pathname
const titleizeWord = (str) => `${str[0].toUpperCase()}${str.slice(1)}`;
const kebabToTitle = (str) => str.split("-").map(titleizeWord).join(" ");
const toBreadcrumbs = (pathname, { rootName = "Home", nameTransform = s=>s } = {}) =>
pathname
.split("/")
.filter(Boolean)
.reduce(
(acc, curr, idx, arr) => {
acc.path += `/${curr}`;
@dillonstreator
dillonstreator / port-to-pid.md
Last active October 7, 2021 01:01
find PID running on a port utility shell function

Find PID running on a port

# .zshrc

port-to-pid() {
        lsof -i:$1 -Fp | sed 's/^p//' | head -n 1
}
@dillonstreator
dillonstreator / go-concurrent-job-processing.go
Last active October 7, 2021 14:16
concurrently process items with a bounded channel with storing job results, graceful shutdown, pickup where left off, and capturing of all errors
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
@dillonstreator
dillonstreator / postgres-jsonb-path-query.sql
Created November 17, 2021 17:31
postgres jsonb path query retrieve field in object with field that is array of objects
SELECT
id,
jsonb_path_query(json_field::jsonb, '$.arrayfield[*] ? (@.fieldid == $fieldid)', '{"fieldid": "field-id-value-to-find"}')->>'fieldvalue' as field_value
FROM table
WHERE json_field::jsonb->'arrayfield' @> '[{"fieldid": "field-id-value-to-find"}]';
@dillonstreator
dillonstreator / midnight-interval.ts
Created November 20, 2021 19:25
javascript/typescript interval to execute every midnight
const midnightInterval = (fn: ()=>void) => {
const midnight = new Date();
midnight.setHours( 24 );
midnight.setMinutes( 0 );
midnight.setSeconds( 0 );
midnight.setMilliseconds( 0 );
const msUntilMidnight = ( midnight.getTime() - new Date().getTime() ) / 1000;
setTimeout(() => {
fn()
@dillonstreator
dillonstreator / mockery-mocks.sh
Created November 30, 2021 16:34
generate mocks for golang interfaces in mocks.go files using mockery and goconcat binaries in shell scripts by finding internal and pkg
#!/usr/bin/env bash
cd "$(dirname "$0")"
find internal pkg -type d -print0 |
while IFS= read -r -d '' path; do
rm -f $path/mock_*.go || exit 1
rm -f $path/mocks.go || exit 1
mockery --dir $path --inpackage --name '(.*)' --quiet || exit 1
@dillonstreator
dillonstreator / athena_bearer_token.zsh
Created January 5, 2022 17:34
athenahealth get bearer token from ATHENA_CLIENT_ID and ATHENA_SECRET zsh command
athena-bearer() {
curl -H "Authorization: Basic $(echo -n "${ATHENA_CLIENT_ID}:${ATHENA_SECRET}" | base64)" -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=client_credentials&scope=athena/service/Athenanet.MDP.*" "${ATHENA_BASE_URL}/oauth2/v1/token"
}
@dillonstreator
dillonstreator / dountil
Created March 28, 2022 15:28
run command until successful with sleep
dountil() {
s=${SLEEP:-1}
until eval $*; do echo "failed.. retrying in $s sec" && sleep $s; done
}
@dillonstreator
dillonstreator / cloudflared-raspberry-pi.md
Last active April 15, 2024 10:43
install `cloudflared` on raspberry pi
sudo apt-get update && sudo apt-get upgrade
sudo apt install curl lsb-release
curl -L https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-archive-keyring.gpg >/dev/null
@dillonstreator
dillonstreator / reader-to-multiple-consumers.md
Last active August 17, 2023 16:20
Golang one `io.Reader` into multiple `io.Reader` consumers
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"log"