Skip to content

Instantly share code, notes, and snippets.

@albertywu
albertywu / unix.sh
Last active August 13, 2019 04:50
unix toolbelt cheatsheet
# filter rows in a file by line number / position
awk
# filter rows in a file by regex
grep
# filter cols in a file
awk
# transform rows by regex
@albertywu
albertywu / bash.md
Last active May 21, 2020 20:51
bash toolkit

Map

replaces lines with a substring match of that line (capture groups)

sed -E 's/regex/string/'

echo "a secret: 12345" | sed -E 's/a secret: (.*)/\1/'

(outputs 12345)

read plist -> xml file

defaults read -app Terminal | plutil -convert xml1 -o plist.xml -r -- -

/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
if (matrix.length === 0) return false
let filteredRows = getFilteredRows(matrix, target)
let filteredCols = getFilteredCols(matrix, target)
@albertywu
albertywu / serdesTree.md
Last active May 26, 2020 04:55
serialize / deserialize a binary tree
/**
 * Encodes a tree to a single string.
 *
 * @param {TreeNode} root
 * @return {string}
 */
var serialize = function(root) {
  let result = []
 
#!/usr/bin/env bash
# --- NORMAL-MODE BENCHMARKS ---
echo "--- web-code-base normal ---"
docker system prune --all --force
docker pull 027047743804.dkr.ecr.us-west-1.amazonaws.com/web-code-base@sha256:ddfb3a6aecf3fa9fbafceb600f5d670efed472196b0dc405fc23b897a2a100d1
time IMAGE="web-code-base:normal-build" docker-compose --file tools/ci/scripts/docker/docker-compose-base.yml build web-code-base
echo "--- web-code normal ---"
docker system prune --all --force
@albertywu
albertywu / pre-push
Created June 3, 2020 23:07
git hook that enforces that you've rebased origin/master before pushing
#!/bin/sh
# Called by "git push" after it has checked the remote status,
# but before anything has been pushed.
# If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
#!/bin/bash
# reproduces this failure locally: https://buildkite.com/uber/web-code-runner/builds/3499312#49338f5b-c4e8-4d4b-8b60-c2961f22657f
function docker_login_ecr {
local DOCKER_CREDENTIAL_UBER_REGISTRY="027047743804.dkr.ecr.us-west-1.amazonaws.com"
aws --region=us-west-1 ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_CREDENTIAL_UBER_REGISTRY
}
docker_login_ecr # so we can pull down image from buildkite
@albertywu
albertywu / errors.go
Last active January 31, 2021 00:29
error handling in golang
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
@albertywu
albertywu / dockertricks.sh
Last active March 2, 2021 15:56
docker tricks
# run a command inside a docker container
docker run --rm node ls
# run multiple commands inside a docker container
docker run --rm node bash -c "ls && pwd"
# pipe commands from stdin
echo "ls && pwd" | docker run --rm -i node bash
# pipe commands from script