Skip to content

Instantly share code, notes, and snippets.

View gsscoder's full-sized avatar
💭
obsessive coding disorder

coder (π³) gsscoder

💭
obsessive coding disorder
View GitHub Profile
@gsscoder
gsscoder / chaos2.fsx
Last active February 27, 2021 13:39
Simple F# function that returns a function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos2.fsx
// latency test
// 0.969491
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) =
fun (service:unit -> 't) ->
if shouldChaos () then
@gsscoder
gsscoder / chaos.fsx
Last active February 27, 2021 13:39
Simple F# function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos.fsx
// latency test
// 0.919409
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) (service:unit -> 't) =
if shouldChaos () then
printfn "%s" name
@gsscoder
gsscoder / verbose.ps1
Created January 12, 2021 07:08
PowerShell code to test if -Verbose switch is enabled
[CmdletBinding()]
param ()
Set-StrictMode -Version Latest
$verbose = if ($PSBoundParameters.ContainsKey('Verbose')) {
$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent } else { $false }
"Verbose enabled: $verbose" | Write-Host
@gsscoder
gsscoder / bearer_token.ps1
Created December 30, 2020 10:55
PowerShell code to get the bearer token using an app registration
$tenantId = 'your_tenant_id'
$clientId = 'your_client_id'
$clientSecret = 'your_client_secret'
$tokenResponse = (Invoke-WebRequest -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" `
-Method Post -UseBasicParsing `
-Body "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret").Content
"$(($tokenResponse | ConvertFrom-Json).access_token)`n" | Write-Host
@gsscoder
gsscoder / Require-AzCliVersion.ps1
Last active April 23, 2021 08:20
PowerShell function to check Azure CLI version
function Require-AzCliVersion {
[OutputType([void])]
param(
[Parameter(Mandatory, ValueFromPipeline)] [ValidatePattern("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,9}$")] [string] $Version,
[switch] $Fail
)
$actual = ('az version' | Invoke-Expression | ConvertFrom-Json).'azure-cli'
if ($actual -eq $Version) {
"Azure CLI present in pinned version $Version" | Write-Verbose
} else {
@gsscoder
gsscoder / Get-RandomString.ps1
Last active January 20, 2021 05:24
PowerShell function to generate alphanumeric string
# PS> 20 | Get-RandomString -Alphanumeric $false -NoQuotes $true
# PS> p({BFhlH*}KG#S{i{Ih5
function Get-RandomString {
[OutputType([string])]
param(
[Parameter(Mandatory, ValueFromPipeline)] [int] $Length,
[bool] $Alphanumeric = $true,
[bool] $NoQuotes = $false
)
@gsscoder
gsscoder / Stage_Profile.ps1
Created July 21, 2020 07:57
PowerShell profile code to stage git changes with one command
function Set-GitChanges {
gaa
gcam Staging
ggp
}
Import-Module posh-git
Import-Module git-aliases -DisableNameChecking
Set-Alias -Name stage -Value Set-GitChanges
@gsscoder
gsscoder / ConsoleEx.cs
Last active April 23, 2021 08:21
C# console helper (colors and bold)
using System;
sealed class ConsoleEx
{
public void Write(ConsoleColor color, string format, params object[] arg)
{
var current = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(format, arg);
Console.ForegroundColor = current;
@gsscoder
gsscoder / PropertyInfoExtensions_IsEnumerable.cs
Last active July 10, 2020 15:16
C# extension method to discover if a property is a collection
using System.Collections.Generic;
using System.Reflection;
public static class PropertyInfoExtensions
{
public static bool IsEnumerable(this PropertyInfo property) =>
(property.PropertyType.IsGenericType &&
property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null;
}
@gsscoder
gsscoder / LowerCaseNamingStrategy.cs
Created July 9, 2020 09:02
C# Json.NET naming strategy to serialize enum values as lowercase strings
/*
public class YourClass
{
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter), converterParameters: typeof(LowerCaseNamingStrategy))]
public YourEnum YourProperty { get; set; }
}
*/
using Newtonsoft.Json.Serialization;