Skip to content

Instantly share code, notes, and snippets.

View byrnedo's full-sized avatar

Donal Byrne byrnedo

View GitHub Profile
@byrnedo
byrnedo / multierror.go
Created November 16, 2022 14:30
Multierror.go, mutliple go error container.
View multierror.go
// MultiError don't use directly, use NewMultiError
type MultiError map[string]error
// NewMultiError returns nil if all errs are nil
func NewMultiError(errs map[string]error) error {
for k, v := range errs {
if v == nil {
delete(errs, k)
}
}
@byrnedo
byrnedo / debounce.go
Last active November 2, 2022 08:53
Debounce go channel
View debounce.go
package pkg
import (
"context"
"time"
)
// Debounce takes a channel, and will notify the output channel with the last received message at frequency min.
// Upon cancel or max timeout, it will check one last time for a message.
// Cancelling the ctx will cause the goroutine to exit.
@byrnedo
byrnedo / repo.go
Last active September 3, 2022 15:27
Repo setup for go that lets you use transactions transparently
View repo.go
package repo
import (
"context"
"database/sql/driver"
"github.com/jmoiron/sqlx"
)
// can be either a *sqlx.DB or a *sqlx.Tx
@byrnedo
byrnedo / sqlx-stats-logger.go
Created July 21, 2022 05:59
Go routine to periodically log sqlx connection pool stats
View sqlx-stats-logger.go
go func(dbx *sqlx.DB) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
stats := dbx.Stats()
b, _ := json.Marshal(stats)
log.Printf("db stats: %d open, %d idle, %d in-use", stats.OpenConnections, stats.Idle, stats.InUse)
@byrnedo
byrnedo / vaccination.sh
Created June 23, 2021 13:22
Swedish Vaccination Time Checker
View vaccination.sh
#!/bin/bash
# FIXME
WEBHOOK_URL=YOUR_SLACK_WEBHOOK_URL_HERE
# A list of the clinics you're interested in polling, you'll find them via the 1177 links
for clin in 1546 500 493 2053 2056 521 528 506; do
apt=$(curl -s https://booking-api.mittvaccin.se/clinique/$clin/appointmentTypes |jq -r '.[] |select(.name == "1 pers Covid-19 30-64 år (född -57 till -91)")|.id')
echo $clin $apt
curl https://booking-api.mittvaccin.se/clinique/$clin/appointments/$apt/slots/210601-210731 -s |jq -e '.[].slots |any(.[]; .available == true)'|grep true
if [ $? -eq 0 ]; then
time=$(date +%s)
@byrnedo
byrnedo / tricks.sh
Created February 15, 2021 13:05
selenium standalone tricks
View tricks.sh
#!/bin/bash
# start a new session
curl --data '{"desiredCapabilities":{"browserName": "chrome"}}' --silent http://localhost:4444/wd/hub/session | jq -r .sessionId
@byrnedo
byrnedo / filli.sh
Created February 15, 2021 13:05
Fill terminal input and return to interactive
View filli.sh
#!/bin/bash
# Sends foo bar and baz then back to you
(printf 'foo\nbar\nbaz\n' && cat) | $1
@byrnedo
byrnedo / indexed_sql_file_runner.go
Last active October 23, 2020 06:10
Run sql commands from a file, saving position in a `.index` file in case of error.
View indexed_sql_file_runner.go
package main
import (
"bufio"
"database/sql"
"flag"
"fmt"
"path"
"strconv"
"strings"
@byrnedo
byrnedo / nginx-json-log.conf
Last active May 20, 2020 13:39
Json Nginx Logging
View nginx-json-log.conf
map $msec $millis { ~(.*)\.(.*) $2; }
map $time_iso8601 $time_iso8601_m { ~(.*)\+(.*) $1.$millis+$2; }
log_format json_combined escape=json
'{'
'"_ms": "$millis",' # Has to be here in order for timestamp to work. Wat
'"timestamp": "$time_iso8601_m",'
'"service": "sp-frontend",'
'"remote_addr":"$remote_addr",'
'"message":"[$status] $request_method $request_uri",'
@byrnedo
byrnedo / httpclient.go
Created May 12, 2020 07:07
Comprehensive http client base in go
View httpclient.go
package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"