Skip to content

Instantly share code, notes, and snippets.

View sandcastle's full-sized avatar
🍦

Glenn Morton sandcastle

🍦
View GitHub Profile
@sandcastle
sandcastle / kubetool.sh
Created July 28, 2018 10:28
A lightweight (no deps) kubectl wrapper for working with contexts and namespaces, heavily inspired by kubectx
#!/bin/sh
[[ -n $DEBUG ]] && set -x
set -eou pipefail
usage() {
cat <<"EOF"
USAGE:
kubetool : show this message
kubetool ctx get : gets the current context
@sandcastle
sandcastle / CacheExtensions.cs
Last active July 25, 2018 14:33
Some helpers for dealing with caching in ASP.Net Core
/// <summary>
/// More information on cache headers can be found here:
///
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching
///
/// The important ones:
///
/// no-store:
///
/// The cache should not store anything about the client request
@sandcastle
sandcastle / AspNetCore21.Tests.cs
Created July 17, 2018 08:13
Asp.net Core 2.1 Xunit Test Configuration
public static class Tests
{
public const string Integration = "IntegrationTests";
}
public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{
protected override IWebHostBuilder CreateWebHostBuilder()
{
return new WebHostBuilder()
@sandcastle
sandcastle / text_replace.sh
Created July 1, 2018 08:15
Cross platform text replacement recursively in a directory
function text_replace() {
case "${OSTYPE}" in
darwin*) PLATFORM="OSX" ;;
linux*) PLATFORM="LINUX" ;;
bsd*) PLATFORM="BSD" ;;
*) PLATFORM="UNKNOWN" ;;
esac
if [[ "${PLATFORM}" == "OSX" || "${PLATFORM}" == "BSD" ]]; then
find artifacts/ -type f -name "*.yml" -exec sed -i "" "s/$1/$2/g" {} +
@sandcastle
sandcastle / inIframe.js
Last active May 2, 2018 12:32
Checks if a given window is within an iframe in a safe way.
/**
* Determines if the the current window is within an iframe
* in a safe cross browser model.
*
* @returns {Boolean} True if in an iframe, else false.
*/
inIframe() {
try {
return window.self !== window.top;
} catch (e) {
@sandcastle
sandcastle / timezones.ts
Created January 29, 2018 00:19
A timezone list with grouping by timezone, in TypeScript.
export interface IanaTimezone {
group: string;
timezone: string;
label: string;
}
export const IANA_TIMEZONES = [
// UTC+14:00
{ group: 'UTC+14:00', timezone: 'Pacific/Kiritimati', label: 'Pacific/Kiritimati (+14)' },
// UTC+13:00
@sandcastle
sandcastle / parseEmailList.js
Last active September 27, 2017 04:29
Parse a list of emails separated by comma (,) or semicolon (;)
/**
* Regex source: https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
*/
const EMAIL_REGEX = /[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/g;
/**
* Parse a list of valid emails from the specified text.
*
* @param {String} text The string of email addresses to parse.
* @returns {[{email: String, name: String}]}
@sandcastle
sandcastle / redirect-ingress.yml
Created August 27, 2017 02:07
An example of a ingress redirect using kubernetes and nginx `configuration-snippet`
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: redirect-ingress
annotations:
ingress.kubernetes.io/configuration-snippet: |
if ($host ~ ^(.+)\.somedomain\.io$) {
return 301 https://$1.domain.io$request_uri;
}
spec:
@sandcastle
sandcastle / DateService.ts
Last active August 17, 2017 00:12
Date and time service to help with UTC handling and formatting.
import { ISecurityService } from './security';
import * as moment from 'moment-timezone';
const UTC_DATE_PARSE: string = 'YYYY-MM-DD';
export const DEFAULT_TIMEZONE: string = 'UTC';
/**
* Provides time zone, date conversion and formatting support
* based on the user's preferences.
*/
@sandcastle
sandcastle / LinqExtensions.Partition.cs
Created August 9, 2017 14:33
Partition a list into many partitions of equal size. Handy for things like migrations where you want to break things into "buckets"
internal static class LinqExtensions
{
public static List<List<T>> Partition<T>(this IEnumerable<T> list, int partitionSize)
{
var i = 0;
var splits = from item in list
group item by i++ / partitionSize into part
select part.ToList();
return splits.ToList();
}