github.com/vpayno/adventofcode-2022-golang-workspace@v0.0.0-20230605190011-dbafed5593de/scripts/go-version-bump (about)

     1  #!/bin/bash
     2  
     3  declare in_gha=false
     4  
     5  if [[ ${1} == --github-action ]]; then
     6  	in_gha=true
     7  	shift
     8  fi
     9  
    10  die() {
    11  	printf "ERROR: %s\n" "$@"
    12  	exit 1
    13  }
    14  
    15  if ! git diff --quiet; then
    16  	die "please commit or stash your existing changes before running this script"
    17  fi
    18  
    19  golang_show_dl_urls() {
    20  	curl -sS https://go.dev/dl/ | grep 'class="download"' | sed -r -e 's/^.*href="(.*)">.*$/https:\/\/go.dev\1/' | sort -V
    21  }
    22  
    23  golang_show_dl_versions() {
    24  	golang_show_dl_urls | sed -r -e 's:^.*/go(.*)[.](zip|msi|tar.gz|pkg)$:\1:' | grep '[..]src$' | sed -r -e 's:[.]src::g' | grep -v -E '(beta|rc)' | grep -E '^[0-9]+[.][0-9]+$' | sort -V
    25  }
    26  
    27  golang_show_dl_patch_versions() {
    28  	golang_show_dl_urls | sed -r -e 's:^.*/go(.*)[.](zip|msi|tar.gz|pkg)$:\1:' | grep '[..]src$' | sed -r -e 's:[.]src::g' | grep -v -E '(beta|rc)' | grep -E '^[0-9]+[.][0-9]+[.][0-9]+$' | sort -V
    29  }
    30  
    31  declare -a FILES
    32  declare old_go_ver
    33  declare new_go_ver
    34  declare current_branch
    35  
    36  old_go_ver="$(awk '/^go / { print $NF }' go.mod)"
    37  new_go_ver="$(golang_show_dl_versions | tail -n 1)"
    38  
    39  if [[ ${old_go_ver} == "${new_go_ver}" ]]; then
    40  	printf "Golang version, %s, is already up to date.\n" "${old_go_ver}"
    41  	exit # 0 exit code since this isn't a workflow error condition
    42  fi
    43  
    44  if ! golang_show_dl_patch_versions | grep -q -E "^${new_go_ver}[.]1$"; then
    45  	printf "Golang version >%s.0 not found. Skipping update.\n" "${new_go_ver}"
    46  	exit # 0 exit code since this isn't a workflow error condition
    47  fi
    48  
    49  mapfile -t FILES < <(git grep --name-only "\b${old_go_ver//[.]/[.]}\b" | grep -v -E '[.]md$')
    50  
    51  sed -r -i -e "s/${old_go_ver}/${new_go_ver}/g" "${FILES[@]}" || die "failed to change the golang version"
    52  
    53  if ! git diff --quiet; then
    54  	git status
    55  	printf "\n"
    56  
    57  	git diff
    58  	printf "\n"
    59  
    60  	if ! ${in_gha}; then
    61  		git add --update . || die "failed to stage the script's changes"
    62  
    63  		git commit -S -m "build(go): bump golang version to ${new_go_ver}" || die "failed to create a golang version bump commit"
    64  
    65  		current_branch="$(git branch --show-current)"
    66  
    67  		git push origin "${current_branch}" || die "failed to push the commit to 'origin ${current_branch}'"
    68  
    69  		printf "\n"
    70  		git show
    71  	fi
    72  fi