From 537acbe98b4467f78873bce20a5cb9694f3e0f80 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 25 Apr 2025 18:27:11 +0200 Subject: [PATCH 1/2] scripts/with-go-mod.sh: use symlink instead of -modfile While the "-modfile=vendor.mod" is slightly more correct, using a symlink allows for most of the go tools to work "as usual", just without an acual `go.mod` being committed in the repository. Signed-off-by: Sebastiaan van Stijn --- scripts/docs/generate-man.sh | 4 ++-- scripts/docs/generate-md.sh | 2 +- scripts/docs/generate-yaml.sh | 2 +- scripts/vendor | 4 ++-- scripts/with-go-mod.sh | 40 ++++++++++++++++++++++------------- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/scripts/docs/generate-man.sh b/scripts/docs/generate-man.sh index cc8f826cb63c..23a7bc971c4a 100755 --- a/scripts/docs/generate-man.sh +++ b/scripts/docs/generate-man.sh @@ -7,7 +7,7 @@ set -eu if ! command -v "$GO_MD2MAN" > /dev/null; then ( set -x - go build -mod=vendor -modfile=vendor.mod -o ./build/tools/go-md2man ./vendor/github.com/cpuguy83/go-md2man/v2 + go build -mod=vendor -o ./build/tools/go-md2man ./vendor/github.com/cpuguy83/go-md2man/v2 ) GO_MD2MAN=$(realpath ./build/tools/go-md2man) fi @@ -15,7 +15,7 @@ fi mkdir -p man/man1 ( set -x - go run -mod=vendor -modfile=vendor.mod -tags manpages ./man/generate.go --source "./man/src" --target "./man/man1" + go run -mod=vendor -tags manpages ./man/generate.go --source "./man/src" --target "./man/man1" ) ( diff --git a/scripts/docs/generate-md.sh b/scripts/docs/generate-md.sh index 1152787738ea..cf5b1fb77b6d 100755 --- a/scripts/docs/generate-md.sh +++ b/scripts/docs/generate-md.sh @@ -4,7 +4,7 @@ set -eu ( set -x - go run -mod=vendor -modfile=vendor.mod -tags docsgen ./docs/generate/generate.go --formats md --source "./docs/reference/commandline" --target "./docs/reference/commandline" + go run -mod=vendor -tags docsgen ./docs/generate/generate.go --formats md --source "./docs/reference/commandline" --target "./docs/reference/commandline" ) # remove generated help.md file diff --git a/scripts/docs/generate-yaml.sh b/scripts/docs/generate-yaml.sh index c6d30a6c772a..2e19fb7cd934 100755 --- a/scripts/docs/generate-yaml.sh +++ b/scripts/docs/generate-yaml.sh @@ -4,4 +4,4 @@ set -eu mkdir -p docs/yaml set -x -go run -mod=vendor -modfile=vendor.mod -tags docsgen ./docs/generate/generate.go --formats yaml --source "./docs/reference/commandline" --target "./docs/yaml" +go run -mod=vendor -tags docsgen ./docs/generate/generate.go --formats yaml --source "./docs/reference/commandline" --target "./docs/yaml" diff --git a/scripts/vendor b/scripts/vendor index d7bde8f92842..714acafa0e26 100755 --- a/scripts/vendor +++ b/scripts/vendor @@ -14,7 +14,7 @@ if [ -z "$TYP" ]; then fi update() { - (set -x ; go mod tidy -modfile=vendor.mod; go mod vendor -modfile=vendor.mod) + (set -x ; go mod tidy; go mod vendor) } validate() { @@ -31,7 +31,7 @@ outdated() { echo "go-mod-outdated not found. Install with 'go install github.com/psampaz/go-mod-outdated@v0.8.0'" exit 1 fi - (set -x ; go list -mod=vendor -mod=readonly -modfile=vendor.mod -u -m -json all | go-mod-outdated -update -direct) + (set -x ; go list -mod=readonly -u -m -json all | go-mod-outdated -update -direct) } case $TYP in diff --git a/scripts/with-go-mod.sh b/scripts/with-go-mod.sh index fc5cfd2cf6e5..6508f3935bac 100755 --- a/scripts/with-go-mod.sh +++ b/scripts/with-go-mod.sh @@ -6,28 +6,38 @@ # when the command is finished. This script should be dropped when this # repository is a proper Go module with a permanent go.mod. -set -e +set -euo pipefail SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOTDIR="$(cd "${SCRIPTDIR}/.." && pwd)" -if test -e "${ROOTDIR}/go.mod"; then - { - scriptname=$(basename "$0") - cat >&2 <<- EOF - $scriptname: WARN: go.mod exists in the repository root! - $scriptname: WARN: Using your go.mod instead of our generated version -- this may misbehave! - EOF - } >&2 -else +cleanup_paths=() + +create_symlink() { + local target="$1" + local link="$2" + + if [ -L "$link" ] && [ "$(readlink "$link")" = "$target" ]; then + # symlink already present; we're done + return + fi + + if [ -e "$link" ]; then + echo "$(basename "$0"): WARN: $link exists but is not the expected symlink!" >&2 + echo "$(basename "$0"): WARN: Using your version instead of our generated version -- this may misbehave!" >&2 + return + fi + set -x + ln -s "$target" "$link" + cleanup_paths+=( "$link" ) +} - tee "${ROOTDIR}/go.mod" >&2 <<- EOF - module github.com/docker/cli +create_symlink "vendor.mod" "${ROOTDIR}/go.mod" +create_symlink "vendor.sum" "${ROOTDIR}/go.sum" - go 1.23.0 - EOF - trap 'rm -f "${ROOTDIR}/go.mod"' EXIT +if [ "${#cleanup_paths[@]}" -gt 0 ]; then + trap 'rm -f "${cleanup_paths[@]}"' EXIT fi GO111MODULE=on GOTOOLCHAIN=local "$@" From 4464134c5b5710c965e9ff7b646ae69a1b05517b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Apr 2025 00:33:34 +0200 Subject: [PATCH 2/2] scripts/with-go-mod.sh: simplify symlink comparison Make sure the symlinks point to the same file; not if both are identical (absolute or relative symlink); it's only for suppressing the warning so not critical. Signed-off-by: Sebastiaan van Stijn --- scripts/with-go-mod.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scripts/with-go-mod.sh b/scripts/with-go-mod.sh index 6508f3935bac..3b9c9dd80534 100755 --- a/scripts/with-go-mod.sh +++ b/scripts/with-go-mod.sh @@ -17,14 +17,12 @@ create_symlink() { local target="$1" local link="$2" - if [ -L "$link" ] && [ "$(readlink "$link")" = "$target" ]; then - # symlink already present; we're done - return - fi - if [ -e "$link" ]; then - echo "$(basename "$0"): WARN: $link exists but is not the expected symlink!" >&2 - echo "$(basename "$0"): WARN: Using your version instead of our generated version -- this may misbehave!" >&2 + # see https://superuser.com/a/196698 + if ! [ "$link" -ef "${ROOTDIR}/${target}" ]; then + echo "$(basename "$0"): WARN: $link exists but is not the expected symlink!" >&2 + echo "$(basename "$0"): WARN: Using your version instead of our generated version -- this may misbehave!" >&2 + fi return fi