Skip to content

Instantly share code, notes, and snippets.

View flakey-bit's full-sized avatar

Eddie Stanley flakey-bit

  • Xero
  • Vancouver, Canada
View GitHub Profile
@flakey-bit
flakey-bit / sqlserver_locks.sql
Created March 25, 2024 21:52 — forked from aleksp99/tran_locks.sql
tran_locks.sql
-- Based on https://gist.github.com/aleksp99/2ca3401be8965f5ea72187a7baad739d
SELECT
dm_tran_locks.request_session_id AS IDSessions,
CASE
WHEN resource_type = 'object'
THEN OBJECT_NAME(dm_tran_locks.resource_associated_entity_id)
ELSE OBJECT_NAME(partitions.object_id)
END AS ObjectName,
indexes.name AS IndexName,
@flakey-bit
flakey-bit / remove-duplicate-files.sh
Created November 26, 2021 01:35
Using bash to remove duplicate files
# Get all duplicate files, sorted by hash
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD | sort
# For each file that is duplicated, extract the filename to keep
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD | sort | uniq --check-chars 32 | cut -c 35-
# Copy the files to keep to another directory
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD | sort | uniq --check-chars 32 | cut -c 35- | xargs -Ifoo cp foo ../keep
# Remove the duplicate files
@flakey-bit
flakey-bit / EitherValidOrUnsupportedValueConverter.cs
Created September 16, 2021 02:08
System.Text.Json converter for Either<UnsupportedValue, T> - useful where T is an enum from a 3rd party API and you want to handle unknown values
internal class EitherValidOrUnsupportedValueConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
var canConvert = typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(Either<,>) && typeToConvert.GenericTypeArguments[0] == typeof(UnsupportedValue);
return canConvert;
}
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
@flakey-bit
flakey-bit / EnumDataAttribute.cs
Created September 2, 2021 00:26
xUnit data atrtibute for generating theory cases based on an Enum
[AttributeUsage(AttributeTargets.Method)]
public class EnumDataAttribute : DataAttribute
{
private readonly Type _tEnum;
public EnumDataAttribute(Type tEnum) => this._tEnum = tEnum;
/// <summary>Returns the data to be used to test the theory.</summary>
/// <param name="methodUnderTest">The method that is being tested</param>
@flakey-bit
flakey-bit / ValidationProblemDetailsBuilder.cs
Created August 28, 2021 01:03
Fluent, immutable builder for ValidationProblemDetails
internal class ValidationProblemDetailsBuilder
{
private readonly List<Action<ModelStateDictionary>> _modelStateActions;
private readonly List<Action<ValidationProblemDetails>> _problemActions;
public ValidationProblemDetailsBuilder(): this (Array.Empty<Action<ModelStateDictionary>>(), Array.Empty<Action<ValidationProblemDetails>>())
{
}
private ValidationProblemDetailsBuilder(IEnumerable<Action<ModelStateDictionary>> modelStateActions, IEnumerable<Action<ValidationProblemDetails>> problemActions)
@flakey-bit
flakey-bit / TypeExtensions.cs
Created February 6, 2020 20:43
Useful extensions for Types in C#
// Explodes a type (i.e. recursively gets all generic type parameters)
public static class TypeExtensions
{
public static ISet<Type> Explode(this Type t)
{
return ExplodeType(t);
}
private static ISet<Type> ExplodeType(Type t, ISet<Type> result = null)
{
@flakey-bit
flakey-bit / EnumExtensions.cs
Created January 27, 2020 19:23
To/From description methods for enums
public static class EnumExtensions
{
public static T GetAttribute<T>(this Enum value) where T : Attribute
{
var type = value.GetType();
var memberInfo = type.GetMember(value.ToString());
var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
return attributes.Length > 0
? (T)attributes[0]
: null;
@flakey-bit
flakey-bit / ComposeRequestUri.cs
Last active November 7, 2019 22:00
Build request URI with query parameters from base Uri and suffix (.NET) - encodes URI components
public static Uri ComposeRequestUri(string baseUri, string pathSuffix=null, IDictionary<string, object> queryParameters=null)
{
if (string.IsNullOrEmpty(baseUri))
{
throw new ArgumentException($"{nameof(baseUri)} is required", nameof(baseUri));
}
var uri = new Uri(baseUri);
if (!string.IsNullOrEmpty(pathSuffix))
@flakey-bit
flakey-bit / MySQLInstall.ps1
Created October 23, 2019 22:02
Install MySQL 5.7.27 & Workbench on Windows unassisted
Write-Host "Installing MySQL"
$mySqlInstaller = DownloadTempFile -Url "https://downloads.mysql.com/archives/get/file/mysql-installer-community-5.7.27.0.msi" -Filename "mysql-installer-community-5.7.27.0.msi"
Invoke-Process "msiexec" "/i $mySqlInstaller /qb"
Invoke-Process "${env:ProgramFiles(x86)}\MySQL\MySQL Installer for Windows\MySQLInstallerConsole.exe" "community install server;5.7.27;X64:*:servertype=Server;passwd=password -Silent"
Write-Host "Installing MySQL Workbench 8.0.16"
Invoke-Process "${env:ProgramFiles(x86)}\MySQL\MySQL Installer for Windows\MySQLInstallerConsole.exe" "community install workbench;8.0.16;X64:* -Silent"
@flakey-bit
flakey-bit / pulseaudio.service
Created September 11, 2019 02:12
Systemd PulseAudio service for Ubuntu + Kodi HTPC
[Unit]
Description=PulseAudio system server
After=avahi-daemon.service network.target
[Service]
Type=forking
ExecStart=/usr/bin/pulseaudio --realtime --no-cpu-limit --system --disallow-exit --daemon
ExecReload=/bin/kill -HUP $MAINPID
Restart=always