Skip to content

Instantly share code, notes, and snippets.

View leite08's full-sized avatar

Rafael Leite leite08

View GitHub Profile
@leite08
leite08 / aws-ecr-calculate-storage.sh
Created July 19, 2024 03:56
Calculate the storage used by an ECR repository
#!/bin/bash
# Based on https://stackoverflow.com/a/78081105/2099911
ECR_REPO=...
REGION=...
total_size=0
echo "Retrieving image details for $ECR_REPO..."
image_details=$(aws ecr describe-images --repository-name $ECR_REPO --region $REGION --no-cli-pager)
echo "Done; calculating the total size..."
for row in $(echo "${image_details}" | jq -c '.imageDetails[]'); do
@leite08
leite08 / sentry-raw.sh
Created June 18, 2024 17:16 — forked from ezy/sentry-raw.sh
CURL request to sentry without an SDK
# Replace <sentry_key>, <sentry-url> and <project-id> to formulate your CURL request. Don't forget to add a trailing slash to URL otherwise you'll get wierd CSRF errors!
curl -X POST --data '{ "exception": [{ "type": "$ErrorMessage"}] }' -H 'Content-Type: application/json' -H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=<sentry_key>, sentry_client=raven-bash/0.1" https://<sentry-url>/api/<project-id>/store/
@leite08
leite08 / aws-ecr-add-tag.sh
Last active April 16, 2024 03:00
AWS ECR - add tag to an image by tag
# This script adds a tag to an already tag ECR Image
ECR_REPO=...
CURR_TAG=...
NEW_TAG=latest
# Can replace imageTag="$CURR_TAG" by imageDigest="sha256:..." if the image doesn't have a tag
MANIFEST=$(aws ecr batch-get-image --repository-name $ECR_REPO --image-ids imageTag="$CURR_TAG" --output text --query 'images[].imageManifest')
aws ecr put-image --repository-name $ECR_REPO --image-tag $NEW_TAG --image-manifest "$MANIFEST"
@leite08
leite08 / insert-random.sql
Last active March 19, 2024 03:24
Populate Postgres table w/ random data
insert into table (id, another, jsonb_data)
with data as (
select i
from generate_series(1, 1000) as g(i)
)
select
gen_random_uuid()::text,
'<insert-id-here>',
('{"dob": "' || timestamp '1940-01-01 20:00:00' + random() * (timestamp '2020-01-01 10:00:00' - timestamp '1940-01-01 20:00:00') || '", "lastName": "last' || i || '", "firstName": "first' || i || '"}')::json
from data;
@leite08
leite08 / server.ts
Created March 3, 2024 18:26
Express server to log text payloads
import express, { Application, Request, Response } from "express";
const port = 8088;
const app: Application = express();
app.use(express.text({ type: "*/*" }));
app.post("/", (req: Request, res: Response) => {
console.log(`>>> HEADERS: ${JSON.stringify(req.headers, undefined, 2)}`);
console.log(`>>> BODY: ${String(req.body)}`);
@leite08
leite08 / db-conn.ts
Created March 1, 2024 06:10
Typescript code to time creating connections with Postgres
import { Client } from "pg";
type Result = {
connect: number;
close: number;
total: number;
};
let results: Result[] = [];
@leite08
leite08 / load-test.sh
Created March 27, 2023 18:17
Simple shell-based load testing script
#!/bin/bash
# Inspired by https://gist.github.com/bigomega/4de7dbc395be0e0f33b4
# TODO: allow multiple headers
# TODO: call curl with optional parameters so the code is not duplicated (DRY)
############################################################
# Help #
############################################################
Help()
@leite08
leite08 / untilfail.sh
Created March 23, 2023 21:25
Shell script to run a command until it fails
#!/bin/bash
count=0
while "$@"; do (( count++ )); done && echo "It failed on the $count iteration"
@leite08
leite08 / post-merge
Created February 27, 2023 16:55
Husky git hook to notify about changes on files/folders upon merge
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# Place this on ./husky and make sure the file is executable (chmod 770 post-merge)
changed() {
git diff --name-only HEAD@{1} HEAD | grep "$1" > /dev/null 2>&1
}
echo "Checking updates on <file-type> files..."
@leite08
leite08 / clean_code.md
Created March 5, 2022 23:07 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules