Repository: etcd-io/etcd
File: scripts/update_dep.sh (Shell)

Code before:
#
# Updates version of given dependency in all the modules that depend on the mod.

set -euo pipefail

source ./scripts/test_lib.sh

if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
    log_error "Illegal number of parameters. Usage: $0 module [version]"
    exit 1
fi

mod="$1"
ver="${2:-}"

function print_current_dep_version {
  log_info "${mod} version in all go.mod files:"
  find . -name go.mod -exec grep -H "^\s*${mod}\s" {} + | sed 's|:|\t|' || true
  printf "\n"
}

function is_fully_indirect {
  local result
  result=$(find . -name go.mod -print0 | xargs -0 -I{} /bin/sh -c "cd \$(dirname {}); go list -f \"{{if eq .Path \\\"${mod}\\\"}}{{.Indirect}}{{end}}\" -m all" | sort | uniq)
  if [ "$result" = "true" ] ; then
    return 0
  else
    return 1
  fi
}

function update_module {
  # The `go get` command is most effective on dependencies that are explicitly
  # listed as direct requirements in the go.mod file. When updating a purely
  # indirect dependency, `go get` might not update it as expected.
  #
  # To work around this, we temporarily promote the indirect dependency to a
  # direct one in the go.mod file using `go mod edit`. This ensures that
  # `go get` will see and correctly update the module. Subsequent cleanup
  # commands (like `go mod tidy` in `fix.sh`) will automatically move it back
  # to being an indirect dependency, but at the newly updated version.
  log_info "Adding '${mod}' to go.mod for update."
  run go mod edit -require "${mod}@${ver:-latest}" || true

  # Check if the module is a dependency.
  if go list -m all | grep -q -E "^\s*${mod}\s"; then
    if [ -z "${ver}" ]; then
      log_info "Updating ${mod} to latest version in $(module_subdir)..."
      run go get -u "${mod}"
    else
      log_info "Updating ${mod} to version ${ver} in $(module_subdir)..."

Review comment (suggestion):
I think this can be simplified by returning the result from find, and later, instead of checking just with the value, compare with `= "true"`.

Code after:
#
# Updates version of given dependency in all the modules that depend on the mod.

set -euo pipefail

source ./scripts/test_lib.sh

if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
    log_error "Illegal number of parameters. Usage: $0 module [version]"
    exit 1
fi

mod="$1"
ver="${2:-}"

function print_current_dep_version {
  log_info "${mod} version in all go.mod files:"
  find . -name go.mod -exec grep -H "^\s*${mod}\s" {} + | sed 's|:|\t|' || true
  printf "\n"
}

function is_fully_indirect {
  local result
  result=$(find . -name go.mod -print0 | xargs -0 -I{} /bin/sh -c "cd \$(dirname {}); go list -f \"{{if eq .Path \\\"${mod}\\\"}}{{.Indirect}}{{end}}\" -m all" | sort | uniq)
  [ "$result" = "true" ]
}

function update_module {
  local subdir
  subdir=$(module_subdir)

  # The `go get` command is most effective on dependencies that are explicitly
  # listed as direct requirements in the go.mod file. When updating a purely
  # indirect dependency, `go get` might not update it as expected.
  #
  # To work around this, we temporarily promote the indirect dependency to a
  # direct one in the go.mod file using `go mod edit`. This ensures that
  # `go get` will see and correctly update the module. Subsequent cleanup
  # commands (like `go mod tidy`) will automatically move it back
  # to an indirect dependency, but at the designated updated version.
  #
  # Note: `go mod edit` requires a specific version (e.g., v1.2.3), so we only
  # use it when a version is explicitly provided. For "latest", we skip this
  # step and let `go get -u` handle it directly.
  if [ -n "${ver}" ]; then
    run go mod edit -require "${mod}@${ver}" || true
  fi

  # Check if the module is a dependency.
  if go list -m all | grep -q -E "^\s*${mod}\s"; then
    log_info "  Updating in ${subdir}..."