Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Bradley danielrbradley

View GitHub Profile
@danielrbradley
danielrbradley / OfflineApp.tsx
Created Nov 5, 2019
Integrating service worker auto-refresh with reach router
View OfflineApp.tsx
import React, { useEffect, useRef } from 'react';
import { Location } from '@reach/router';
import { register } from '../serviceWorker';
/**
* Include this component around your router to register a service worker to cache the application
* so it works offline. Then, when the application has automatically updated itself, intercept the
* Reach router location change and do a hard browser navigation to enble the application updates
* to be applied.
* @example
@danielrbradley
danielrbradley / exec.ts
Created Oct 25, 2019
Pulumi custom component for running local commands
View exec.ts
import * as pulumi from '@pulumi/pulumi';
import { exec, ExecOptions } from 'child_process';
export type ExecArgs = {
command: pulumi.Input<string>;
options?: pulumi.Lifted<ExecOptions>;
};
export class Exec extends pulumi.ComponentResource {
stdout: pulumi.Output<string>;
@danielrbradley
danielrbradley / Pulumi+Prettier+ESLint-setup.md
Created Oct 22, 2019
Setting up ESLint & Prettier for Pulumi
View Pulumi+Prettier+ESLint-setup.md

Setting up ESLint & Prettier for Pulumi

This is my process for addint ESLint and Prettier support for a new Pulumi TypeScript project.

Install packages

yarn add -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-airbnb-base eslint-config-prettier eslint-plugin-import eslint-plugin-prettier prettier
@danielrbradley
danielrbradley / aws-auth.sh
Last active Jan 8, 2020
Get console session with AWS access for 36 hours when using MFA
View aws-auth.sh
#!/bin/bash
# Note: Depends on having jq and AWS CLI installed: `brew install jq`, `brew install awscli`
set -e # stop on error
AWS_ACCOUNT="1234567890"
AWS_USERNAME="YOUR_AWS_USERNAME"
MFA_SERIAL="arn:aws:iam::$AWS_ACCOUNT:mfa/$AWS_USERNAME"
export AWS_CONFIG_FILE='/dev/null' # ignore config file so CLI doesn't pick up MFA config
read -p "Enter MFA code for $MFA_SERIAL: " MFA
@danielrbradley
danielrbradley / README.md
Created Aug 3, 2018
Script for backing up processed photos
View README.md

My method for storing and backing up photos is as follows:

  1. All photos get downloaded into the Archive folder, in a folder for the current year, with the batch folder name starting with the date.
  2. The photos get reviewed, the ones which get picked are edited, exported as JPEGs, and the edit metadata saved alongside.
  3. Once editing is complete, the processed JPEGs are uploaded to wherever they're being shared (e.g. Google Photos).
  4. Run the script below which copies only files which have been picked and edited into the Best folder using the same folder names, but not grouped into years.
  5. The Best folder is backed up to a second local disk, and an offsite location (AWS Glacier).
  6. The Archive folder is only backed up onto a second local replicated disk.
@danielrbradley
danielrbradley / type.test.ts
Created Sep 15, 2017
Testing JSON matches TypeScript types
View type.test.ts
import { writeFileSync, mkdirSync, existsSync } from 'fs'
import { resolve } from 'path'
import * as ts from 'typescript'
function checkForTypeErrors(jsonContent: string) {
const tempDir = resolve('.tmp')
const tempFilePath = resolve(tempDir, 'typecheck.ts')
if (!existsSync(tempDir)) {
mkdirSync(tempDir)
}
@danielrbradley
danielrbradley / Request-accept-headers.md
Last active Apr 13, 2017
AWS API Gateway proxy via Lambda
View Request-accept-headers.md

Headers added by chrome...

Going direct to the PNG:

text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

When linked from img tag in an html page:

image/webp,image/*,*/*;q=0.8
@danielrbradley
danielrbradley / unsafe.fsx
Last active Jan 1, 2017
Types instead of Hungarian notation
View unsafe.fsx
// Response to https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/
// How to use types in place of naming conventions
module Unsafe
// What different variations do we want to handle?
type Encodable =
| Safe of string
| Unsafe of string
// Wrap our Request method to always mark the output as unsafe
@danielrbradley
danielrbradley / 1-plain-function.fs
Created Dec 9, 2016
3 ways of organising F# functions related to a type
View 1-plain-function.fs
module VendingMachine
type Coin = Quarter | Dime | Nickel
let valueOf coin =
match coin with
| Quarter -> 0.25m
| Dime -> 0.10m
| Nickel -> 0.05m
@danielrbradley
danielrbradley / IsinValidation.fs
Created Dec 6, 2016
Validate an ISIN (International Securities Identification Number) in F#
View IsinValidation.fs
module Isin
open System.Text.RegularExpressions
// See: http://en.wikipedia.org/wiki/International_Securities_Identification_Number
[<Literal>]
let private AsciiZero = 48
[<Literal>]
let private AsciiNine = 57
You can’t perform that action at this time.