Skip to content

Instantly share code, notes, and snippets.

View midwire's full-sized avatar
🏠
Working from home

Chris Blackburn midwire

🏠
Working from home
View GitHub Profile
@midwire
midwire / tor.server.sh
Created July 10, 2014 14:02
TOR service control script
#!/usr/bin/env bash
cmd=$1
function start_tor() {
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.tor.plist
}
function stop_tor() {
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.tor.plist
@midwire
midwire / varnish.server.sh
Created July 10, 2014 14:02
Varnish service control script
#!/usr/bin/env bash
cmd=$1
PROG=/usr/local/opt/varnish3/sbin/varnishd
VCL=/usr/local/etc/varnish/default.vcl
OPTS="-n /usr/local/var/varnish -f $VCL -s malloc,1G -T localhost:6082 -a :6081"
function start_varnish() {
echo "Running [$PROG $OPTS]"
$PROG $OPTS
@midwire
midwire / bulk_file_rename
Last active August 29, 2015 14:04
bulk_file_rename
#!/usr/bin/env ruby
# A Ruby bulk file renaming script
require 'fileutils'
require 'pry'
require 'colored'
require 'trollop'
class BulkFileRename
include FileUtils
@midwire
midwire / install_ruby
Created July 30, 2014 15:01
Script for ruby-install
#!/usr/bin/env bash
# Script for ruby-install
build=`which ruby-install`
rubies="$HOME/.rubies"
if [[ ! -x $build ]]; then
echo "'brew install ruby-install' before using install_ruby"
exit 1
fi
@midwire
midwire / cherry_pick_external.rb
Last active August 29, 2015 14:05
Cherry Pick commits from an external repository
#!/usr/bin/env ruby
require 'pry'
require 'colored'
require 'trollop'
class CherryPickExternal
attr_accessor :external_repo, :sha, :elapsed, :color_output, :test_run
def self.run(*args)
start_time = Time.now
@midwire
midwire / hash.rb
Created August 19, 2014 01:10
Better Ruby Hash
class Hash
def grep(pattern)
inject([]) do |res, kv|
res << kv if kv[0] =~ pattern or kv[1] =~ pattern
res
end
end
# Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
def except(*keys)
@midwire
midwire / array.rb
Created August 19, 2014 01:12
Better Ruby Array
class Array
def count_occurrences
k = Hash.new(0)
self.each{|x| k[x] += 1}
k
end
def randomize
self.sort_by { rand }
end
@midwire
midwire / stat.rb
Created August 19, 2014 01:14
Get the device name where a file resides
class File
class Stat
def self.device_name(file)
Dir['/dev/*'].inject({}) { |h, v|
h.update(File.stat(v).rdev => v)
}.values_at(File.stat(file).dev).first || nil
end
end
@midwire
midwire / fixnum.rb
Created August 19, 2014 01:17
Enhanced Ruby Fixnum class
class Fixnum
# Format a number with commas and a decimal point
def commify
to_s =~ /([^\.]*)(\..*)?/
int, dec = $1.reverse, $2 ? $2 : ""
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
end
int.reverse + dec
end
@midwire
midwire / rake_helper.rb
Created August 19, 2014 01:18
Automate gem releasing and versioning
require 'thor'
require 'midwire_common'
module MidwireCommon
# RakeHelper helps to automate gem release and versioning tasks
class RakeHelper
include Rake::DSL if defined? Rake::DSL
def self.install_tasks(opts = {})