Skip to content

Instantly share code, notes, and snippets.

@brandond
Last active May 15, 2021 00:10
Show Gist options
  • Save brandond/1b3cb66ea012564f886e2e9982bdce42 to your computer and use it in GitHub Desktop.
Save brandond/1b3cb66ea012564f886e2e9982bdce42 to your computer and use it in GitHub Desktop.
K3s => RKE2 go.mod replacement sync script
#!/bin/bash
K3S_VERSION=$(go mod edit --json | jq -r '.Require[] | select(.Path | contains("rancher/k3s")) | .Version')
K3S_COMMIT=$(grep -oE '\w{12}$' <<< ${K3S_VERSION})
K3S_GO_MOD=$(curl -qsL "https://raw.githubusercontent.com/k3s-io/k3s/${K3S_COMMIT}/go.mod")
echo "Updating go.mod replacements from k3s ${K3S_VERSION}"
for MODPATH in $(go mod edit --json | jq -r '.Replace[] | .Old.Path'); do
REPLACEMENT=$(go mod edit --json /dev/stdin <<<${K3S_GO_MOD} | jq -r --arg MODPATH "${MODPATH}" '.Replace[] | select(.Old.Path==$MODPATH) | .Old.Path + "=" + .New.Path + "@" + .New.Version')
if [ -n "${REPLACEMENT}" ]; then
go mod edit --replace=${REPLACEMENT}
fi
done
go mod tidy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment