1
- #! /bin/bash
1
+ #! /usr/ bin/env bash
2
2
# Copyright 2025 The etcd Authors
3
3
#
4
4
# Licensed under the Apache License, Version 2.0 (the "License");
15
15
#
16
16
# Usage:
17
17
# ./scripts/update_dep.sh module version
18
- # or ./scripts/update_dep.sh module
18
+ # or ./scripts/update_dep.sh module (to update to the latest version)
19
19
# e.g.
20
20
# ./scripts/update_dep.sh github.com/golang/groupcache
21
21
# ./scripts/update_dep.sh github.com/soheilhy/cmux v0.1.5
@@ -26,48 +26,57 @@ set -euo pipefail
26
26
27
27
source ./scripts/test_lib.sh
28
28
29
- if [ " $# " -ne 2 ]; then
30
- echo " Illegal number of parameters"
29
+ if [ " $# " -lt 1 ] || [ " $# " -gt 2 ]; then
30
+ log_error " Illegal number of parameters. Usage: $0 module [version] "
31
31
exit 1
32
32
fi
33
33
34
34
mod=" $1 "
35
- ver=" $2 "
35
+ ver=" ${2 :- } "
36
36
37
37
function print_current_dep_version {
38
- echo " ${mod} version in all go mod files"
39
- grep --exclude-dir=.git --include= \* .mod -Ri " ^. *${mod} v.*$ " | grep -v sum
40
- printf " \n\n "
38
+ log_info " ${mod} version in all go. mod files: "
39
+ find . -name go .mod -exec grep -H " ^\s *${mod} \s " {} + | sed ' s|:|\t| ' || true
40
+ printf " \n"
41
41
}
42
42
43
43
function is_fully_indirect {
44
- # check if all lines end with "// indirect"
45
- # if grep found nothing, the error code will be non-zero
46
- ALL=$( grep --exclude-dir=.git --include=\* .mod -Ri " ^.*${mod} v.*$" | grep -v sum | wc -l)
47
- ONLY_INDIRECT=$( grep --exclude-dir=.git --include=\* .mod -Ri " ^.*${mod} v.*// indirect$" | grep -v sum | wc -l)
48
- if [[ " $ALL " == " $ONLY_INDIRECT " ]]; then
49
- echo " Fully indirect, we will terminate the script"
50
- exit 1
51
- else
52
- echo " Not fully indirect, we will perform dependency bump"
44
+ # Returns 0 (true) if the dependency is fully indirect, 1 (false) otherwise.
45
+ local all_lines
46
+ all_lines=$( find . -name go.mod -exec grep -E " ^\s*${mod} \s" {} + || true)
47
+ if [ -z " ${all_lines} " ]; then
48
+ # Not a dependency anywhere.
49
+ return 0
53
50
fi
51
+
52
+ local direct_lines
53
+ direct_lines=$( echo " ${all_lines} " | grep -v " // indirect" )
54
+
55
+ if [ -z " ${direct_lines} " ]; then
56
+ return 0 # true, fully indirect
57
+ fi
58
+ return 1 # false, has direct dependencies
54
59
}
55
60
56
61
function update_module {
57
- run go mod tidy
58
-
59
- deps=$( go list -f ' {{if .Version}}{{.Path}},{{.Version}}{{end}}' -m all)
60
- if [[ " $deps " == * " ${mod} " * ]]; then
62
+ # Check if the module is a dependency.
63
+ if go list -m all | grep -q -E " ^\s*${mod} \s" ; then
61
64
if [ -z " ${ver} " ]; then
65
+ log_info " Updating ${mod} to latest version in $( module_subdir) ..."
62
66
run go get -u " ${mod} "
63
67
else
68
+ log_info " Updating ${mod} to version ${ver} in $( module_subdir) ..."
64
69
run go get " ${mod} @${ver} "
65
70
fi
66
71
fi
67
72
}
68
73
69
74
print_current_dep_version
70
- is_fully_indirect
75
+ if is_fully_indirect; then
76
+ log_warning " Dependency '${mod} ' is fully indirect or not used. Nothing to do."
77
+ exit 0
78
+ fi
79
+
71
80
run_for_modules update_module
72
81
73
82
./scripts/fix.sh
0 commit comments