Skip to content

Instantly share code, notes, and snippets.

@jkeifer
jkeifer / compile-requirements.bash
Last active April 21, 2024 20:08
Bash script using pip-tools to compile a python project's requirements files
#!/usr/bin/env bash
set -euo pipefail
find_this () {
THIS="${1:?'must provide script path, like "${BASH_SOURCE[0]}" or "$0"'}"
trap "echo >&2 'FATAL: could not resolve parent directory of ${THIS}'" EXIT
[ "${THIS:0:1}" == "/" ] || THIS="$(pwd -P)/${THIS}"
THIS_DIR="$(dirname -- "${THIS}")"
@jkeifer
jkeifer / set-aws-secrets.sh
Last active September 20, 2023 14:51
Shell function to set/unset AWS session env vars from configured profiles (with SSO support)
set-aws-secrets() {
local profile_name="${1:?"must provide profile name"}"
local creds
creds=$(aws configure export-credentials --profile "${profile_name}")
rc=$?
if [ "${rc}" -eq 255 ]; then
# using sso and not logged in
local sso_session="$(aws configure get sso_session --profile "${profile_name}")"
@jkeifer
jkeifer / dbmate
Last active April 17, 2023 16:14
A dbmate (https://github.com/amacneil/dbmate) wrapper to allow managing a reference schema file
#!/usr/bin/env bash
#
# Released under the MIT license at
# https://gist.github.com/jkeifer/f75c65213c6a327229cf85ffa47e1efe
#
# Copyright 2023 Jarrett Keifer <jkeifer0@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
@jkeifer
jkeifer / gfsib
Last active August 4, 2022 02:25
Git [g]it-[f]ormat-[s]taged, implemented [i]n [b]ash
#!/usr/bin/env bash
#
# gfsib: [g]it-[f]ormat-[s]taged, implemented [i]n [b]ash
#
# This script is _heavily_ inspired by `git-format-staged` by
# Jesse Hallett <jesse@sitr.us>.
#
# This implementation is © Jarrett Keifer <jkeifer0@gmail.com>.
# Distributed under the MIT License at
# https://gist.github.com/jkeifer/3cb226efcd4a4654a3461732a3f29ab8
@jkeifer
jkeifer / getopt.bash
Last active March 15, 2023 16:39
getopt-like option parsing in pure bash
#!/usr/bin/env bash
#
# Released under the MIT license at
# https://gist.github.com/jkeifer/62ab2ce30254923f2ecf791f61871490
#
# Copyright 2021 - 2023 Jarrett Keifer <jkeifer0@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
@jkeifer
jkeifer / nsmgr
Last active June 9, 2020 01:03
Simple tool to manage network namespaces as files
#!/bin/sh -eu
usage() {
cat <<EOF
$0 - utility to manage network namespaces through arbitrary files
Supported commands:
add FILE
@jkeifer
jkeifer / traps.bash
Last active May 2, 2018 00:03
bash EXIT traps to ensure resource cleanup
#!/bin/bash
# Call like `./traps.bash 0` and you should see:
# outside
# level 1
# level 2
# level 2 trap
# This won't run on error
# level 1 trap
# Nor with this on error
@jkeifer
jkeifer / obj_replacement.py
Last active March 30, 2018 18:34
How to do in-place object replacement in python
from __future__ import print_function
_id_seq = 1
def id_seq():
global _id_seq
num = _id_seq
_id_seq += 1
return num