Skip to content

Instantly share code, notes, and snippets.

View chernjie's full-sized avatar

CJ (curl | jq) chernjie

View GitHub Profile
@chernjie
chernjie / cache_get.sh
Last active February 22, 2024 00:23
cache_get: use curl to fetch if cache is not available locally
#!/usr/bin/env bash
cache_get() {
local cache="./data/$1.json"
test -f "$cache" && find "$cache" -type f -mtime +$((1/24)) -exec rm -v {} + >&2 # clear cache >1 hr
test -f "$cache" || curl --silent "$2" --create-dirs --output "$cache"
cat "$cache"
}
@chernjie
chernjie / mkpasswd
Last active September 15, 2023 07:19
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from optparse import OptionParser
from passlib.hash import sha512_crypt;
import getpass;
parser = OptionParser(usage="%prog --method=sha-512")
parser.add_option(
'--method',
@chernjie
chernjie / queue.js
Created June 21, 2022 00:52
chive vanilla js practice
$$('figure img')
.map(el => el.src)
.reverse()
.reduce((last, next) => {
return () => {
setTimeout(() => {
console.log('<img src="'+ next +'" />')
last()
}, 1000)
}
@chernjie
chernjie / 10-magento-filter.conf
Created March 19, 2015 04:59
Logstash multiline filter for Magento
filter {
if [type] == "magento" {
multiline{
pattern => "^%{TIMESTAMP_ISO8601:timestamp}"
what => "previous"
negate=> true
}
grok {
match => [
"message",
@chernjie
chernjie / branch-cleanup.sh
Last active June 15, 2021 21:01
Find redundant branches and print commands to delete them
#!/usr/bin/env bash
COLOR_YELLOW(){ echo -en "\033[33m"; }
COLOR_RESET() { echo -en "\033[0m"; }
MAIN_BRANCH=${1:-master}
MAIN_REMOTE=${2:-origin}
label() {
echo -e "\033[33m$@\033[0m" >&2
@chernjie
chernjie / npmr
Last active February 7, 2021 22:41
#!/usr/bin/env bash
# Like `npm`, but executes the command in nested sub-directories within
# your current git repository
#
# @author CJ <lim@chernjie.com>
# Example:
#
# $ npmr ls lodash # List installed packages in nested node packages
@chernjie
chernjie / set.sh
Last active September 12, 2020 21:43
CLI implementation of Set, using file for storage
#!/usr/bin/env bash
STATEFILE=${STATEFILE:-retry.log}
# dependencies
for i in sort gsed grep echo
do command -v $i > /dev/null || (echo $i not found >&2 && exit 1)
done
prime() {
var table = $$('.emoji-card').map((el, i) => {
return {
emoji: el.firstChild.firstChild.innerHTML,
code: el.lastChild.firstChild.innerHTML,
description: el.lastChild.lastChild.innerHTML
};
}).reduce((table, el, i) => {
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + el.emoji + '</td><td>' + el.code + '</td><td>' + el.description + '</td>';
table.appendChild(tr);
@chernjie
chernjie / git-gh-setup.sh
Created February 9, 2015 09:25
Fetch Github's Pull Request as a remote branch
#!/usr/bin/env bash
git remote -v | grep fetch | grep github | \
while read remote url _; do
if ! git config --get-all "remote.$remote.fetch" | grep -q refs/pull
then
git config --add "remote.$remote.fetch" \
'+refs/pull/*/head:refs/remotes/'"$remote"'/pull/*'
fi
done
#!/usr/bin/env php
<?php
// if no arguments, take input from stdin
$argc < 2 && array_push($argv, file_get_contents("php://stdin"));
array_shift($argv);
$output = array();