Skip to content

Commit 04d409f

Browse files
Add gomate
a simple tool to tidy go modules and update deps Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent 705cd26 commit 04d409f

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ gotools:
4646
godeps: gotools
4747
$(GO) mod download
4848

49+
gotidy:
50+
./scripts/gomate.sh tidy
51+
4952
fpc-sdk: godeps
5053
$(foreach DIR, $(FPC_SDK_DEP_DIRS), $(MAKE) -C $(DIR) build || exit;)
5154

scripts/gomate.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright IBM Corp. All Rights Reserved.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
set -euo pipefail
9+
10+
# lint me: shfmt -i 2 -ci -l -w
11+
12+
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
13+
script_name=$(basename "${0}")
14+
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
15+
readonly script_name script_dir
16+
17+
readonly repo_dir="$(pwd)"
18+
19+
# how to use this script
20+
function usage() {
21+
cli_name=${0##*/}
22+
echo "gomate helps to manage multiple go modules in a single repository.
23+
Usage: $cli_name [command]
24+
Commands:
25+
initwork creates go workspace for this project (\`go work init\` and \`go work use\` everywhere)
26+
tidy runs \`go mod tidy\` everywhere
27+
update [XYZ] updates a specific dep (\`go get XYZ\`) everywhere; if no dep argument given, \`go get -u\` is called
28+
help shows this help
29+
"
30+
exit 1
31+
}
32+
33+
# create go work init
34+
function init_work() {
35+
echo "go work init"
36+
go work init
37+
find "$repo_dir" -iname "go.mod" -exec dirname {} \; | xargs go work use
38+
}
39+
40+
# update deps; take as parameter the dependency to update; if empty all deps are updates
41+
function update() {
42+
if [[ -z $1 ]]; then
43+
# check all update
44+
echo "go get -u everywhere"
45+
find "$repo_dir" -iname "go.mod" -execdir sh -c "go get -u" \;
46+
else
47+
# check a specific dep
48+
echo "go get $1 everywhere"
49+
find "$repo_dir" -iname "go.mod" -execdir sh -c "go get $1" \;
50+
fi
51+
}
52+
53+
# run go mod tidy everywhere
54+
function tidy() {
55+
echo "go mod tidy everywhere"
56+
find "$repo_dir" -iname "go.mod" -execdir sh -c "go mod tidy" \;
57+
}
58+
59+
main() {
60+
case "${1-""}" in
61+
initwork)
62+
init_work
63+
;;
64+
tidy)
65+
tidy
66+
;;
67+
update)
68+
update "${2-""}"
69+
;;
70+
*)
71+
usage
72+
;;
73+
esac
74+
}
75+
76+
main "${@}"

0 commit comments

Comments
 (0)