Pragmatism in the real world

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.

Extracting the base name of a file in Bash

I have a handy bash script that transcodes videos using Don Meton's video_transcoding tools. This script was written in a hurry and one limitation it had was that it re-transcoded any source file even if the output file already existed. The script looked like this: #!/usr/bin/env bash readonly source_dir="${1:-MKV}" readonly output_dir="${2:-MP4}" for file in "$source_dir"/*.mkv; do ./transcode.sh "$file" "$output_dir" done What it should do is only run transcode.sh if the output file doesn't exist, so… continue reading.

Using Docker to create a MySQL server

When working on test code on my computer, I usually use the built-in PHP server (php -S) which works nicely. Every so often, I need access to MySQL and I use Docker to temporarily create a MySQL server for me. This is how I do it. The magic command is: $ docker run –name mysql \ -e MYSQL_USER=rob -e MYSQL_PASSWORD=123456 -e MYSQL_DATABASE=bookshelf \ -p 3306:3306 -d mysql/mysql-server:5.7 This creates a Docker container called "mysql" on… continue reading.

Getting started writing an Alexa Skill

We now have 4 Amazon Echo devices in the house, and, inspired by a demo LornaJane gave me at DPC, I have decided to write some skills for it. This article covers what I learnt in order to get my first Swift skill working. Our bins are collected by the council every other week; one week it's the green recycling bin and the other week, it's the black waste bin. Rather than looking it up,… continue reading.

Use curl to create a CouchDB admin user

This too me longer to find than it should have done, so I'm writing it here for future me. When you install CouchDB, it is in a mode where anyone can do anything with the database including creating and deleting databases. This is called "Admin Party" mode which is a pretty cool name, but not what I want. Creating admin users To create a user in 1.6 (I've not used 2.0 yet, but assuming it's… continue reading.

View an SSL certificate from the command line

I recently had some trouble with verifying an SSL in PHP on a client's server that I couldn't reproduce anywhere else. It eventually turned out that the client's IT department was presenting a different SSL certificate to the one served by the website. To help me diagnose this, I used this command line script to display the SSL certificate: getcert.sh #!/bin/bash echo | openssl s_client -showcerts -servername !$ -connect $1:443 2>/dev/null \ | openssl x509… continue reading.

View header and body with curl

I recently discovered the -i switch to curl! I have no idea why I didn't know about this before… Curl is one of those tools that every developer should know. It's universal and tends to be available everywhere. When developing APIs, I prefer to use curl to view the output of a request like this: $ curl -v -H "Accept: application/json" https://api.joind.in/ * Trying 178.208.42.30… * Connected to api.joind.in (178.208.42.30) port 443 (#0) * TLS… continue reading.