Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

feat: Go workspaces support #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
description: "Runs `goimports`, requires golang"
- id: go-vet
name: 'go vet'
entry: run-go-vet.sh
entry: run-go-vet.py
files: '\.go$'
language: 'script'
description: "Runs `go vet`, requires golang"
Expand Down Expand Up @@ -83,7 +83,7 @@
description: "Runs `go generate`, requires golang"
- id: go-mod-tidy
name: 'go-mod-tidy'
entry: run-go-mod-tidy.sh
entry: run-go-mod-tidy.py
pass_filenames: false
language: 'script'
description: "Runs `go mod tidy -v`, requires golang"
Expand Down
35 changes: 35 additions & 0 deletions _workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

import json
import os
import subprocess


def get_package_path(path, modules):
for module in modules:
mod_path = module["AbsolutePath"]
file_path = os.path.abspath(path)

if file_path == mod_path or file_path.startswith(mod_path + os.path.sep):
return module["ModPath"] + file_path[len(mod_path) :]

raise ValueError("no module found for path")


def get_modules():
if not os.path.exists("go.work"):
return {
"AbsolutePath": os.path.abspath("."),
"ModPath": subprocess.check_output(["go", "list"]),
}

workspace_config = json.loads(
subprocess.check_output(["go", "work", "edit", "-json"])
)
return [
{
"AbsolutePath": os.path.abspath(workspace["DiskPath"]),
"ModPath": workspace["ModPath"],
}
for workspace in workspace_config["Use"]
]
11 changes: 10 additions & 1 deletion run-go-build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#!/usr/bin/env bash
FILES=$(go list ./... | grep -v /vendor/)

set -eu -o pipefail

MODULES=.
if [[ -f go.work ]]; then
MODULES="$(go work edit -json | python -c 'import json,sys;print("\n".join(w["ModPath"] for w in json.load(sys.stdin)["Use"]))')"
fi

FILES="$(xargs -I '{}' go list '{}/...' <<< "${MODULES}" | grep -v /vendor/)"

exec go build $FILES
32 changes: 32 additions & 0 deletions run-go-mod-tidy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

import subprocess
import sys

from _workspace import get_modules


def main(args):
modules = get_modules()
for module in modules:
subprocess.check_call(
["go", "mod", "tidy"],
cwd=module['AbsolutePath'],
)

try:
subprocess.check_call(
["git", "diff", "--exit-code", "go.mod", "go.sum"],
cwd=module['AbsolutePath'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError:
print("go.mod or go.sum differs, please re-add it to your commit", file=sys.stderr)
return 3

return 0


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
9 changes: 8 additions & 1 deletion run-go-unit-tests.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env bash

set -eu -o pipefail

fail() {
echo "unit tests failed"
exit 1
}

FILES=$(go list ./... | grep -v /vendor/) || fail
MODULES=.
if [[ -f go.work ]]; then
MODULES="$(go work edit -json | python -c 'import json,sys;print("\n".join(w["ModPath"] for w in json.load(sys.stdin)["Use"]))')"
fi

FILES=$(xargs -I '{}' go list '{}/...' <<< "${MODULES}" | grep -v /vendor/) || fail
go test -tags=unit -timeout 30s -short -v ${FILES} || fail
16 changes: 16 additions & 0 deletions run-go-vet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

import os
import sys

from _workspace import get_modules, get_package_path


def main(args):
modules = get_modules()
paths = set(get_package_path(os.path.dirname(arg), modules) for arg in args)
os.execvp("go", ["go", "vet", *paths])


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
6 changes: 0 additions & 6 deletions run-go-vet.sh

This file was deleted.