Skip to content

Instantly share code, notes, and snippets.

View edr3x's full-sized avatar
🦥
Naffing

Anuj Dhungana edr3x

🦥
Naffing
View GitHub Profile
@edr3x
edr3x / container-cpu-usae.sh
Created July 3, 2024 04:34
Get absolute CPU usae of container
#!/usr/bin/env bash
if [ -z "$1" ]; then
echo "Usage: $0 <container-id>"
exit 1
fi
CONTAINER_ID=$1
CPU_PERCENT=$(docker stats $CONTAINER_ID --no-stream --format "{{.CPUPerc}}" | tr -d '%')
create or replace function gen_uuidv7() returns uuid as $$
declare
begin
return gen_uuidv7(clock_timestamp());
end $$ language plpgsql;
create or replace function gen_uuidv7(p_timestamp timestamp with time zone) returns uuid as $$
declare
v_time numeric := null;
v_unix_t numeric := null;
@edr3x
edr3x / base64url.sh
Created May 25, 2024 06:01
url friendly base64 encoder and decoder
#!/usr/bin/env bash
encode() {
local input="$1"
if [ -z "$input" ]; then
input=$(cat)
fi
echo -n "$input" | base64 -w0 | tr '+/' '-_' | tr -d '='
}
@edr3x
edr3x / isValidDomain.go
Created May 17, 2024 10:43
Utility function to check if the domain is valid or not
package main
import (
"fmt"
"regexp"
"golang.org/x/net/publicsuffix"
)
func isRootDomain(domain string) (bool, error) {
@edr3x
edr3x / uuidv7_timestamp_extrator.md
Created April 19, 2024 08:19
Extract timestamp from UUIDv7 string

Code

func extractTimestamp(uuid string) (time.Time, error) {
	parts := strings.Split(uuid, "-")
	millisecondsStr := parts[0] + parts[1]
	milliseconds, err := strconv.ParseInt(millisecondsStr, 16, 64)
	if err != nil {
		return time.Time{}, err
	}
@edr3x
edr3x / KubeImageRegistry.md
Last active April 13, 2024 19:43
Deploy your own container registry on k3s for local development and debugging

Local Container Image Registry in K3S

1. Add reg.local on your /etc/hosts file

2. Create k8s namespace

kubectl create namespace registry
@edr3x
edr3x / local-registry.md
Created April 12, 2024 20:20
Make your local docker images available to k3s

Note:
There may be better way but this is the way i do it

First download and run registry service using docker

docker run -d -p 5000:5000 --restart always --name registry registry:2

Tag your local docker image

@edr3x
edr3x / Makefile
Created March 22, 2024 17:35
Rabbitmq with docker setup
PHONY: up down
up:
mkdir -p mq-vol
sudo chown -R 1001 mq-vol
docker compose up --build --detach
down:
docker compose down
exit 0
@edr3x
edr3x / is_power.go
Created February 16, 2024 11:39
Function to check if a number is a power of another number in GO
func isPower(number int, isPowerOf int) bool {
if number < 1 {
return false
}
if number%isPowerOf == 0 {
return isPower(number/isPowerOf, isPowerOf)
}
return number == 1
}
@edr3x
edr3x / dctx.sh
Created February 2, 2024 14:28
Switch Docker contexts easily with fzf
#!/usr/bin/env bash
contexts=$(docker context ls | awk '{if ($1 != "NAME") print $1}')
selected_option=$(printf "%s\n" "${contexts[@]}" | fzf --prompt="Select docker contexts: " --height=~50% --layout=reverse --border --exit-0)
docker context use $selected_option