浏览代码

hack: replace go-mod-prepare.sh with wrapper script

To make the local build environment more correct and consistent, we
should never leave an uncommitted go.mod in the tree; however, we need a
go.mod for certain commands to work properly. Use a wrapper script to
create and destroy the go.mod as needed instead of potentially changing
tooling behavior by leaving it.

If a go.mod already exists, this script will warn and call the wrapped
command with GO111MODULE=on.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
Bjorn Neergaard 2 年之前
父节点
当前提交
a449f77774
共有 5 个文件被更改,包括 34 次插入24 次删除
  1. 0 1
      .github/workflows/buildkit.yml
  2. 1 4
      hack/buildkit-ref
  3. 0 14
      hack/go-mod-prepare.sh
  4. 2 5
      hack/vendor.sh
  5. 31 0
      hack/with-go-mod.sh

+ 0 - 1
.github/workflows/buildkit.yml

@@ -69,7 +69,6 @@ jobs:
       -
       -
         name: BuildKit ref
         name: BuildKit ref
         run: |
         run: |
-          ./hack/go-mod-prepare.sh
           # FIXME(thaJeztah) temporarily overriding version to use for tests; remove with the next release of buildkit
           # FIXME(thaJeztah) temporarily overriding version to use for tests; remove with the next release of buildkit
           # echo "BUILDKIT_REF=$(./hack/buildkit-ref)" >> $GITHUB_ENV
           # echo "BUILDKIT_REF=$(./hack/buildkit-ref)" >> $GITHUB_ENV
           echo "BUILDKIT_REF=0bfcd83e6db95e6c6877ee6e5224b994cea62ba1" >> $GITHUB_ENV
           echo "BUILDKIT_REF=0bfcd83e6db95e6c6877ee6e5224b994cea62ba1" >> $GITHUB_ENV

+ 1 - 4
hack/buildkit-ref

@@ -9,11 +9,8 @@ if [ -n "$BUILDKIT_REF" ]; then
 	exit 0
 	exit 0
 fi
 fi
 
 
-# prepare go mod
-./hack/go-mod-prepare.sh
-
 # get buildkit version from vendor.mod
 # get buildkit version from vendor.mod
-BUILDKIT_REF=$(GO111MODULE=on go list -mod=mod -modfile=vendor.mod -u -m -f '{{.Version}}' "github.com/${BUILDKIT_REPO}")
+BUILDKIT_REF=$(./hack/with-go-mod.sh go list -mod=mod -modfile=vendor.mod -u -m -f '{{.Version}}' "github.com/${BUILDKIT_REPO}")
 if [[ "${BUILDKIT_REF}" == *-*-* ]]; then
 if [[ "${BUILDKIT_REF}" == *-*-* ]]; then
 	# if pseudo-version, figure out just the uncommon sha (https://github.com/golang/go/issues/34745)
 	# if pseudo-version, figure out just the uncommon sha (https://github.com/golang/go/issues/34745)
 	BUILDKIT_REF=$(echo "${BUILDKIT_REF}" | awk -F"-" '{print $NF}' | awk 'BEGIN{FIELDWIDTHS="7"} {print $1}')
 	BUILDKIT_REF=$(echo "${BUILDKIT_REF}" | awk -F"-" '{print $NF}' | awk 'BEGIN{FIELDWIDTHS="7"} {print $1}')

+ 0 - 14
hack/go-mod-prepare.sh

@@ -1,14 +0,0 @@
-#!/usr/bin/env bash
-
-set -e
-
-SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-ROOTDIR="$(git -C "$SCRIPTDIR" rev-parse --show-toplevel)"
-
-set -x
-
-tee "${ROOTDIR}/go.mod" << EOF
-module github.com/docker/docker
-
-go 1.18
-EOF

+ 2 - 5
hack/vendor.sh

@@ -7,18 +7,15 @@
 set -e
 set -e
 
 
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-"${SCRIPTDIR}"/go-mod-prepare.sh
-
-export GO111MODULE=on
 
 
 tidy() (
 tidy() (
 		set -x
 		set -x
-		go mod tidy -modfile vendor.mod -compat 1.18
+		"${SCRIPTDIR}"/with-go-mod.sh go mod tidy -modfile vendor.mod -compat 1.18
 )
 )
 
 
 vendor() (
 vendor() (
 		set -x
 		set -x
-		go mod vendor -modfile vendor.mod
+		"${SCRIPTDIR}"/with-go-mod.sh go mod vendor -modfile vendor.mod
 )
 )
 
 
 help() {
 help() {

+ 31 - 0
hack/with-go-mod.sh

@@ -0,0 +1,31 @@
+#!/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="$(git -C "$SCRIPTDIR" rev-parse --show-toplevel)"
+
+if test -e "${ROOTDIR}/go.mod"; then
+	{
+		scriptname=$(basename "$0")
+		echo "${scriptname}: WARN: go.mod exists in the repository root!"
+		echo "${scriptname}: WARN: Using your go.mod instead of our generated version -- this may misbehave!"
+	} >&2
+else
+	set -x
+
+	tee "${ROOTDIR}/go.mod" >&2 <<- EOF
+		module github.com/docker/docker
+
+		go 1.18
+	EOF
+	trap 'rm -f "${ROOTDIR}/go.mod"' EXIT
+fi
+
+GO111MODULE=on "$@"