Skip to content

Instantly share code, notes, and snippets.

View jeffjohnson9046's full-sized avatar

Jefe Johnson jeffjohnson9046

View GitHub Profile
@jeffjohnson9046
jeffjohnson9046 / jq-json-to-csv.sh
Created February 13, 2020 19:37
A fancy jq filter to turn JSON output into a CSV format
# From the second answer here - this SO answer gets all the credit:
# https://stackoverflow.com/questions/32960857/how-to-convert-arbitrary-simple-json-to-csv-using-jq
# This is the filter to convert JSON to CSV output:
# NOTE: This filter only works on "flat" JSON; nested properties don't work with this filter as-is.
jq -r '(.[0] | keys_unsorted) as $keys | ([$keys] + map([.[ $keys[] ]])) [] | @csv'
# For example:
aws ec2 describe-instances \
--region us-west-2 \
@jeffjohnson9046
jeffjohnson9046 / ec2-instance-query.zsh
Last active October 10, 2022 13:57
Look up an EC2 Instance by public IP across more than one region
# This is the function I created that's a wee bit less shitty than the file above. I created a file called (oddly enough)
# ec2-instance-query.zsh and put it in my ~/.oh-my-zsh/custom directory. After sourcing my ~/zshrc, I have a fancy-pants
# function for querying EC2 instances and ElasticIP addresses for a given IP:
function ec2-instance-query() {
echo "Finding EC2 information for IP $1:"
for r in `aws ec2 describe-regions --output text | cut -f4 | grep "us-"`
do
aws ec2 describe-instances \
--region $r \
--filter "Name=network-interface.addresses.association.public-ip, Values=$1" \
@jeffjohnson9046
jeffjohnson9046 / set-aws-env.zsh
Last active November 3, 2022 12:47
A zsh script for setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables
# I have several AWS profiles in my ~/.aws/config thus there are several access keys and secret keys in my
# ~/.aws/credentials file. Sometimes (e.g. when using kops or kubectl) I need to have the AWS_ACCESS_KEY_ID
# and AWS_SECRET_ACCESS_KEY environment variables set. There doesn't seem to be a way to use the aws cli to set these
# environment variables for me, so... well... here we are.
#
# This script will:
# * Look at the ~/.aws/credentials file for the specified profile
# * Get the next two lines beneath the profile name, which are the access_key_id and secret_key respectively
# * Export those two values as environment variables
#
@jeffjohnson9046
jeffjohnson9046 / zsh-prompt-setup.sh
Created January 14, 2020 18:41
Steps I took to set up my Z shell prompt the way I like
# NOTE: These steps assume that zsh is already installed
# Download and install iTerm2, which can be found here: https://iterm2.com/downloads/
# Install oh-my-zsh (details and alternate installation instructions can be found here: https://github.com/ohmyzsh/ohmyzsh):
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# Install the Powerlevel9k theme for zsh (details for using the oh-my-zsh installation can be found here: https://github.com/Powerlevel9k/powerlevel9k/wiki/Install-Instructions#option-2-install-for-oh-my-zsh):
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
@jeffjohnson9046
jeffjohnson9046 / k8s-get-docker-container-memory.sh
Created October 19, 2019 15:43
Get the currently used and maximum available memory for a Docker container running in Kubernetes
# These commands demonstrate how to determine how much memory is availble on a k8s worker node and how much memory is
# available to the k8s _container_ hosted on that node.
# Source:
# https://shuheikagawa.com/blog/2017/05/27/memory-usage/
#
# Other interesting articles:
# https://docs.docker.com/engine/docker-overview/#the-underlying-technology
# https://docs.docker.com/config/containers/resource_constraints/
#
# These values are approximations, because according to the first article, calculating/determining available memory
@jeffjohnson9046
jeffjohnson9046 / create-saml-keystore.sh
Last active June 3, 2021 21:49
Create a SAML keystore for SSO
# Create a password-protected keystore. Change the -keypass value to a password that meets your password policy. LastPass (or some other password generator) can come in handy here for creating a password.
keytool -genkeypair -alias my-service-provider -keypass password -keyalg RSA -keysize 2048 -keystore my-sso-keystore.jks
# Use openssl to get the identity provider's public key as a file named sso.crt.
openssl s_client -connect my-sso-domain.example.com:443 > sso.crt
# Open the sso.crt file in any editor and remove everything around the BEGIN and END lines. If required, concatenate with any intermediate certificates.
vi sso.crt
# When done editing, the file should look similar to this:
@jeffjohnson9046
jeffjohnson9046 / .psqlrc
Last active August 3, 2019 18:53
A .psqlrc file gathered from various sources, for use with PostgreSQL
-- Set client encoding
\encoding UTF8
\set QUIET 1
-- print 'NULL' (instead of a blank) for any columns that have a null value
\pset null 'NULL'
-- Do not automatically commit after every statement
\set AUTOCOMMIT off
@jeffjohnson9046
jeffjohnson9046 / rename-first-char-to-uppercase.sh
Last active July 30, 2019 17:03
Rename files/directories so that the first letter is upper-case on OS X
# Assume you have a directory structure that looks something like this:
ls -alh /path/to/my/directory
drwxr-xr-x 38 jeffjohnson staff 1.2K Jul 18 11:07 ./
drwxr-xr-x 6 jeffjohnson staff 192B Jun 23 15:00 ../
drwxr-xr-x 10 jeffjohnson staff 320B Aug 28 2017 foo/
drwxr-xr-x 12 jeffjohnson staff 384B Dec 18 2017 bar/
drwxr-xr-x 11 jeffjohnson staff 352B Feb 26 2018 baz/
-rw-r--r-- 1 jeffjohnson staff 1.8M Aug 14 2017 some-update.sql
# list continues...
@jeffjohnson9046
jeffjohnson9046 / .mongrc.js
Created June 27, 2019 00:17
A prompt for the mongo shell that displays the user you're logged in as and the database you're pointed at.
/**
Set the mongo shell prompt to display [user name]@[database name]. If the user or database name cannot
be captured, then display "<none>" instead.
*/
var prompt = function() {
var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0].user || "<none>";
var dbLabel = db.getName() || "<none>";
return user + "@" + dbLabel + ">";
}
@jeffjohnson9046
jeffjohnson9046 / search-for-commits-in-git.sh
Last active May 28, 2019 17:22
Search for commits in git
# From here: https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from
# First, make sure you have everything locally:
$ git fetch --all --tags --prune
# Show the local branches a commit is on (the "b4f8b91" is the first 7 characters of the commit hash):
$ git branch --contains b4f8b91
develop
* feature/ast-3975
master