Skip to content

Commit 535bb6c

Browse files
committedMar 20, 2025··
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>
1 parent 47775a8 commit 535bb6c

File tree

7 files changed

+61
-68
lines changed

7 files changed

+61
-68
lines changed
 

‎Makefile

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ dynbinary: ## build dynamically linked binary
6767

6868
.PHONY: plugins
6969
plugins: ## build example CLI plugins
70-
./scripts/build/plugins
70+
scripts/build/plugins
7171

7272
.PHONY: vendor
7373
vendor: ## update vendor with go modules
7474
rm -rf vendor
75-
./scripts/vendor update
75+
scripts/with-go-mod.sh scripts/vendor update
7676

7777
.PHONY: validate-vendor
7878
validate-vendor: ## validate vendor
79-
./scripts/vendor validate
79+
scripts/with-go-mod.sh scripts/vendor validate
8080

8181
.PHONY: mod-outdated
8282
mod-outdated: ## check outdated dependencies
83-
./scripts/vendor outdated
83+
scripts/with-go-mod.sh scripts/vendor outdated
8484

8585
.PHONY: authors
8686
authors: ## generate AUTHORS file from git history
@@ -115,15 +115,15 @@ shell-completion: ## generate shell-completion scripts
115115

116116
.PHONY: manpages
117117
manpages: ## generate man pages from go source and markdown
118-
scripts/docs/generate-man.sh
118+
scripts/with-go-mod.sh scripts/docs/generate-man.sh
119119

120120
.PHONY: mddocs
121121
mddocs: ## generate markdown files from go source
122-
scripts/docs/generate-md.sh
122+
scripts/with-go-mod.sh scripts/docs/generate-md.sh
123123

124124
.PHONY: yamldocs
125125
yamldocs: ## generate documentation YAML files consumed by docs repo
126-
scripts/docs/generate-yaml.sh
126+
scripts/with-go-mod.sh scripts/docs/generate-yaml.sh
127127

128128
.PHONY: help
129129
help: ## print this help

‎dockerfiles/Dockerfile.vendor

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RUN --mount=target=/context \
1616
--mount=target=/go/pkg/mod,type=cache <<EOT
1717
set -e
1818
rsync -a /context/. .
19-
./scripts/vendor update
19+
./scripts/with-go-mod.sh ./scripts/vendor update
2020
mkdir /out
2121
cp -r vendor.mod vendor.sum vendor /out
2222
EOT
@@ -32,12 +32,12 @@ rsync -a /context/. .
3232
git add -A
3333
rm -rf vendor
3434
cp -rf /out/* .
35-
./scripts/vendor validate
35+
./scripts/with-go-mod.sh ./scripts/vendor validate
3636
EOT
3737

3838
FROM psampaz/go-mod-outdated:${MODOUTDATED_VERSION} AS go-mod-outdated
3939
FROM base AS outdated
4040
RUN --mount=target=.,rw \
4141
--mount=target=/go/pkg/mod,type=cache \
4242
--mount=from=go-mod-outdated,source=/home/go-mod-outdated,target=/usr/bin/go-mod-outdated \
43-
./scripts/vendor outdated
43+
./scripts/with-go-mod.sh ./scripts/vendor outdated

‎scripts/docs/generate-man.sh

+16-15
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22

33
set -eu
44

5-
: "${MD2MAN_VERSION=v2.0.6}"
5+
: "${GO_MD2MAN:=go-md2man}"
66

7-
function clean() {
8-
rm -f go.mod
9-
}
10-
11-
export GO111MODULE=auto
12-
trap clean EXIT
13-
14-
./scripts/vendor init
15-
# build gen-manpages
16-
go build -mod=vendor -modfile=vendor.mod -tags manpages -o /tmp/gen-manpages ./man/generate.go
17-
# build go-md2man
18-
go build -mod=vendor -modfile=vendor.mod -o /tmp/go-md2man ./vendor/github.com/cpuguy83/go-md2man/v2
7+
if ! command -v "$GO_MD2MAN" > /dev/null; then
8+
(
9+
set -x
10+
go build -mod=vendor -modfile=vendor.mod -o ./build/tools/go-md2man ./vendor/github.com/cpuguy83/go-md2man/v2
11+
)
12+
GO_MD2MAN=$(realpath ./build/tools/go-md2man)
13+
fi
1914

2015
mkdir -p man/man1
21-
(set -x ; /tmp/gen-manpages --root "." --target "$(pwd)/man/man1")
16+
(
17+
set -x
18+
go run -mod=vendor -modfile=vendor.mod -tags manpages ./man/generate.go --root "." --target "./man/man1"
19+
)
2220

2321
(
2422
cd man
@@ -31,6 +29,9 @@ mkdir -p man/man1
3129
continue
3230
fi
3331
mkdir -p "./man${num}"
34-
(set -x ; /tmp/go-md2man -in "$FILE" -out "./man${num}/${name}")
32+
(
33+
set -x ;
34+
"$GO_MD2MAN" -in "$FILE" -out "./man${num}/${name}"
35+
)
3536
done
3637
)

‎scripts/docs/generate-md.sh

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,9 @@
22

33
set -eu
44

5-
: "${CLI_DOCS_TOOL_VERSION=v0.9.0}"
6-
7-
function clean() {
8-
rm -f go.mod
9-
}
10-
11-
export GO111MODULE=auto
12-
trap clean EXIT
13-
14-
./scripts/vendor init
15-
# build docsgen
16-
go build -mod=vendor -modfile=vendor.mod -tags docsgen -o /tmp/docsgen ./docs/generate/generate.go
17-
185
(
196
set -x
20-
/tmp/docsgen --formats md --source "$(pwd)/docs/reference/commandline" --target "$(pwd)/docs/reference/commandline"
7+
go run -mod=vendor -modfile=vendor.mod -tags docsgen ./docs/generate/generate.go --formats md --source "./docs/reference/commandline" --target "./docs/reference/commandline"
218
)
229

2310
# remove generated help.md file

‎scripts/docs/generate-yaml.sh

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
set -eu
44

5-
: "${CLI_DOCS_TOOL_VERSION=v0.9.0}"
6-
7-
function clean() {
8-
rm -f go.mod
9-
}
10-
11-
export GO111MODULE=auto
12-
trap clean EXIT
13-
14-
./scripts/vendor init
15-
# build docsgen
16-
go build -mod=vendor -modfile=vendor.mod -tags docsgen -o /tmp/docsgen ./docs/generate/generate.go
17-
185
mkdir -p docs/yaml
196
set -x
20-
/tmp/docsgen --formats yaml --source "$(pwd)/docs/reference/commandline" --target "$(pwd)/docs/yaml"
7+
go run -mod=vendor -modfile=vendor.mod -tags docsgen ./docs/generate/generate.go --formats yaml --source "./docs/reference/commandline" --target "./docs/yaml"

‎scripts/vendor

-15
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ if [ -z "$TYP" ]; then
1313
usage
1414
fi
1515

16-
init() {
17-
# create dummy go.mod, see comment in vendor.mod
18-
cat > go.mod <<EOL
19-
module github.com/docker/cli
20-
21-
go 1.23.0
22-
EOL
23-
}
24-
2516
update() {
2617
(set -x ; go mod tidy -modfile=vendor.mod; go mod vendor -modfile=vendor.mod)
2718
}
@@ -44,20 +35,14 @@ outdated() {
4435
}
4536

4637
case $TYP in
47-
"init")
48-
init
49-
;;
5038
"update")
51-
init
5239
update
5340
;;
5441
"validate")
55-
init
5642
update
5743
validate
5844
;;
5945
"outdated")
60-
init
6146
outdated
6247
;;
6348
*)

‎scripts/with-go-mod.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script is used to coerce certain commands which rely on the presence of
4+
# a go.mod into working with our repository. It works by creating a fake
5+
# go.mod, running a specified command (passed via arguments), and removing it
6+
# when the command is finished. This script should be dropped when this
7+
# repository is a proper Go module with a permanent go.mod.
8+
9+
set -e
10+
11+
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12+
ROOTDIR="$(cd "${SCRIPTDIR}/.." && pwd)"
13+
14+
if test -e "${ROOTDIR}/go.mod"; then
15+
{
16+
scriptname=$(basename "$0")
17+
cat >&2 <<- EOF
18+
$scriptname: WARN: go.mod exists in the repository root!
19+
$scriptname: WARN: Using your go.mod instead of our generated version -- this may misbehave!
20+
EOF
21+
} >&2
22+
else
23+
set -x
24+
25+
tee "${ROOTDIR}/go.mod" >&2 <<- EOF
26+
module github.com/docker/cli
27+
28+
go 1.23.0
29+
EOF
30+
trap 'rm -f "${ROOTDIR}/go.mod"' EXIT
31+
fi
32+
33+
GO111MODULE=on GOTOOLCHAIN=local "$@"

0 commit comments

Comments
 (0)
Please sign in to comment.