Skip to content

Instantly share code, notes, and snippets.

View mohd-akram's full-sized avatar
💼
Looking for work

Mohamed Akram mohd-akram

💼
Looking for work
View GitHub Profile
@mohd-akram
mohd-akram / console.js
Created June 10, 2024 11:48
A custom Node.js console that writes to stderr and the debug console
const inspector = require("inspector");
// See:
// https://github.com/nodejs/node/blob/v22.2.0/lib/internal/util/inspector.js#L83
// Wrap a console implemented by Node.js with features from the VM inspector
function wrapConsole(console) {
const inspectorConsole = inspector.console;
for (const key of Object.keys(inspectorConsole)) {
// If the console has the same method as the inspector console,
@mohd-akram
mohd-akram / main.c
Created May 30, 2024 09:01
Execute C files directly
//usr/bin/cc -o ${o=`mktemp`} "$0" && exec -a "$0" "$o" "$@"
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%s", argv[0]);
for (int i = 1; i < argc; i++)
printf(" %s", argv[i]);
printf("\n");
return 0;
@mohd-akram
mohd-akram / create-postfix
Last active May 13, 2024 16:58
Create a local Postfix instance
#!/bin/sh
set -eux
# Enable multi-instance operation
sudo postmulti -e init
mkdir -p ~/.config ~/.local/state/spool
# Create a new local instance
@mohd-akram
mohd-akram / forward
Created May 12, 2024 12:52
Forward a port from a server using SSH
#!/bin/sh
# usage: forward host port [localport]
forward() {
ssh -NL ${3-$2}:localhost:$2 $1
}
@mohd-akram
mohd-akram / lsopen
Created May 10, 2024 16:56
List open TCP and UDP ports
#!/bin/sh
# usage: lstcp [options] [port]
lstcp() (
while getopts : o; do opts="$opts -$OPTARG"; done; shift $((OPTIND-1))
lsof -i tcp"${1+:}$1" -s tcp:listen $opts
)
# usage: lsudp [options] [port]
lsudp() (
@mohd-akram
mohd-akram / gh-ssh
Last active April 17, 2024 09:13
Utility to handle creating and using GitHub deploy keys
#!/bin/sh
set -euo pipefail
dir=~/.ssh/github.com
name=$(basename "$0")
help=$(printf "\
usage: %s [-d] [repo]
@mohd-akram
mohd-akram / port-revbump
Created October 9, 2023 14:51
Utility to bump revision of a list of ports (MacPorts)
#!/bin/sh
set -euo pipefail
root=
while getopts D: opt; do
case $opt in
D) root=$OPTARG ;;
?) exit 2
esac
done
@mohd-akram
mohd-akram / foreach2of.js
Last active September 11, 2023 11:00
jscodeshift mod to convert Array.forEach to for-of loop
/** @type {import('jscodeshift').Transform} */
module.exports = function (file, api) {
const j = api.jscodeshift;
const root = j(file.source);
return root
.find(j.CallExpression, { callee: { property: { name: "forEach" } } })
.replaceWith((path) => {
const call = path.value;
const callee = /** @type {import('jscodeshift').MemberExpression} */ (
@mohd-akram
mohd-akram / urls
Created August 3, 2022 19:09
Find URLs in a directory
#!/bin/sh
grep -ro "http[^ )>']*" "$1" | grep -v "[{}]" | cut -d: -f 2- | sort | uniq
@mohd-akram
mohd-akram / mkv2mp4
Last active January 20, 2023 08:26
POSIX shell script to convert MKV file to MP4 using ffmpeg
#!/bin/sh
set -eu
if [ $# = 0 ]; then
printf "usage: %s file [opts]\n" "$(basename "$0")" >&2
exit 2
fi
f=$1
shift
if gpac -i "$f" inspect | grep -q DolbyVision; then
set -- -ab dby1 "$@"