Skip to content

Instantly share code, notes, and snippets.

View KylePDavis's full-sized avatar

Kyle P Davis KylePDavis

View GitHub Profile
@KylePDavis
KylePDavis / FormatStringRegExpBuilderExample.js
Last active October 10, 2015 21:38
XRegExp tokenizer and parser that builds a new RegExp to validate strings against a given format string
// Get XRegExp in all it's goodness and install native stuff
var XRegExp = require("xregexp");
XRegExp.install("natives");
///////////////////////////////////////////////////////////////////////////////
/// LEXER
///////////////////////////////////////////////////////////////////////////////
// Create a formatToken XRegExp to match against the formatStr
@KylePDavis
KylePDavis / sh_env_var_opts.sh
Last active September 14, 2023 01:26
Simple bash shell script templates. There are two versions: 1) simple env var based options, and 2) with added command line argument parsing and error handling.
#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
# USAGE:
# DESCRIPTION OF ENV VARS HERE
###############################################################################
set -e # exit on command errors (so you MUST handle exit codes properly!)
set -o pipefail # capture fail exit codes in piped commands
#set -x # execution tracing debug messages
# Get command info
@KylePDavis
KylePDavis / js_class_template.js
Created October 16, 2012 19:42
JavaScript Classes Template (with proper inheritance)
//NOTE: if not in Node.js then you'll need to remove references to module.exports and require()
var MyClassName = (function(){
// CONSTRUCTOR
var klass = module.exports = MyClassName = function MyClassName(){
//[CONSTRUCTOR]; e.g., if(arguments.length > 0) throw new Error("Unexpected args!");
//[INSTANCE MEMBERS]; e.g., this.value = 123;
base.call(this);
}, base = require("./BaseClass"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
// PRIVATE STATIC MEMBERS
@KylePDavis
KylePDavis / js_object_copy_slice.js
Created November 12, 2012 15:20
Experimenting with the idea of copying Object slices and creating composite Objects via slices
var util = require("util");
var o = {
p1: {
c1:[{l1:"a"}, {l2:"b"}],
c2:{l3:3, l4:4}
},
p2: {foo:"bar"}
};
process.stdout.write("// Original object:\n");
@KylePDavis
KylePDavis / mocha_test.js
Last active October 12, 2015 19:18
Self-hosted Mocha Test Case Template
"use strict";
// Mocha one-liner to make these tests self-hosted
if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
// requires
var assert = require("assert"),
MyClass = require("../../lib/MyClass");
@KylePDavis
KylePDavis / sh_template_template.sh
Created February 13, 2014 02:56
A template for shell-based templates. Good for generalizing config files and the like.
#!/bin/bash
# Template bash script using given script file as source for variable values
if [ -f "$1" ]; then source "$1" || echo "#! ERROR: Unable to read variables from script: $1!" 1>&2 && exit 1; fi
(awk '/TMPL$/,/^TMPL/{print}' "$0" | grep -Eio '(^|[^\])\${?[A-Z][A-Z0-9_]*}?' | cut -c3- | tr -d '{}' | sort -u) | grep -v -w -F "$(env | cut -f1 -d= | sort)" | sed 's/^/ERROR: need tmpl var: /' | grep . && exit 123 || true
###############################################################################
cat<<TMPL
# Config generated on $(date)
HOST=$SOME_HOST
FILES="$(ls -1 | paste -sd,)"
TMPL
@KylePDavis
KylePDavis / get_url_stats.sh
Created March 26, 2014 03:30
A simple script to get the given URL with cURL and output stats.
#!/bin/bash
# A simple script to get the given URL with cURL and output stats.
#
# USAGE:
# get_url_stats.sh <URL>
#
###############################################################################
out() { echo "`date +%Y%m%dT%H%M%SZ`: $*"; }
err() { out "$*" 1>&2; }
@KylePDavis
KylePDavis / karma_test.js
Last active August 29, 2015 14:02
Self-hosted Karma Test Case Template
// Karma one-liner to make these tests self-hosted (needs karma.conf.js support for TEST_FILE env var)
if(typeof require!=='undefined' && require.main===module) global.describe=function(){}, process.env.TEST_FILE=__filename, require("karma").server.start({configFile:__dirname+'/../karma.conf.js'});
describe('app.module.thing', function(){
// module deps
beforeEach(module('app.module'));
// vars injected
var $rootScope;
@KylePDavis
KylePDavis / etc_launchd.conf
Created August 25, 2014 14:10
Mac OS X Power User ProTip: Increase OS Limits
# Increased file and proc limits for Mac OS X based on http://superuser.com/a/514049
limit maxfiles 8192 20480
limit maxproc 1000 2000
@KylePDavis
KylePDavis / backup.sh
Created March 19, 2015 15:08
Self-contained auto-updating rsync-based backup script from a lifetime ago used to cross-sync data to local backup servers and then remote backup servers
#!/bin/sh
# Description:
# Automated backup script designed to support multiple backup servers.
#
# ChangeLog:
# 20130822 kdavis - Updated to use fully qualified hostname
# 20120125 kdavis - Minor updates for mysqldump over ssh so it can use the same security model as rsync, setting env vars from command line, and logging
# 20080610 kdavis - Added missing rsync error code handling to one of the backup methods that uses rsync internally
# 20080123 kdavis - Added more debugging; and...
# 20080119 kdavis - Added lock files/PID checks to ensure only one instance