Skip to content

Instantly share code, notes, and snippets.

View JonCanning's full-sized avatar

Jon Canning JonCanning

View GitHub Profile
@JonCanning
JonCanning / test.sh
Last active June 6, 2024 06:23
golang continuous testing
#!/bin/bash
fswatch -l 1 -o ./**/*.go --event=Updated | while read -r; do
clear
output=$(go test ./... 2>&1)
if echo "$output" | grep -- '- FAIL' > /dev/null; then
echo -e "\e[31m$(echo "$output" | awk '/--- FAIL/,/Test:/')\e[0m"
elif [ -z "$output" ]; then
echo -e "\e[31mBUILD FAILED\e[0m"
else
echo -e "\e[32mPASS\e[0m"
@JonCanning
JonCanning / log.go
Last active June 20, 2024 15:04
Go Logger
package log
import (
"context"
"log/slog"
"os"
"github.com/covalenthq/lumberjack"
slogmulti "github.com/samber/slog-multi"
"go.opentelemetry.io/otel/trace"
@JonCanning
JonCanning / install.sh
Created December 18, 2023 10:04
Install latest docker-compose
curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url | grep -i "$(uname -s)-$(uname -m)" | cut -d '"' -f 4 | head -n 1 | xargs curl -L -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
@JonCanning
JonCanning / kafka.fsx
Last active January 24, 2023 11:02
Kafka F# example
#r "nuget: Confluent.Kafka"
#r "nuget: System.Text.Json"
open Confluent.Kafka
open System
open System.Text.Json
open System.Threading.Tasks
open Confluent.Kafka.Admin
let server = "localhost:9092"
@JonCanning
JonCanning / packet.fsx
Last active January 12, 2023 22:19
Send a packet using SharpPcap and PacketDotNet
#r "nuget: PacketDotNet"
#r "nuget: SharpPcap"
open PacketDotNet
open SharpPcap
open System.Net.NetworkInformation
open System.Net
open System.IO
let deviceName = "eth0"
@JonCanning
JonCanning / benchmark.fsx
Last active January 14, 2023 12:27
F# BenchmarkDotNet script
#r "nuget:BenchmarkDotNet"
open BenchmarkDotNet.Running
open BenchmarkDotNet.Attributes
[<InProcessAttribute true; MemoryDiagnoser>]
type Runner() =
[<Benchmark>]
member _.Test1() = "test1"
@JonCanning
JonCanning / dedupe.fs
Created July 8, 2022 09:08
Dedupe items in a seq
let dedupe source =
seq {
let mutable previous = ValueNone
for item in source do
match previous with
| ValueSome x when x = item -> ()
| _ -> yield item
previous <- ValueSome item
}
#!/bin/bash
while IFS=, read -r col1 ; do
echo "$col1"
done <"$1"
@JonCanning
JonCanning / OptionsMonitor.fs
Last active March 4, 2022 16:30
FSharp IOptionsMonitor for ConsoleLoggerProvider
open Microsoft.Extensions.Logging.Console
open Microsoft.Extensions.Options
type OptionsMonitor<'T>(options) =
interface IOptionsMonitor<'T> with
member _.CurrentValue = options
member _.Get _ = options
member _.OnChange _ = { new IDisposable with member _.Dispose() = () }
let loggerProvider = new ConsoleLoggerProvider(OptionsMonitor(ConsoleLoggerOptions(FormatterName = "Json")))
@JonCanning
JonCanning / Json.fs
Created August 13, 2021 16:20
Sensible json defaults
module Json
open Microsoft.FSharpLu.Json
open Newtonsoft.Json
open Newtonsoft.Json.Serialization
open Newtonsoft.Json.Converters
let private compactJsonSerializerSettings =
let settings = Compact.TupleAsArraySettings.settings