Skip to content

Instantly share code, notes, and snippets.

Avatar

Kurt Roberts kurtroberts

View GitHub Profile
@kurtroberts
kurtroberts / datastoreDecorator.js
Created Sep 10, 2020
Amplify Datastore - Control when Sync Starts
View datastoreDecorator.js
/******
Amplify's DataStore is useful, but it is very, very immature and has some
incredibly naive implementation choices. One of those is that it will
perform synchronization on the first call to query, regardless of a call
to start.
This is an issue, because most non-trivial applications will require a
login so that the app can load appropriate content for the user in question.
@kurtroberts
kurtroberts / index.js
Created Jul 29, 2020
Fun with browser automation
View index.js
const puppeteer = require('puppeteer');
async function getLink (url, xpathExpression) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
let link = await page.$eval(xpathExpression, anchor => { return anchor.href; });
await browser.close();
return link;
};
@kurtroberts
kurtroberts / session.sh
Created Dec 19, 2018
Silly Perl Tricks
View session.sh
# remove a string from all lines in a file
cat thing-groups.csv | perl -ne 's/,SmartGroup//g; print $_' -
# just the ones with that smartgroup, but remove it
cat thing-groups.csv | perl -ne 'while(/(.*),SmartGroup(.*)/g){ print "$1$2\n" };' -
# just the ones with that smartgroup, but make it the last one
cat thing-groups.csv | perl -ne 'while(/(.*)(,SmartGroup)(.*)/g){ print "$1$3$2\n" };' -
@kurtroberts
kurtroberts / session.sh
Last active Oct 31, 2018
Script Alerting Options
View session.sh
#/bin/bash
# Built in:
osascript <<EOF 2>/dev/null
display notification "test message" with title "title"
EOF
# yo- https://github.com/sheagcraig/yo
yo_scheduler -t "Title" -n "test message"
@kurtroberts
kurtroberts / logging-docker-to-cloudwatch.md
Created Sep 21, 2018
Using awslogs driver for docker with docker-machine
View logging-docker-to-cloudwatch.md

Make a docker-machine with AWS Credentials

Obviously, you'll need to make credentials first.

$ docker-machine create -d virtualbox \
    --engine-env AWS_REGION=us-east-1 \
    --engine-env AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxx \
    --engine-env AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxx \
    awslogs
@kurtroberts
kurtroberts / demos.sh
Created Sep 5, 2018
Strategies for parsing JSON on the Mac without installing anything
View demos.sh
# using OSAScript's JavaScript support
osascript -l JavaScript -e "console.log(($(cat /Users/$(whoami)/Library/Application\ Support/Google/Chrome/Local\ State)).profile.info_cache.Default.user_name)"
# using python
cat /Users/$(whoami)/Library/Application\ Support/Google/Chrome/Local\ State | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["profile"]["info_cache"]["Default"]["user_name"];'
View list-command-pattern.sh
#!/bin/bash
tasklist="Task1 Task2"
Task1 () {
echo "task 1"
}
Task2 () {
echo "task 2"
@kurtroberts
kurtroberts / Bottleneck.js
Last active Sep 1, 2020
Strategies for meeting API Limits with Promises
View Bottleneck.js
const _ = require('lodash'),
Bottleneck = require('bottleneck'),
limiter = new Bottleneck({
minTime: 333,
maxConcurrent: 1
}),
Promise = require('bluebird'),
promises = [];
_.range(10).forEach(function (x) {
@kurtroberts
kurtroberts / index.js
Created Aug 9, 2018
Handling HTTP/HTTPS redirection in a Lambda function
View index.js
'use strict';
/****
Set up as a lambda function, it doesn't need any special permissions.
To test, you'll use a CloudFrontHTTPRedirect template.
Then, you'll create a CloudFront Distribution and attach this as a behavior.
Use the Event Type "ViewerRequest" to trigger it.
@kurtroberts
kurtroberts / drop-all.sql
Created Aug 4, 2018
Drop all tables in a PSQL schema
View drop-all.sql
-- Liberally borrowed from - https://stackoverflow.com/questions/3327312/drop-all-tables-in-postgresql
-- If you happen to be working on a jira database, you're going to have to do it twice to resolve all the cascades
DO $$ DECLARE
r RECORD;
BEGIN
-- if the schema you operate on is not "current", you will want to
-- replace current_schema() in query with 'schematodeletetablesfrom'
-- *and* update the generate 'DROP...' accordingly.
You can’t perform that action at this time.