Skip to content

Instantly share code, notes, and snippets.

@dillonstreator
dillonstreator / fetchTimeout.ts
Last active April 13, 2024 20:36
`fetch` wrapper with timeout
class TimeoutError extends Error {
error: unknown;
constructor(timeoutMs: number, error: unknown) {
super(`Operation timed out after ${timeoutMs}ms`);
this.name = 'TimeoutError';
this.error = error;
}
}
@dillonstreator
dillonstreator / github-action-validate-k8s-yaml.yaml
Last active September 19, 2023 21:18
Github Action to validate Kubernetes yaml in a directory (`k8s`) with `kubeconform` including popular CRD's
name: Validate Kubernetes YAML
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
@dillonstreator
dillonstreator / golang-arbitrary-image-count-upload-with-browser-compression.md
Created August 21, 2023 00:41
Golang http server arbitrary image count upload with `browser-image-compression` and concurrent streaming
package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
@dillonstreator
dillonstreator / context-aware-io-copy.md
Last active August 21, 2023 00:36
Golang context aware `io.Copy`
type readerFunc func(p []byte) (n int, err error)

func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }

func Copy(ctx context.Context, dst io.Writer, src io.Reader) error {
	_, err := io.Copy(dst, readerFunc(func(p []byte) (int, error) {
		select {
		case <-ctx.Done():
 return 0, ctx.Err()
@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"
@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 &gt;/dev/null
@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 / 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 / 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 / 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()