Skip to content

Instantly share code, notes, and snippets.

View ChrisCinelli's full-sized avatar

Chris Cinelli ChrisCinelli

  • Close5
  • San Francisco, CA, United States
View GitHub Profile
@ChrisCinelli
ChrisCinelli / index.js
Last active December 19, 2017 01:56
Fast string template with tolerance to undefined
const _get = require('lodash/get');
const regEx = /\{\s*([a-zA-Z0-9\[\]\._]+)(\s*\|\s*['"]([^\}\s]+)['"])?\s*\}/g;
/*
f({a: 1, b: { c: 2, d: 3}}, '({a}, {b.c}, {c.d})') will output (1, 2, 3)
*/
module.exports = (obj, str) => str.replace(regEx, (match, p1, p2, p3) => _get(obj, p1) || p3 || '');
@ChrisCinelli
ChrisCinelli / backup-data.sh
Last active September 29, 2023 16:30
Backup all indexes in Elastic Search (ES) in an archive
#!/bin/bash
# See https://gist.github.com/593100533a7dbb4474612f9adb8f8ff6 for more info
# This script backup data from a ES instance to disk and create a xz archive with the command to restore it
# You need to have jq ( https://stedolan.github.io/jq/ ) and elasticdump ( https://www.npmjs.com/package/elasticdump ) installed
# source ES instance
DEFAULT_ESR='http://locahost:9200' # Edit this
ESR=${1:-$DEFAULT_ESR} # Or just use $1 to add the Elastic Search url
@ChrisCinelli
ChrisCinelli / split10m
Last active November 10, 2018 00:43
Split a file in pieces of 10Mb that can be easily reassembled with 'cat'
#!/bin/bash
# Split files in a bunch of 10Mb files and add .zip (even if they are not zip)
# Why? They can be attached to a issue in Github and overcoming the ;-)
# Use: split10m file
# or tar cJf - folder_to_compress | split10m
gsplit -b 10m -d --additional-suffix=.zip ${1:--} ${2:-archive}
# It can be reconstructed with just:
# $ cat archive*.zip > originalfile
# Or assuming that the file was a .tar.xz file, it can be extract directly:
@ChrisCinelli
ChrisCinelli / migrate-data.sh
Last active November 10, 2018 01:02
Migrate all indexes of Elastic Search from a cluster to another - It has less limirations then reindex when runinng with differnt ES versions
#!/bin/bash
# See https://gist.github.com/db1af03d007d2f64a44ff22f4633fe74 for more info
# This script backup data from a ES instance to disk and create a xz archive with the command to restore it
# You need to have jq ( https://stedolan.github.io/jq/ ) and elasticdump ( https://www.npmjs.com/package/elasticdump ) installed
# source ES instance
DEFAULT_ESR='http://source:9200' # Edit this
ESR=${1:-$DEFAULT_ESR} # Or just use $1 to add the Elastic Search url
@ChrisCinelli
ChrisCinelli / cachedRequest.js
Last active November 20, 2018 00:09
cache for npm request and elastic search
var _request = require('request');
const { stringify } = require('flatted/cjs');
const LRU = require('lru-cache');
const crypto = require('crypto');
// Interface for cached requests
class Cache {
// opt to pass to LRU, h hash function for the key