Useful Code Snippets

12/01/2021

Here are some code snippets I've collected over time. They solve common problems and I find myself coming back to them again and again.

Ghostty

font-family = SF Mono
font-thicken = true
font-size = 14
cursor-style = block
shell-integration-features = no-cursor
window-height = 40
window-width = 120
background-opacity = 0.8
background-blur-radius = 20
adjust-cell-height = 5%
minimum-contrast = 1.1
window-padding-x = 8
copy-on-select = clipboard

Command Line (CLI)

Finding Your Public IP Address

curl ifconfig.me

For a potentially faster result that doesn't rely on an external web service, you can use dig to query Google's DNS servers directly.

dig +short TXT o-o.myaddr.l.google.com @ns1.google.com

Node.js

Clearing the NPX Cache

When you use npx to run a package, it caches it to speed up future executions. Sometimes, this cache can become corrupted or you might just want to ensure you're running the absolute latest version of a package. To clear the npx cache, you can simply remove its directory.

rm -rf ~/.npm/_npx

macOS

Inspecting App Entitlements with codesign

On macOS, applications have entitlements that define what resources and capabilities they are allowed to access. You can inspect these using the codesign command-line tool. This is useful for security audits or debugging.

Here's how you can check the entitlements for the Docker application. Note that the command syntax changed slightly in macOS 12.0.

For macOS versions before 12.0:

codesign -dvvvvv --entitlements :- /Applications/Docker.app

For macOS 12.0 and newer:

codesign -dvvvvv --entitlements - /Applications/Docker.app

Docker

A Complete Docker Cleanup

If you want to reset your Docker environment to a clean slate, you'll need to stop and remove all containers, images, volumes, and networks. These commands will wipe your local Docker setup completely, so use them with caution!

# Stop all running containers
docker stop $(docker ps -qa)

# Remove all containers (stopped or running)
docker rm $(docker ps -qa)

# Remove all Docker images
docker rmi -f $(docker images -qa)

# Remove all Docker volumes
docker volume rm $(docker volume ls -qf)

# Remove all custom Docker networks
docker network rm $(docker network ls -q)

# A more direct way to prune volumes
docker volume prune -f

After running these, your Docker installation should be pristine. You can verify by running docker ps -a, docker images -a, and docker volume ls, which should all return empty lists.

Upgrading Docker Compose Services

To update the images used by your services in a docker-compose.yml file to their latest versions, you can use docker-compose. First, pull the latest images, then restart the services with the new images.

Source: Stack Overflow

# Pull the latest versions of all images in the compose file
docker-compose pull

# Recreate services with the new images
docker-compose up -d --remove-orphans

# Optionally, you can clean up old, unused images
docker image prune

Enabling BBR for Better Network Performance

BBR is a TCP congestion control algorithm developed by Google that can significantly improve network throughput and latency. Here’s how you can enable it on a Linux-based system (like a server or a VM).

First, verify which congestion control algorithms are available.

sysctl net.ipv4.tcp_available_congestion_control

Next, you may need to install a newer kernel.

sudo apt install --install-recommends linux-generic-hwe-` # Press Tab to see options `

Then, edit the sysctl.conf file to enable BBR by default.

sudo vi /etc/sysctl.conf

Add these lines to the file:

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

Finally, apply the changes and reboot the system for them to take effect.

sudo sysctl -p
reboot