Skip to content

Instantly share code, notes, and snippets.

@whylom
whylom / gist:1448530
Created December 8, 2011 20:53
Ruby method that shells out to git describe and formats results
def get_tag_info(commit)
info = `git describe --tags #{commit}`.chomp
if info[/(.*)-(\d+)-g([a-z0-9]{7})/]
matches = Regexp.last_match
last_tag = matches[1]
commits_since_tag = matches[2]
noun = (commits_since_tag.to_i == 1) ? 'commit' : 'commits'
"#{last_tag} + #{commits_since_tag} #{noun}"
else
@whylom
whylom / tunnel.sh
Created March 8, 2012 14:49
Open an SSH tunnel to a remote MySQL server
# Open an SSH tunnel to port 3306 (default MySQL port) on a remote machine.
# Specify the local port with an optional 2nd argument.
# The default is 3307, in case you have MySQL running locally on 3306.
#
# $ tunnel user@host
# -> opens tunnel to host on port 3306, using local port 3307
#
# $ tunnel user@host 3333
# -> opens tunnel to host on port 3306, using local port 3333
@whylom
whylom / gist:2127639
Created March 19, 2012 22:19
Snippet to get a stack trace string from an Exception
"#{error.message}\n#{error.backtrace.join("\n")}"
@whylom
whylom / gist:2139733
Created March 20, 2012 18:53
Grep an array of words (with captures!)
words = ["Life", "Liberty", "and", "the", "pursuit", "of", "Happiness"]
words.grep(/^[A-Z]/)
#=> ["Life", "Liberty", "Happiness"]
words.grep(/^([A-Z])/) { $1 }
#=> ["L", "L", "H"]
@whylom
whylom / blink.js
Created April 3, 2012 18:22
How to start and stop a light from blinking.
@whylom
whylom / gist:2322673
Created April 6, 2012 20:23
Dump a MySQL schema without the data.
mysqldump --no-data -u root -ppw funky_database > funky_database.sql
@whylom
whylom / gist:2322771
Created April 6, 2012 20:39
How to access a remote MySQL server via an SSH tunnel

For security reasons, many hosting providers restrict external access to the default MySQL port 3306. But if you have SSH access to the database server, you can access the MySQL server from your local machine by using an SSH tunnel. An SSH tunnel forwards bits back & forth between a port on your machine and a port on the remote machine, all lovingly encrypted with SSH.

Here's how you do it! But first, my assumptions:

  1. You already have configured SSH access to the remote server.
  2. You want to access port 3306 on the remote server.
  3. You want to access port 3307 on your local machine. This is simply because many devs (myself included) routinely have MySQL running on port 3307.

Here is the Unix command to open an SSH tunnel to hostname_or_ip with the user account username

@whylom
whylom / gist:2361395
Created April 11, 2012 18:58
Flavors of Ruby string interpolation
# First, some variables!
a, b = %w(foo bar)
# Good old-fashioned string interpolation.
"#{a} is not #{b}" #=> "foo is not bar"
# Today, I learned that this craziness works!
"%s is not %s" % [a, b] #=> "foo is not bar"
@whylom
whylom / gist:2431034
Created April 20, 2012 18:57
Count the files and lines of code in a directory tree.
# Shell out to the fast and reliable `wc` utility.
def lines(path)
# output of wc (returned by backticks) is formatted like this:
# " 111 filename.ext"
`wc -l #{path}`.strip.split(' ').first.to_i
end
# **/* gets all files and directories in all subdirectories.
files = Dir["/path/to/directory/**/*"]
@whylom
whylom / gist:2432236
Created April 20, 2012 22:07
leggo: idea for a utility to kill a process that's using a specific port
$ leggo 3306
Port 3306 is being used by "mysqld"
process 66117 | started 4:53PM | up for 0:01.25
/usr/local/Cellar/mysql/5.5.20/bin/mysqld
--basedir=/usr/local/Cellar/mysql/5.5.20
--datadir=/usr/local/var/mysql
--plugin-dir=/usr/local/Cellar/mysql/5.5.20/lib/plugin