Pragmatism in the real world

Building Go binaries for different platforms

One nice thing about Go is that it can cross-compile which allows me to use my Mac to build binaries for different operating systems. This is increibly useful for providing binaries for Rodeo, my command line Flickr uploader. To do this we set the GOOS and GOARCH environment variables before calling go build. For example to build for x64 on Linux, I do: version=`git describe –tags HEAD` env GOOS=linux GOARCH=amd64 go build \ -ldflags "-X… continue reading.

Setting the version of a Go application when building

When I was building Rodeo, my command line Flickr uploader, one thing I wanted to do was set the version number that you see when you type rodeo -v to the correct version when I build the application for release. The basic process I wanted was: Tag git repository with new version number and push Build rodeo with that version without editing any files Upload binary I've emphasised the important part. I don't want to… continue reading.

Only display body on error with curl

Sometimes, I only want to display the response from a curl request if it errors – i.e. the status code is 400 or higher. With curl 7.76 or greater this is done using the –fail-with-body parameter: curl -s –fail-with-body \ https://api.example.com/foo/${id} \ -H "Content-Type: application/json" \ –data "@data.json" On success, the return code ($?) is 0 and on failure the body is output with the return code set to 22. Prior to 7.76, you can… continue reading.

Quick script to (re)create my python virtualenv

When working on rst2pdf, I use pyenv as I've written about before. Recently, I've found myself needing to recreate virtualenvs for various Python versions and then recreate them to easily reset the list of packages installed into them via pip. To my my life easier, I created a script that I can run from the command line: $ newenv rst2pdf-dev-py3.9 3.9.5 This creates a new virtualenv called rst2pdf-dev-py3.9 from Python version 3.9.5, upgrade pip and… continue reading.

A few Git tips

I don't do that much that's clever with git, but I've found the following helpful. Automatically prune When you do a git fetch or git pull, you can ask it to remove remote tracking branches for a branch that has been removed on the remote by using the –prune flag. This can be automated globally with: git config –global fetch.prune true and if you only want it for a specific repository, you can use: git… continue reading.

Using MailHog via Docker for testing email

I recently needed to modify the emails that a client project sends out. It was set up to send via SMTP and so the easiest way to do this for me was to add a local MailHog instance and point the application at it. Manually running via Docker The quickest and easiest way to do this is via Docker. Manually, we can do: $ docker run -p 8025:8025 -p 1025:1025 mailhog/mailhog This will run MailHog… continue reading.

Recursive PHP lint

There are many scripts that recursively execute php -l on a set of files or directories. This is mine: #!/usr/bin/env bash set -o nounset # Recursively call `php -l` over the specified directories/files if [ -z "$1" ] ; then printf 'Usage: %s …\n' "$(basename "$0")" exit 1 fi ERROR=false SAVEIFS=$IFS IFS=$'\n' while test $# -gt 0; do CURRENT=${1%/} shift if [ ! -f $CURRENT ] && [ ! -d $CURRENT ] ; then echo… continue reading.

Prevent an external drive from auto mounting on macOS

I have an external drive attached to the USB hub on my Thunderbolt Display that I use to clone my laptop's internal drive every night using the schedule feature of SuperDuper!. At the appointed time, SuperDuper! will mount the external drive, clone the internal drive and then unmount the external drive again which is very convenient. However, whenever I plug in my laptop, the drive automatically mounts. This doesn't particularly worry me, but when I… continue reading.

Ignoring mass reformatting commits with git blame

I've recently merged a PR by Stephen to rst2df that reformats the entire codebase to align with PEP 8. As rst2pdf is over a decade old, this has resulted in a lot of changes to the files which now have Stephen's name attached. This affects git blame. For example: git blame -L 137,145 rst2pdf/findfonts.py e753c71eb (roberto.alsina 2008-09-05 14:49:24 +0000 137) d56705e4f (roberto.alsina 2009-10-30 15:07:05 +0000 138) # So now we have a font we know… continue reading.

Turn off foreign key checks when restoring a mysql file dump

I recently received a MySQL dump file where the various tables in it had foreign keys to each other. I usually restore with this command: mysql –login-path=rob < dump.sql but this generated the error: ERROR 1217 (23000) at line 288805: Cannot delete or update a parent row: a foreign key constraint fails It turns out that the easiest way to solve this is to use the –init-command switch to set foreign keys off for this… continue reading.