Skip to content

Instantly share code, notes, and snippets.

View karrick's full-sized avatar

Karrick McDermott karrick

View GitHub Profile
@karrick
karrick / overwrite
Created April 17, 2020 22:47
overwrite
#!/bin/bash
remote=some.remote.host
OPTIND=1
while getopts :r: option 2>/dev/null
do
case $option in
r) remote="$OPTARG" ;;
\:) echo >&2 "fatal: option [$OPTARG] requires an argument\n" ;;
func escapeSingleQuote(slice []byte) []byte {
l := len(slice)
var shifts int
for index := l - 1; index >= 0; index-- {
if slice[index] == '\'' {
slice = append(slice, byte(0))
copy(slice[index+1:], slice[index:]) // move bytes to right
shifts++
}
}
@karrick
karrick / healthCheck.go
Created October 4, 2018 17:49
makeHealthcheck returns a healthcheck http.HandlerFunc
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
gohm "github.com/karrick/gohm/v2"
)
@karrick
karrick / flock.go
Last active November 12, 2020 18:03
Examples of using advisory locking with golang.org/x/sys/unix
// These funcitons ought to come with a huge warning. Using them without
// understanding the differences between blocking and non-blocking, or
// between exclusive and shared access, and when to use them will cause
// problems that do not necessarily manifest right away, but rather
// fester in your program and rear their ugly head in the middle of the
// night when you're sleeping.
//
// If you use advisory file locking, *always* use Shared locking every
// single time you read the file, and *always* use Exclusive locking every
// single time you write the file. Period. There are no exceptions. Heed
@karrick
karrick / growByteSlice.go
Last active August 8, 2018 20:59
growByteSlice will return a byte slice with a capacity at least equal to the specified size.
// growByteSlice will return a byte slice with a capacity at least equal to the
// specified size.
func growByteSlice(buf []byte, need int) []byte {
// Optimization for when buffer might be required to grow by more than
// double its size.
if need > cap(buf)<<1 {
t := make([]byte, need)
copy(t, buf)
return t
}
package main
import (
"os"
"golang.org/x/crypto/ssh/terminal"
)
func readHiddenFromTTY(prompt string) (string, error) {
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
package main
import "time"
func backoff(retries int, callback func() error) error {
var err error
delay := time.Millisecond
for {
err = callback()
if err == nil {
@karrick
karrick / pruneEmptyDirectories.go
Created February 15, 2018 08:34
Go function that prunes empty directories from file system hierarchy
package main
import (
"fmt"
"os"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
)
@karrick
karrick / readStdinWithTimeout.go
Last active February 15, 2018 08:31
Go function that prompts user but has a timeout on standard input
package main
import (
"bufio"
"fmt"
"io"
"os"
"time"
"github.com/karrick/gorill"
@karrick
karrick / sortAndMaybeInsertString.go
Created October 19, 2017 18:29
sortAndMaybeInsertString
package sortAndMaybeInsertString
func sortAndMaybeInsertString(s string, a []string) []string {
if len(a) == 0 {
return append(a, s)
}
sort.Strings(a)
i := sort.SearchStrings(a, s)