Nullish coalescing operator and empty string

This was an annoying lesson to learn.

An empty string is not nullish it is falsy.
Nullish is not the same as falsy.

const x = '';

const a = x ? x : 'abc'; // a = 'abc'
const b = x ?? 'abc'; // b = '', b != 'abc'

In the above example I expected constant B to be ‘abc’ but since an empty string is not nullish and ?? checks for nullish values it is not.

Call forwarding with Telus

I will be travelling to the USA soon and because I am cheap, I have setup a complicated strategy to redirect my phone calls to a VoIP number to avoid paying Telus’s $12/day roaming fee.

This requires setting up call forwarding with Telus.

Here are the steps to forward all call types to my VoIP number.

In case it is not obvious 1234567890 should be replaced with the number you want your calls forwarded to.

Activation:

*21*1234567890#

Deactivation

#21#

Restrict AWS user access to a single S3 bucket

I need S3 storage for a number of use cases. I typically create a new IAM user for each use. What I don’t like is that the default S3FullAccess policy unsurprisingly gives that user full access to ALL of my S3 buckets.

While this might be fine if the user will be for my local machine’s AWS CLI package it is not acceptable for a service like a plugin that backs up a WordPress website to S3. If the WordPress site is ever compromised I could lose all my data in all my S3 buckets.

Fortunately it is relatively simple to restrict an IAM user’s S3 access to a single bucket.

Here is the policy I am using from here, which I can confirm does work. (Something I cannot say for the official Amazon article on the same subject.)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                        "s3:GetBucketLocation",
                        "s3:ListAllMyBuckets"
                      ],
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::YOUR-BUCKET-NAME",
                "arn:aws:s3:::YOUR-BUCKET-NAME/*"
            ]
        }
    ]
}

Script to quickly create a Divi child theme

A best practice in WordPress is to never modify a third-party theme directly. Instead any modification you need to make should be done inside of a child theme.

The same applies when using Divi (affiliate link).

Here is a quick bash script that will simplify this process.

Note: The commands and theme images are from here.

I used the WordPress droplet from Digital Ocean (affiliate link) to test this script so you may need to adjust the script depending on where your site’s web root is located.

#!/bin/bash
cd /home/becauseiforgetthings/htdocs/becauseiforgetthings.com/wp-content/themes
mkdir divi-child
cd divi-child
cat <<EOT > style.css
/*
 Theme Name:     Divi Child
 Theme URI:      https://www.elegantthemes.com/gallery/divi/
 Description:    Divi Child Theme
 Author:         Elegant Themes
 Author URI:     https://www.elegantthemes.com
 Template:       Divi
 Version:        1.0.0
*/
EOT
cat <<EOT > funcitons.php
<?php
function my_theme_enqueue_styles() { 
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
EOT
wget https://www.elegantthemes.com/blog/wp-content/uploads/2018/02/screenshot-copy.png
mv screenshot-copy.png screenshot.png
cd

Import and Export MySQL and PostgreSQL databases inside Docker containers

I need to dump databases just infrequently enought that I have not memorized the correct commands yet.

PostgreSQL

Export

docker exec -t DB_CONTAINER_NAME pg_dumpall -c -U postgres > dump.sql

Import

cat dump.sql | docker exec -i DB_CONTAINER_NAME psql -U postgres

MySQL

Export

docker exec DB_CONTAINER_NAME /usr/bin/mysqldump -u root --password=PASSWORD DATABASE > backup.sql

Import

cat backup.sql | docker exec -i DB_CONTAINER_NAME /usr/bin/mysql -u root --password=PASSWORD DATABASE