Skip to content

Instantly share code, notes, and snippets.

Avatar
😁

Oliver Tappin olivertappin

😁
View GitHub Profile
@olivertappin
olivertappin / cookies-enabled.js
Created Oct 13, 2020
Detect whether cookies are enabled in the browser
View cookies-enabled.js
function cookiesEnabled() {
if (navigator.cookieEnabled) {
return true;
}
document.cookie = 'cookietest=1';
let enabled = document.cookie.indexOf("cookietest=") !== -1;
document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
return enabled;
}
@olivertappin
olivertappin / extract.sh
Last active Sep 17, 2020
Extract a string from a commit message between two square brackets
View extract.sh
# Example with a space after the colon
echo "bc3977f Some beautiful long commit message [skip: verification]" | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
verification
# Example with no space after the colon
echo "bc3977f Some beautiful long commit message [skip: verification]" | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
verification
# Working example from the last commit message
git log -1 --oneline | sed -En 's/.*\[skip\:(.*|\ .*)\]/\1/p' | awk '{$1=$1;print}'
@olivertappin
olivertappin / docker-exec.sh
Created Jun 9, 2020
docker exec using the Docker Engine API with /var/run/docker.sock and curl
View docker-exec.sh
#!/bin/sh
# Why? In a containerised environment, you may need to run commands on other Docker containers
# within the Docker bridge network. For slim images such as Alpine-based distributions, you do
# not want the docker executable installed. Instead, you can simply pass the docker socket during
# runtime, and interact with it using cURL. The following functions allow you to easily interact
# with other containers, as you would using the native docker exec command.
# Example of mounting the docker socket:
#
@olivertappin
olivertappin / versioning-ranges.txt
Last active Apr 8, 2020
Semantic Versioning Ranges
View versioning-ranges.txt
For a simplistic view on what versioning ranges look like, see below:
Version Range
>=1.0 1.0.0 - *.*.*
>=1.0 <2.0 1.0.0 - 1.*.*
>=1.0 <1.1 || >=1.2 1.0.0 - 1.*.* || 1.2.0 - *.*.*
>=1.0 <1.1 1.0.0 - 1.*.*
>=1.0.0 <2.1 1.0.* - 2.0.*
>=1.0.0 <=2.1.0 1.0.0 - 2.1.0
~1.2 1.2.0 - 1.*.*
@olivertappin
olivertappin / install-xscreensaver.sh
Last active Jan 14, 2020
Install xscreensaver on Ubuntu
View install-xscreensaver.sh
sudo apt-get install \
xscreensaver \
xscreensaver-gl-extra \
xscreensaver-data-extra \
xscreensaver-screensaver-bsod
# Or, via the preferred method:
# Look for the latest version from https://www.jwz.org/xscreensaver/download.html
sudo apt-get install \
xorg-dev \
@olivertappin
olivertappin / wordpress.conf
Created Jan 4, 2020
WordPress Nginx configuration
View wordpress.conf
location / {
try_files $uri /index.php$is_args$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ /\. {
deny all;
}
@olivertappin
olivertappin / collect-cpu-stats
Created Nov 27, 2019
Collect CPU usage statistics for the top 5 processes over a 60 second period
View collect-cpu-stats
#!/bin/bash
OUTPUT=~/stats.txt;
for i in {0..120};
do date >> $OUTPUT;
ps -Ao user,pid,pcpu,pmem,comm --sort=-pcpu | head -n6 >> $OUTPUT;
echo "" >> $OUTPUT;
sleep 0.5;
done
@olivertappin
olivertappin / show-blocking-transactions.sql
Created Nov 16, 2019
Run this query to check for a blocking transaction
View show-blocking-transactions.sql
SELECT
trx_w.trx_id AS waiting_trx_id,
trx_w.trx_mysql_thread_id AS waiting_process_id,
TIMESTAMPDIFF(SECOND, trx_w.trx_wait_started, CURRENT_TIMESTAMP) AS wait_time,
trx_w.trx_query AS waiting_query,
l.lock_table AS waiting_table_lock,
trx_b.trx_id AS blocking_trx_id,
trx_b.trx_mysql_thread_id AS blocking_process_id,
CONCAT(pl.user, '@', pl.host) AS blocking_user,
pl.command,
@olivertappin
olivertappin / database-table-counts.sql
Created May 10, 2019
Show counts of all table rows from a particular database using information_schema
View database-table-counts.sql
SELECT
TABLE_NAME AS DATABASE_NAME,
FORMAT(TABLE_ROWS, 0) AS Rows
FROM information_schema.tables
WHERE
table_schema = 'DATABASE_NAME'
AND TABLE_ROWS > 0
;
@olivertappin
olivertappin / column-entry-creation.sql
Created Mar 26, 2019
Return the column entry for a SHOW CREATE TABLE (based on primary key or column name)
View column-entry-creation.sql
SELECT RTRIM(CONCAT(
'`',
COLUMN_NAME,
'`',
' ',
COLUMN_TYPE,
' ',
IF (COLLATION_NAME IS NULL, '', CONCAT('COLLATE ', COLLATION_NAME, ' ')),
IF (IS_NULLABLE = 'YES', '', 'NOT NULL '),
IF (COLUMN_DEFAULT IS NULL, '', CONCAT('DEFAULT ', COLUMN_DEFAULT, ' ')),