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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-03-18 17:54:04 +01:00
parent 47775a8fa0
commit 535bb6c85c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
7 changed files with 61 additions and 68 deletions

View File

@ -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

View File

@ -16,7 +16,7 @@ RUN --mount=target=/context \
--mount=target=/go/pkg/mod,type=cache <<EOT
set -e
rsync -a /context/. .
./scripts/vendor update
./scripts/with-go-mod.sh ./scripts/vendor update
mkdir /out
cp -r vendor.mod vendor.sum vendor /out
EOT
@ -32,7 +32,7 @@ rsync -a /context/. .
git add -A
rm -rf vendor
cp -rf /out/* .
./scripts/vendor validate
./scripts/with-go-mod.sh ./scripts/vendor validate
EOT
FROM psampaz/go-mod-outdated:${MODOUTDATED_VERSION} AS go-mod-outdated
@ -40,4 +40,4 @@ FROM base AS outdated
RUN --mount=target=.,rw \
--mount=target=/go/pkg/mod,type=cache \
--mount=from=go-mod-outdated,source=/home/go-mod-outdated,target=/usr/bin/go-mod-outdated \
./scripts/vendor outdated
./scripts/with-go-mod.sh ./scripts/vendor outdated

View File

@ -2,23 +2,21 @@
set -eu
: "${MD2MAN_VERSION=v2.0.6}"
: "${GO_MD2MAN:=go-md2man}"
function clean() {
rm -f go.mod
}
export GO111MODULE=auto
trap clean EXIT
./scripts/vendor init
# build gen-manpages
go build -mod=vendor -modfile=vendor.mod -tags manpages -o /tmp/gen-manpages ./man/generate.go
# build go-md2man
go build -mod=vendor -modfile=vendor.mod -o /tmp/go-md2man ./vendor/github.com/cpuguy83/go-md2man/v2
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_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
)

View File

@ -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

View File

@ -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"

View File

@ -13,15 +13,6 @@ if [ -z "$TYP" ]; then
usage
fi
init() {
# create dummy go.mod, see comment in vendor.mod
cat > go.mod <<EOL
module github.com/docker/cli
go 1.23.0
EOL
}
update() {
(set -x ; go mod tidy -modfile=vendor.mod; go mod vendor -modfile=vendor.mod)
}
@ -44,20 +35,14 @@ outdated() {
}
case $TYP in
"init")
init
;;
"update")
init
update
;;
"validate")
init
update
validate
;;
"outdated")
init
outdated
;;
*)

33
scripts/with-go-mod.sh Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
#
# This script is used to coerce certain commands which rely on the presence of
# a go.mod into working with our repository. It works by creating a fake
# go.mod, running a specified command (passed via arguments), and removing it
# 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
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
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 "$@"