Skip to content

Instantly share code, notes, and snippets.

View ardislu's full-sized avatar

Ardis Lu ardislu

View GitHub Profile
@ardislu
ardislu / pre-commit
Last active November 8, 2021 05:30
git hook for a common use case: linting staged files before commit. Pipes all staged JS or TS files to eslint, and all CSS or SCSS files to stylelint. Assuming eslint and stylelint are node modules installed in the same root as the git repo.
#!/usr/bin/env bash
root="$(git rev-parse --show-toplevel)" # Full file path to the directory where this git repo sits
cd "$root" # Required to make the relative file paths to eslint and stylelint work
for file in $(git diff --name-only --cached) # List of staged files
do
filename="${file##*/}" # Remove all except the characters after the last '/'
extension="${filename##*.}" # Remove all except the characters after the last '.'
@ardislu
ardislu / cpu-stress
Last active November 8, 2021 05:31
Starts a background process that infinitely gzips random bytes and outputs to /dev/null. Good for artificially creating CPU load for testing.
#!/usr/bin/env bash
cat /dev/urandom | gzip --best >> /dev/null &
@ardislu
ardislu / hash.ps1
Last active December 11, 2021 23:38
Powershell script to hash a string.
# December 2021 update: this gist is not maintained.
# View the latest version of this function in my module: https://github.com/ardislu/ArdisUtilities
Param(
[string]$Value = '',
[string]$Algorithm = 'SHA1'
)
$stream = [System.IO.MemoryStream]::new([byte[]][char[]]$Value)
@ardislu
ardislu / pm2-ipfs
Last active November 8, 2021 05:31
Start an ipfs daemon process using pm2 with garbage collection enabled and auto-restart when 256 MB of memory is consumed.
#!/usr/bin/env bash
sudo env PATH=$PATH pm2 start "ipfs daemon --enable-gc" --name "ipfs" --max-memory-restart 256M
@ardislu
ardislu / install-legacy-google-compute-agents
Last active November 8, 2021 05:31
Download, install, and start the monitoring and logging agents for a Google Compute instance.
#!/usr/bin/env bash
# NOTE: These are legacy agents that are replaced by the Ops Agent
# https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/installation
# Download monitoring agent script, install it, and start the agent
curl -sSO https://dl.google.com/cloudagents/add-monitoring-agent-repo.sh
sudo bash add-monitoring-agent-repo.sh --also-install
sudo service stackdriver-agent start
@ardislu
ardislu / install-google-compute-agents
Last active November 8, 2021 05:32
Download and install the ops agent for a Google Compute instance.
#!/usr/bin/env bash
# Download ops agent script, install it, and check that the agent is running
curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh
sudo bash add-google-cloud-ops-agent-repo.sh --also-install
sudo systemctl status google-cloud-ops-agent"*"
@ardislu
ardislu / crontab-15
Created July 12, 2021 03:52
Example crontab line to schedule a script to execute every 15 minutes.
# Run /home/username/example.sh every 15 minutes
*/15 * * * * /bin/sh /home/username/example.sh
@ardislu
ardislu / base64encode.ps1
Created September 14, 2021 03:57
One-liner to base64 encode a string input.
[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($args[0]))
@ardislu
ardislu / uuid-hyphens.js
Created September 14, 2021 05:22
Display a UUID as five groups separated by hyphens, in the form 8-4-4-4-12.
const uuid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
uuid.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
// 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
@ardislu
ardislu / invalid-logins
Last active November 8, 2021 05:32
View logged invalid login attempts on Ubuntu.
#!/usr/bin/env bash
sudo grep 'Invalid user' /var/log/auth.log | less -N