Skip to content

Instantly share code, notes, and snippets.

@mingfang
mingfang / f.js
Created September 24, 2024 11:56
dynamic javascript eval
export default function f(body) {
const code = `
with(this){
return(async function(){
${body}
}).call(this)
}
`
return new Function(code)
};
@mingfang
mingfang / spawnPromise.js
Created September 24, 2024 05:41
child_process.spawn as promise
import child_process from 'child_process';
function spawnPromise(cmd, args, options) {
return new Promise(function (resolve, reject) {
const process = child_process.spawn(cmd, args, options);
process.on('close', function (code) {
resolve(code);
});
process.on('error', function (err) {
reject(err);
@mingfang
mingfang / apache_age_load_sql.sql
Created March 6, 2024 11:16
Apache AGE load data using SQL
CREATE EXTENSION IF NOT EXISTS age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
-- Apache AGE load_sql
CREATE OR REPLACE FUNCTION public.load_sql(query agtype) RETURNS SETOF agtype AS $$
BEGIN
RETURN QUERY EXECUTE FORMAT($sql$
WITH query AS (%s)
@mingfang
mingfang / apache_age_graph_search.sql
Last active March 6, 2024 11:08
Apache AGE Full text search of graph data
CREATE EXTENSION IF NOT EXISTS age;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
-- Apache AGE graph_search
CREATE OR REPLACE FUNCTION public.graph_search(graph agtype, label agtype, property agtype, search agtype) RETURNS SETOF agtype AS $$
DECLARE
fts TEXT;
@mingfang
mingfang / skopeo-sync.sh
Last active January 23, 2024 15:19
Use skopeo to sync all images across private Docker registries
export SRC="10.0.4.5:5000"
export DEST="10.0.4.5:6000"
curl -s http://${SRC}/v2/_catalog | jq .repositories[] | xargs -I {} \
skopeo sync \
--src-tls-verify=false --src docker --src-creds ${DOCKER_USER}:${DOCKER_PASS} \
--dest-tls-verify=false --dest docker --dest-creds ${DOCKER_USER}:${DOCKER_PASS} \
${SRC}/{} ${DEST}
@mingfang
mingfang / ReactAdapter.svelte
Last active August 9, 2023 18:58
Svelte component to use React components
<script>
import {onDestroy, onMount} from "svelte"
export let component
let container
let root
onMount(async ()=> {
const {createRoot} = await import('react-dom/client')
const {createElement} = await import('react')
@mingfang
mingfang / gist:689258d4499d3aeb7ed174fdfc8d9521
Created September 27, 2020 03:17
Set Devialet Speakers to Optical Direct Mode
volume=70
speakers="192.168.2.103 192.168.2.60"
for host in $speakers; do
echo $host
# enable optical direct mode
curl -s http://$host/opticaldirect/enabled -XPOST
# name must be Left or Right
curl -s http://$host/device/name | jq .
role=$(curl -s http://$host/device/name | jq -r .data.name)
curl -s http://$host/opticaldirect/role -d "{\"role\":\"Front$role\"}" -H "Content-Type: application/json"
@mingfang
mingfang / shell.go
Created November 10, 2019 04:12
Cadence activity to execute shell commands
package main
import (
"context"
"flag"
"os"
"os/exec"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
"go.uber.org/cadence/activity"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mingfang
mingfang / screensaver.sh
Created August 31, 2017 05:22
script to turn off and on Raspberry Pi Display by detecting IP or bluetooth device
#!/bin/bash
while true
do
if ping -c 1 -w 3 192.168.2.67 &> /dev/null || hcitool scan | grep -q -E "ming-macbook|Ming's iPhone" ; then
echo "welcome back. turning screen on"
echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
else
echo "you're gone. turning screen off"
echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power