Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
xns-informers deps update
#!/bin/bash
set -eo pipefail
die () {
echo >&2 "$@"
show_help
exit 1
}
show_help() {
local usage
usage="
$(basename "$0")
Usage:
./$(basename "$0") [flags]
Options:
--version of istio/istio upstream project from which go.mod is used
-h, --help
Help message.
Example:
./$(basename "$0") --version 1.15.2
"
echo "$usage"
}
function header {
echo -e "\n\e[92m\e[4m\e[1m${1}\e[0m\n"
}
function hasDepToReplace {
local value=$1
local deps=$2
for dep in "${deps[@]}"; do
found=$(echo "$dep" | grep "$value" || true)
if [ -n "$found" ]; then
return 0
fi
done
return 1
}
while test $# -gt 0; do
case "$1" in
-h|--help)
show_help
exit 0
;;
--version)
if [[ $1 == "--"* ]]; then
version="${2/--/}"
shift
fi
shift
;;
*)
die "$(basename "$0"): unknown flag $(echo $1 | cut -d'=' -f 1)"
exit 1
;;
esac
done
if [ -z "$version" ]; then
die "Missing version"
fi
if ! command -v curl &>/dev/null; then
echo "curl is required"
exit 1
fi
if ! command -v deptree &>/dev/null; then
echo "deptree is required. $ go install github.com/vc60er/deptree@latest"
exit 1
fi
istioDeps=$(curl -s https://raw.githubusercontent.com/istio/istio/${version}/go.mod)
mapfile -t deps < <(go mod graph | deptree -d 1 | cut -d' ' -f 2 | tr -s '\n' | sort | grep -v "tree:")
mapfile -t replaceDeps < <(echo "${istioDeps}" | grep -Po 'replace \K.*')
header "Updating deps:"
for dep in "${deps[@]}"; do
lib="${dep%@*}"
istioDep=$(echo "${istioDeps}" | grep -v "replace" | grep "${lib} " || true)
if [ -n "$istioDep" ]; then
newVersion=${istioDep#*\ }
echo "go mod edit -require=${lib}@${newVersion%"// indirect"}"
go mod edit -require=${lib}@${newVersion%"// indirect"}
fi
done
header "Adding explicit replaces:"
for dep in "${replaceDeps[@]}"; do
name=${dep%%\ *}
newVersion=${dep##*\ }
hasDepToReplace "${name}" "${deps[*]}"
if [[ $? -eq 0 ]]; then
echo "go mod edit -replace ${name}=${name}@${newVersion}"
go mod edit -replace=${name}=${name}@${newVersion}
fi
done
@bartoszmajsak
Copy link
Author

bartoszmajsak commented Oct 25, 2022

❯ ./align-deps.sh --version 1.15.2

Updating deps:

go mod edit -require=cloud.google.com/go@v0.102.0 
go mod edit -require=github.com/Azure/go-autorest/autorest/adal@v0.9.20 
go mod edit -require=github.com/Azure/go-autorest/autorest/date@v0.3.0 
go mod edit -require=github.com/Azure/go-autorest/autorest@v0.11.27 
go mod edit -require=github.com/Azure/go-autorest/logger@v0.2.1 
go mod edit -require=github.com/Azure/go-autorest/tracing@v0.6.0 
go mod edit -require=github.com/Azure/go-autorest@v14.2.0+incompatible 
go mod edit -require=github.com/davecgh/go-spew@v1.1.1
go mod edit -require=github.com/evanphx/json-patch@v4.12.0+incompatible 
go mod edit -require=github.com/gogo/protobuf@v1.3.2
go mod edit -require=github.com/golang/protobuf@v1.5.2
go mod edit -require=github.com/go-logr/logr@v1.2.3 
go mod edit -require=github.com/google/gnostic@v0.5.7-v3refs 
go mod edit -require=github.com/google/go-cmp@v0.5.8
go mod edit -require=github.com/google/gofuzz@v1.2.0
go mod edit -require=github.com/go-openapi/jsonpointer@v0.19.5 
go mod edit -require=github.com/go-openapi/jsonreference@v0.19.6 
go mod edit -require=github.com/go-openapi/swag@v0.21.1 
go mod edit -require=github.com/imdario/mergo@v0.3.12 
go mod edit -require=github.com/josharian/intern@v1.0.0 
go mod edit -require=github.com/json-iterator/go@v1.1.12 
go mod edit -require=github.com/mailru/easyjson@v0.7.7 
go mod edit -require=github.com/modern-go/concurrent@v0.0.0-20180306012644-bacd9c7ef1dd 
go mod edit -require=github.com/modern-go/reflect2@v1.0.2 
go mod edit -require=github.com/munnerz/goautoneg@v0.0.0-20191010083416-a7dc8b61c822 
go mod edit -require=github.com/pkg/errors@v0.9.1 
go mod edit -require=github.com/pmezard/go-difflib@v1.0.0
go mod edit -require=github.com/PuerkitoBio/purell@v1.1.1 
go mod edit -require=github.com/PuerkitoBio/urlesc@v0.0.0-20170810143723-de5bf2ad4578 
go mod edit -require=github.com/spf13/pflag@v1.0.5
go mod edit -require=github.com/stretchr/testify@v1.7.2 
go mod edit -require=golang.org/x/crypto@v0.0.0-20220525230936-793ad666bf5e 
go mod edit -require=golang.org/x/exp@v0.0.0-20220407100705-7b9b53b0aca4
go mod edit -require=golang.org/x/mod@v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 
go mod edit -require=golang.org/x/net@v0.0.0-20220624214902-1bab6f366d9e
go mod edit -require=golang.org/x/oauth2@v0.0.0-20220622183110-fd043fe589d2
go mod edit -require=golang.org/x/sys@v0.0.0-20220627191245-f75cf1eec38b
go mod edit -require=golang.org/x/term@v0.0.0-20210927222741-03fcf44c2211 
go mod edit -require=golang.org/x/text@v0.3.7 
go mod edit -require=golang.org/x/time@v0.0.0-20220609170525-579cf78fd858
go mod edit -require=golang.org/x/tools@v0.1.10 
go mod edit -require=golang.org/x/xerrors@v0.0.0-20220609144429-65e65417b02f 
go mod edit -require=google.golang.org/appengine@v1.6.7 
go mod edit -require=google.golang.org/genproto@v0.0.0-20220628213854-d9e0b6570c03
go mod edit -require=google.golang.org/protobuf@v1.28.0
go mod edit -require=gopkg.in/inf.v0@v0.9.1 
go mod edit -require=gopkg.in/yaml.v2@v2.4.0
go mod edit -require=gopkg.in/yaml.v3@v3.0.1
go mod edit -require=istio.io/api@v0.0.0-20221005164339-97dc20dc0ff3
go mod edit -require=istio.io/client-go@v1.15.2-0.20221005164739-2eeb07839129
go mod edit -require=k8s.io/apimachinery@v0.24.2
go mod edit -require=k8s.io/api@v0.24.2
go mod edit -require=k8s.io/client-go@v0.24.2
go mod edit -require=k8s.io/klog/v2@v2.70.0
go mod edit -require=k8s.io/kube-openapi@v0.0.0-20220627174259-011e075b9cb8
go mod edit -require=k8s.io/utils@v0.0.0-20220210201930-3a6ce19ff2f9
go mod edit -require=sigs.k8s.io/controller-runtime@v0.12.2
go mod edit -require=sigs.k8s.io/gateway-api@v0.5.1-0.20220921185115-ee7a83814203
go mod edit -require=sigs.k8s.io/json@v0.0.0-20211208200746-9f7c6b3444d2 
go mod edit -require=sigs.k8s.io/structured-merge-diff/v4@v4.2.1 
go mod edit -require=sigs.k8s.io/yaml@v1.3.0

Adding explicit replaces:

go mod edit -replace github.com/imdario/mergo=github.com/imdario/mergo@v0.3.5
go mod edit -replace sigs.k8s.io/gateway-api=sigs.k8s.io/gateway-api@v0.0.0-20221006194514-510afb48c902

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment