Skip to content

Instantly share code, notes, and snippets.

View jstangroome's full-sized avatar

Jason Stangroome jstangroome

View GitHub Profile
@jstangroome
jstangroome / upgrade-go.sh
Created September 5, 2024 04:51
Upgrade Go modules with Dockerfile builds
#!/bin/bash
set -o errexit
# Assumes the latest version of Go is installed on the PATH.
# Finds all go.mod files under the current working directory and upgrades the
# Go modules to the latest version of Go and upgrades all their Go dependencies
# too.
# Then finds the Dockerfile in the go.mod file's parent directory and updates
@jstangroome
jstangroome / README.md
Created August 20, 2024 05:50
Copy the exact git tree from one commit to another branch

Rather that trying to safely synchronize files between branches, including hidden files, avoiding the .git dir, and removing excess files, it is possible to ask git to create a new commit that represents the exact file set of another commit.

This is based on the concept that git commits are just a pointer to a tree object representing the state of all the files in the repository at the moment of the commit. Instead of creating a new commit based on our git workspace, we create a commit based on the tree from another commit.

# find the tree ID of the source commit
git cat-file -p ${source_commit} | grep tree
func getPodNamespace() string {
const namespaceEnvVar = "POD_NAMESPACE"
const namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
namespaceName := os.Getenv(namespaceEnvVar)
if namespaceName != "" {
return namespaceName
}
namespaceNameBytes, err := os.ReadFile(namespaceFile)
if err != nil {
log.Printf("[ERROR] '%s' environment variable not specified and could not read file '%s': '%#v'",
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"log"
@jstangroome
jstangroome / log.go
Last active April 5, 2023 22:48
Adapter to enable gradual migration from hashicorp/logutils to uber/zap
import (
"io"
"regexp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Replaces hashicorp/logutils by directing Golang's log package to Zap but
// extracting the log level from any `[$severity]` pattern in the message.
@jstangroome
jstangroome / set-drag.ps1
Created June 17, 2020 10:27
Change the mouse drag distance required to initiate a drag operation, e.g. moving files in Explorer
param (
[int]$drag = 4
)
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa
$importDefinition= @'
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
'@
$importType = Add-Type -MemberDefinition $importDefinition -Name WinAPICall -Namespace SetDragSystemParametersInfo –PassThru
@jstangroome
jstangroome / docker-content-tag.sh
Created April 18, 2020 11:43
Generate a tag derived from the inputs to a Docker image, without needing to build the image itself.
#!/bin/bash
cat <<'EOF' >.dockerfile.content-tag
FROM busybox:1
WORKDIR /src/
ENTRYPOINT ["/bin/sh", "-c", "find . -type f \\! -path ./.dockerfile.content-tag -exec sha256sum -b {} +"]
COPY . /src/
EOF
iid=$( docker image build -q -f .dockerfile.content-tag . )
@jstangroome
jstangroome / kubectl-evict
Created March 4, 2020 12:14
kubectl evict in bash
#!/bin/bash
print_usage () {
printf 'Usage: kubectl evict -n <namespace> <pod>\n' >&2
}
main () {
if [ "-n" != "$1" ] && [ "--namespace" != "$1" ]
then
print_usage
@jstangroome
jstangroome / install.sh
Created April 13, 2019 02:22
WSL useful extra packages
apt-get install -y \
apt-transport-https \
binutils \
build-essential \
python-pip \
socat \
sysstat \
traceroute \
unzip \
zip
@jstangroome
jstangroome / find-long-commit-messages.sh
Created January 30, 2019 04:39
Find long commit messages
#!/bin/bash
while read -r commit_hash
do
message=$(
git cat-file commit "${commit_hash}" |
sed '1,/^$/d'
)
if [ "${message:0:5}" = 'Merge' ] || [ "${message:0:6}" = 'Revert' ]