Skip to content

Instantly share code, notes, and snippets.

View pda's full-sized avatar
💾
Formatting…

Paul Annesley pda

💾
Formatting…
View GitHub Profile
@pda
pda / brew-uses-gnutls.txt
Created March 4, 2014 04:15
$ brew uses gnutls | cat
bitlbee
emacs
finch
freediameter
glib-networking
gnu-smalltalk
gst-plugins-bad
gwenhywfar
hamsterdb
inspircd
@pda
pda / getopts.sh
Created March 5, 2014 02:35
getopts (bash) usage example.
#!/bin/bash
# Example: ./getopts.sh -b world -a hello one two three
# Output: A=hello B=world 1=one 2=two 3=three 4=
while getopts "a:b:" opt; do
case "$opt" in
"a") A="$OPTARG";;
"b") B="$OPTARG";;
esac
@pda
pda / args.sh
Last active August 29, 2015 13:57
Bash argument grouping
#!/bin/bash
A=("$@")
X="$@"
echo; echo '"$@": <-- correct'
for arg in "$@"; do echo "$arg"; done
echo; echo '"${A[@]}": <-- correct'
for arg in "${A[@]}"; do echo "$arg"; done
@pda
pda / base_script.rb
Created June 3, 2014 21:02
BaseScript; small base for CLI scripts; signal handling, indented logging, colors ticks/crosses, injectable args/IO.
# A base class for implementing CLI scripts.
# ARGV and in/out IO's are injected, so can be mocked & tested.
# Basic signal handling by calling exit_on_signals inside work loops etc.
# Requires Ruby 2.0.0+ for keyword args etc.
class BaseScript
EXIT_SUCCESS = 0
INDENT = " "
def initialize(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr)
@pda
pda / ec2-lvm.sh
Last active August 29, 2015 14:07
For EC2 instances with two instance volumes, use LVM to combine them into a single /mnt
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
set -x
DEVICE_B="/dev/xvdb"
DEVICE_C="/dev/xvdc"
VG_NAME="ephemeral"
LV_NAME="ephemeral"
DEVICE_LV="/dev/$VG_NAME/$LV_NAME"
@pda
pda / race.rb
Created November 18, 2014 01:53
thumbor_aws async/blocking debugging.
#!/usr/bin/env ruby
require "open-uri"
require "benchmark"
def main
urls = ARGV
results = Queue.new
class A
def with(&block)
instance_eval(&block)
end
private
def private_blarg
puts 'this is from a private method'
end
end
#!/usr/bin/env ruby
# Contrived example of self-pipe preventing signal race condition prior to select()
# @see http://cr.yp.to/docs/selfpipe.html
# @author Paul Annesley
SELF_READ, SELF_WRITE = IO.pipe
@run = true
trap :INT do
#!/usr/bin/env ruby
#
# Are you one of the 10% of programmers who can write a binary search?
# http://reprog.wordpress.com/2010/04/19/are-you-one-of-the-10-percent/
def bin_search(search, subject)
discarded_left = 0
while subject.any?
#!/bin/bash
# Attempts to fast-forward the current local branch to its remote tracked branch.
# Safely refuses if there is no remote tracking, or if a fast-forward is not possible.
# @author Paul Annesley
if [ -n "$(git merge --ff-only 2>&1 | grep 'unknown option')" ]; then
echo "Your git doesn't seem to support --ff-only... try git 1.7+"
exit 1
fi