Skip to content

Instantly share code, notes, and snippets.

View M1ke's full-sized avatar

Mike Lehan M1ke

View GitHub Profile
@M1ke
M1ke / .jq
Created February 12, 2024 20:35
~/.jq for handy jq pipes
# https://stackoverflow.com/a/28641626/518703
def decode_ddb:
def _sprop($key): select(keys == [$key])[$key]; # single property objects only
((objects | { value: _sprop("S") }) # string (from string)
// (objects | { value: _sprop("NULL") | null }) # null (from boolean)
// (objects | { value: _sprop("B") }) # blob (from string)
// (objects | { value: _sprop("N") | tonumber }) # number (from string)
// (objects | { value: _sprop("BOOL") }) # boolean (from boolean)
// (objects | { value: _sprop("M") | map_values(decode_ddb) }) # map (from object)
// (objects | { value: _sprop("L") | map(decode_ddb) }) # list (from encoded array)
@M1ke
M1ke / apigw-express.ts
Created September 21, 2022 10:46
Quick helper functions when testing local serverless applications (Typescript + AWS Lambda) to translate `express` req/res objects into the appropriate types for API Gateway
const apiGWEventFromReq = (req: Request): APIGatewayEvent => {
const [headers, multiValueHeaders] = Object.keys(req.headers).reduce((parts, key) => {
const val = req.headers[key]
if (!val) {
return parts
}
const [headers, multiValueHeaders] = parts
if (Array.isArray(val)) {
multiValueHeaders[key] = val;
} else {
@M1ke
M1ke / psalm-query.php
Created April 27, 2022 15:06
This tool processes a SQL query and generates a guess at an appropriate Psalm object-like array definition
<?php
const PSALM_STRING = ': string';
const SELECT_ALL = '*';
// These must be upper case to avoid accidental positives
const SQL_SELECT = 'SELECT';
const SQL_FROM = 'FROM';
const ARG_DEBUG = '--debug';
// This is lower case as the field names are forced to lower case
const SQL_AS = ' as ';
@M1ke
M1ke / bitbucket-pipelines.yml
Created February 14, 2022 17:49
Composer update pipeline in Bitbucket
pipelines:
# Manually triggered Pipelines
custom:
composer-update:
- step:
name: Composer Update
image: prooph/composer:<pick your version, e.g 7.4>
script:
- export COMPOSER_BRANCH="composer-update-auto-$(date +"%Y-%m-%d")"
- git checkout -b "$COMPOSER_BRANCH"
@M1ke
M1ke / markdown-to-github-pdf.sh
Created July 15, 2021 11:39
Simple command line tool using python's grip library and wkhtmltopdf to generate a PDF file from a Markdown file. Basically starts a server, captures it as a PDF and then kills the server (that's the complex bit!)
#!/bin/bash
# Install grip $ pip install grip
# Install wkhtmltopdf $ sudo apt-get install wkhtmltopdf
md=$1
pdf=$md.pdf
port=$(( ( RANDOM % 6000 ) + 5000 ))
echo $port
@M1ke
M1ke / bitbucket-prs.php
Last active July 6, 2021 15:57
Handy PHP script to examine and merge lists of BitBucket PRs
<?php
/*
* Declare the following consts:
*
* REPO_DEFAULT - Set this to avoid having to type each time
* TEAM - Find this in URLs
* USER, PASS - Bitbucket credentials
*
* Install Guzzle and require './vendor/autoload.php'
*/
@M1ke
M1ke / cloudwatch-parse-slow-query.md
Last active May 28, 2021 10:23
Example queries for parsing mysql slow query logs

Using AWS RDS we can export default MySQL logs to CloudWatch. This can include the slow query log.

As standard, CloudWatch logs will just show the log message, which allows reading the log, but not doing useful analysis, such as finding the slowest queries over a time range, or the slow query which repeats the most often.

Using CloudWatch Insights we can write custom log search queries to try and extract more information. It starts with a parse step, where the "glob" parser can be used to take a single block log message and pull out individual data points:

PARSE @message '# Time: * *\n# User@Host: *[*] @ * Id: *\n# Query_time: * Lock_time: * Rows_sent: * Rows_examined: *\n*' as date,time,user,host,ip,id,duration,lock,rows,examined,query
@M1ke
M1ke / interface-trait-inheritance.php
Created March 8, 2021 12:24
Demonstrates the use of traits and interfaces to "mock" multiple inheritance in PHP
interface AsNumber {
public function asNumber(): int;
}
interface CanBeAdded {
public function add(AsNumber $b): int;
}
interface CanBeMultiplied {
public function multiply(AsNumber $b): int;
@M1ke
M1ke / bash.md
Last active September 7, 2020 14:37
Generally useful stuff for bash

Felt I should collate useful stuff I constantly bounce between files to look up

Expand alias (when in interactive terminal)

Ctrl + Alt + e

(Ubuntu)

Exit if variable (e.g. envvar) isn't set

@M1ke
M1ke / php-yorkshire-19-workshop.md
Last active May 15, 2019 13:36
A guide to prepare for the AWS workshop on Friday 22nd Feb at PHPUK conference

In order to get the most from the workshop you'll benefit from being able to follow along on your own machine. The setup for this is not complicated but it will require you to have carried out a few tasks beforehand to save time during the workshop - we only have 2 hours after all.

All the steps below should take you no more than 30 minutes, more likely around 15! If you need help Tweet me @m1ke

You can pre-register for the session here and you'll need a code that was emailed to you with subject "PHP UK Conference - Info for Attendees"

The key aspect is this: you must have CLI access to a working AWS account with billing enabled. You will incur some costs during the workshop but if you stick to the advice given these costs are likely to be in the region of cents to a dollar at most. Be aware that once you have an AWS account with billing enabled you should do your best to keep the credentials used to access that account secure. If you are conce