Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update update_dep.sh #18609

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
42 changes: 36 additions & 6 deletions scripts/update_dep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,51 @@ set -euo pipefail

source ./scripts/test_lib.sh

if [ "$#" -ne 2 ]; then
echo "Illegal number of parameters"
exit 1
fi
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also outdates the documentation at the top of the file 😅


mod="$1"
ver="$2"

function maybe_update_module {
function print_current_dep_version {
echo "${mod} version in all go mod files"
grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*$" | grep -v sum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By passing --include to the first grep, I don't think you need to pipe the second grep. It won't match go.sum files.

I think your regular expression can be simplified to "${mod} v". I don't see the value of ^.* and .*$, which matches anything before and after. I'd suggest simplifying.

printf "\n\n"
}

function is_fully_indirect {
# check if all lines end with "// indirect"
# if grep found nothing, the error code will be non-zero
ALL=$(grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*$" | grep -v sum | wc -l)
ONLY_INDIRECT=$(grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*// indirect$" | grep -v sum | wc -l)
if [[ "$ALL" == "$ONLY_INDIRECT" ]]; then
echo "Fully indirect, we will terminate the script"
exit 1
else
echo "Not fully indirect, we will perform dependency bump"
fi
Comment on lines +31 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach would be to use go list for this, i.e., something like:

local result
result=$(find . -name go.mod | xargs -I{} /bin/sh -c 'cd $(dirname {}); go list -f "{{if eq .Path \"'"${mod}"'\"}}{{.Indirect}}{{end}}" -m all' | sort | uniq)
if [ "$result" = "true" ] ; then
   read -p "Module ${mod} is an indirect dependency. Are you sure you want to update it? [y/N] " -r confirm
   [[ "${confirm,,}" == "y" ]] || exit
else
  echo "Not fully..."
fi

}

function update_module {
run go mod tidy

deps=$(go list -f '{{if not .Indirect}}{{if .Version}}{{.Path}},{{.Version}}{{end}}{{end}}' -m all)
deps=$(go list -f '{{if .Version}}{{.Path}},{{.Version}}{{end}}' -m all)
if [[ "$deps" == *"${mod}"* ]]; then
if [ -z "${ver}" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the changes from the top of the file

if [ "$#" -ne 2 ]; then
    echo "Illegal number of parameters"
    exit 1
fi

We will never reach this conditional, as ${ver} will never be empty.

run go get "${mod}"
run go get -u "${mod}"
else
run go get "${mod}@${ver}"
fi
fi
}
}

print_current_dep_version
is_fully_indirect
run_for_modules update_module

./scripts/fix.sh
PASSES="dep" ./scripts/test.sh

go mod tidy
run_for_modules maybe_update_module
print_current_dep_version