From 535bb6c85c77612486d97997d4c0f8e8182d2dd7 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 18 Mar 2025 17:54:04 +0100 Subject: [PATCH] rewrite using "with-go-mod.sh" script and "go run" Use the same script as is used in moby/moby, which more gracefully handles an existing `go.mod` (which can be symlinked) into account. - keep the scripts called generic, and update the Makefile to invoke them with the "with-go-mod.sh" script. - use "go run" instead of building temporary binaries - check if go-md2man exists before building a binary Signed-off-by: Sebastiaan van Stijn --- Makefile | 14 +++++++------- dockerfiles/Dockerfile.vendor | 6 +++--- scripts/docs/generate-man.sh | 31 ++++++++++++++++--------------- scripts/docs/generate-md.sh | 15 +-------------- scripts/docs/generate-yaml.sh | 15 +-------------- scripts/vendor | 15 --------------- scripts/with-go-mod.sh | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 68 deletions(-) create mode 100755 scripts/with-go-mod.sh diff --git a/Makefile b/Makefile index 976a7356b4..0ec30ba668 100644 --- a/Makefile +++ b/Makefile @@ -67,20 +67,20 @@ dynbinary: ## build dynamically linked binary .PHONY: plugins plugins: ## build example CLI plugins - ./scripts/build/plugins + scripts/build/plugins .PHONY: vendor vendor: ## update vendor with go modules rm -rf vendor - ./scripts/vendor update + scripts/with-go-mod.sh scripts/vendor update .PHONY: validate-vendor validate-vendor: ## validate vendor - ./scripts/vendor validate + scripts/with-go-mod.sh scripts/vendor validate .PHONY: mod-outdated mod-outdated: ## check outdated dependencies - ./scripts/vendor outdated + scripts/with-go-mod.sh scripts/vendor outdated .PHONY: authors authors: ## generate AUTHORS file from git history @@ -115,15 +115,15 @@ shell-completion: ## generate shell-completion scripts .PHONY: manpages manpages: ## generate man pages from go source and markdown - scripts/docs/generate-man.sh + scripts/with-go-mod.sh scripts/docs/generate-man.sh .PHONY: mddocs mddocs: ## generate markdown files from go source - scripts/docs/generate-md.sh + scripts/with-go-mod.sh scripts/docs/generate-md.sh .PHONY: yamldocs yamldocs: ## generate documentation YAML files consumed by docs repo - scripts/docs/generate-yaml.sh + scripts/with-go-mod.sh scripts/docs/generate-yaml.sh .PHONY: help help: ## print this help diff --git a/dockerfiles/Dockerfile.vendor b/dockerfiles/Dockerfile.vendor index f557838405..9a7f860120 100644 --- a/dockerfiles/Dockerfile.vendor +++ b/dockerfiles/Dockerfile.vendor @@ -16,7 +16,7 @@ RUN --mount=target=/context \ --mount=target=/go/pkg/mod,type=cache < /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_MD2MAN=$(realpath ./build/tools/go-md2man) +fi mkdir -p man/man1 -(set -x ; /tmp/gen-manpages --root "." --target "$(pwd)/man/man1") +( + set -x + go run -mod=vendor -modfile=vendor.mod -tags manpages ./man/generate.go --root "." --target "./man/man1" +) ( cd man @@ -31,6 +29,9 @@ mkdir -p man/man1 continue fi mkdir -p "./man${num}" - (set -x ; /tmp/go-md2man -in "$FILE" -out "./man${num}/${name}") + ( + set -x ; + "$GO_MD2MAN" -in "$FILE" -out "./man${num}/${name}" + ) done ) diff --git a/scripts/docs/generate-md.sh b/scripts/docs/generate-md.sh index 5715db63a6..1152787738 100755 --- a/scripts/docs/generate-md.sh +++ b/scripts/docs/generate-md.sh @@ -2,22 +2,9 @@ set -eu -: "${CLI_DOCS_TOOL_VERSION=v0.9.0}" - -function clean() { - rm -f go.mod -} - -export GO111MODULE=auto -trap clean EXIT - -./scripts/vendor init -# build docsgen -go build -mod=vendor -modfile=vendor.mod -tags docsgen -o /tmp/docsgen ./docs/generate/generate.go - ( set -x - /tmp/docsgen --formats md --source "$(pwd)/docs/reference/commandline" --target "$(pwd)/docs/reference/commandline" + go run -mod=vendor -modfile=vendor.mod -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 c583a420e4..c6d30a6c77 100755 --- a/scripts/docs/generate-yaml.sh +++ b/scripts/docs/generate-yaml.sh @@ -2,19 +2,6 @@ set -eu -: "${CLI_DOCS_TOOL_VERSION=v0.9.0}" - -function clean() { - rm -f go.mod -} - -export GO111MODULE=auto -trap clean EXIT - -./scripts/vendor init -# build docsgen -go build -mod=vendor -modfile=vendor.mod -tags docsgen -o /tmp/docsgen ./docs/generate/generate.go - mkdir -p docs/yaml set -x -/tmp/docsgen --formats yaml --source "$(pwd)/docs/reference/commandline" --target "$(pwd)/docs/yaml" +go run -mod=vendor -modfile=vendor.mod -tags docsgen ./docs/generate/generate.go --formats yaml --source "./docs/reference/commandline" --target "./docs/yaml" diff --git a/scripts/vendor b/scripts/vendor index 1bcddfb9b8..d7bde8f928 100755 --- a/scripts/vendor +++ b/scripts/vendor @@ -13,15 +13,6 @@ if [ -z "$TYP" ]; then usage fi -init() { - # create dummy go.mod, see comment in vendor.mod - cat > go.mod <&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 + set -x + + tee "${ROOTDIR}/go.mod" >&2 <<- EOF + module github.com/docker/cli + + go 1.23.0 + EOF + trap 'rm -f "${ROOTDIR}/go.mod"' EXIT +fi + +GO111MODULE=on GOTOOLCHAIN=local "$@"