Skip to content

Commit

Permalink
fix(lint): shell lint
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <troian.ap@gmail.com>
  • Loading branch information
troian committed Sep 27, 2024
1 parent dce81bf commit 761af08
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 127 deletions.
15 changes: 15 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ if ! has readlink; then
exit 1
fi

if ! has pv; then
echo "pv is not installed"
exit 1
fi

if ! has lz4; then
echo "lz4 is not installed"
exit 1
fi

if ! has git-cliff; then
echo "git-cliff is not installed"
exit 1
fi

if [ -z "$GOPATH" ]; then
GOPATH=$(go env GOPATH)
export GOPATH
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand Down Expand Up @@ -47,7 +49,7 @@ jobs:
# using PAT as homebrew is located in different repo
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
test-network-upgrade-on-release:
runs-on: upgrade-tester
runs-on: self-hosted1
needs:
- publish
steps:
Expand Down
20 changes: 17 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- name: Install dependencies
run: |
brew install bash direnv
brew install bash direnv pv lz4 git-cliff
sudo chsh -s /usr/local/bin/bash
- name: Hook direnv to bash
run: echo 'eval "$(direnv hook bash)"' >> $HOME/.bashrc
Expand All @@ -44,6 +44,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -64,6 +66,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -82,6 +86,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -101,6 +107,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -119,6 +127,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: ./script/tools.sh gha-deps
- run: git fetch --prune --unshallow
- name: Detect required Go version
run: |
Expand All @@ -140,6 +150,8 @@ jobs:
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- uses: actions/checkout@v4
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -165,6 +177,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow
- name: Install dependencies
run: ./script/tools.sh gha-deps
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
Expand All @@ -189,7 +203,7 @@ jobs:
run: make release

network-upgrade-names:
runs-on: upgrade-tester
runs-on: self-hosted1
steps:
- name: Cleanup build folder
run: |
Expand Down Expand Up @@ -222,7 +236,7 @@ jobs:
./script/semver.sh validate "$upgrade"
done <<< $(find "$dir" ! -path "$dir" -maxdepth 1 -type d -exec basename {} \;)
network-upgrade:
runs-on: upgrade-tester
runs-on: self-hosted1
steps:
- name: Cleanup build folder
run: |
Expand Down
2 changes: 2 additions & 0 deletions make/test-upgrade.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ UPGRADE_BINARY_VERSION ?= local
REMOTE_TEST_WORKDIR ?= ~/go/src/github.com/akash-network/node
REMOTE_TEST_HOST ?=

COSMOVISOR := /Users/amr/go/src/github.com/cosmos/cosmos-sdk/tools/cosmovisor/cosmovisor

$(AKASH_INIT):
$(ROOT_DIR)/script/upgrades.sh --workdir=$(AP_RUN_DIR) --gbv=$(GENESIS_BINARY_VERSION) --ufrom=$(UPGRADE_FROM) --uto=$(UPGRADE_TO) --config="$(PWD)/config.json" init
touch $@
Expand Down
227 changes: 125 additions & 102 deletions script/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,117 +6,140 @@ SEMVER=$SCRIPT_DIR/semver.sh
gomod="$SCRIPT_DIR/../go.mod"

function get_gotoolchain() {
local gotoolchain
local goversion
local local_goversion

gotoolchain=$(grep -E '^toolchain go[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n')
goversion=$(grep -E '^go [0-9]{1,}.[0-9]{1,}(.[0-9]{1,})?$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n')

if [[ ${gotoolchain} == "" ]]; then
# determine go toolchain from go version in go.mod
if which go > /dev/null 2>&1 ; then
local_goversion=$(GOTOOLCHAIN=local go version | cut -d ' ' -f 3 | sed 's/go*//' | tr -d '\n')
if [[ $($SEMVER compare "v$local_goversion" v"$goversion") -ge 0 ]]; then
goversion=$local_goversion
else
local_goversion=
fi
fi

if [[ "$local_goversion" == "" ]]; then
goversion=$(curl -s "https://go.dev/dl/?mode=json&include=all" | jq -r --arg regexp "^go$goversion" '.[] | select(.stable == true) | select(.version | match($regexp)) | .version' | head -n 1 | sed -e s/^go//)
fi

if [[ $goversion != "" ]] && [[ $($SEMVER compare "v$goversion" v1.21.0) -ge 0 ]]; then
gotoolchain=go${goversion}
else
gotoolchain=go$(grep -E '^go [0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n').0
fi
fi

echo -n "$gotoolchain"
local gotoolchain
local goversion
local local_goversion

gotoolchain=$(grep -E '^toolchain go[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$' <"$gomod" | cut -d ' ' -f 2 | tr -d '\n')
goversion=$(grep -E '^go [0-9]{1,}.[0-9]{1,}(.[0-9]{1,})?$' <"$gomod" | cut -d ' ' -f 2 | tr -d '\n')

if [[ ${gotoolchain} == "" ]]; then
# determine go toolchain from go version in go.mod
if which go >/dev/null 2>&1; then
local_goversion=$(GOTOOLCHAIN=local go version | cut -d ' ' -f 3 | sed 's/go*//' | tr -d '\n')
if [[ $($SEMVER compare "v$local_goversion" v"$goversion") -ge 0 ]]; then
goversion=$local_goversion
else
local_goversion=
fi
fi

if [[ "$local_goversion" == "" ]]; then
goversion=$(curl -s "https://go.dev/dl/?mode=json&include=all" | jq -r --arg regexp "^go$goversion" '.[] | select(.stable == true) | select(.version | match($regexp)) | .version' | head -n 1 | sed -e s/^go//)
fi

if [[ $goversion != "" ]] && [[ $($SEMVER compare "v$goversion" v1.21.0) -ge 0 ]]; then
gotoolchain=go${goversion}
else
gotoolchain=go$(grep -E '^go [0-9]{1,}.[0-9]{1,}$' <"$gomod" | cut -d ' ' -f 2 | tr -d '\n').0
fi
fi

echo -n "$gotoolchain"
}

replace_paths() {
local file="${1}"
local cimport="${2}"
local nimport="${3}"
local sedcmd=sed
local file="${1}"
local cimport="${2}"
local nimport="${3}"
local sedcmd=sed

if [[ "$OSTYPE" == "darwin"* ]]; then
sedcmd=gsed
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
sedcmd=gsed
fi

$sedcmd -ri "s~$cimport~$nimport~" "${file}"
$sedcmd -ri "s~$cimport~$nimport~" "${file}"
}

function replace_import_path() {
local next_major_version=$1
local curr_module_name
local curr_version
local new_module_name

curr_module_name=$(cd go || exit; go list -m)
curr_version=$(echo "$curr_module_name" | sed -n 's/.*v\([0-9]*\).*/\1/p')
new_module_name=${curr_module_name%/"v$curr_version"}/$next_major_version

echo "current import paths are $curr_module_name, replacing with $new_module_name"

declare -a modules_to_upgrade_manually

modules_to_upgrade_manually+=("./go/go.mod")

echo "preparing files to replace"

declare -a files

while IFS= read -r line; do
files+=("$line")
done < <(find . -type f -not \( \
-path "./install.sh" \
-or -path "./upgrades/*" \
-or -path "./.cache/*" \
-or -path "./dist/*" \
-or -path "./.git*" \
-or -name "*.md" \
-or -path "./.idea/*" \))

echo "updating all files"

for file in "${files[@]}"; do
if test -f "$file"; then
# skip files that need manual upgrading
for excluded_file in "${modules_to_upgrade_manually[@]}"; do
if [[ "$file" == *"$excluded_file"* ]]; then
continue 2
fi
done

replace_paths "$file" "\"$curr_module_name" "\"$new_module_name"
fi
done

echo "updating go.mod"
for retract in $(cd go || exit; go mod edit --json | jq -cr '.Retract | if . != null then .[] else empty end'); do
local low
local high

low=$(jq -r '.Low' <<<"$retract")
high=$(jq -r '.High' <<<"$retract")
echo " dropping retract: [$low, $high]"
go mod edit -dropretract=["$low","$high"]
done

replace_paths "./go/go.mod" "$curr_module_name" "$new_module_name"
local next_major_version=$1
local curr_module_name
local curr_version
local new_module_name

curr_module_name=$(
cd go || exit
go list -m
)
curr_version=$(echo "$curr_module_name" | sed -n 's/.*v\([0-9]*\).*/\1/p')
new_module_name=${curr_module_name%/"v$curr_version"}/$next_major_version

echo "current import paths are $curr_module_name, replacing with $new_module_name"

declare -a modules_to_upgrade_manually

modules_to_upgrade_manually+=("./go/go.mod")

echo "preparing files to replace"

declare -a files

while IFS= read -r line; do
files+=("$line")
done < <(find . -type f -not \( \
-path "./install.sh" \
-or -path "./upgrades/*" \
-or -path "./.cache/*" \
-or -path "./dist/*" \
-or -path "./.git*" \
-or -name "*.md" \
-or -path "./.idea/*" \))

echo "updating all files"

for file in "${files[@]}"; do
if test -f "$file"; then
# skip files that need manual upgrading
for excluded_file in "${modules_to_upgrade_manually[@]}"; do
if [[ "$file" == *"$excluded_file"* ]]; then
continue 2
fi
done

replace_paths "$file" "\"$curr_module_name" "\"$new_module_name"
fi
done

echo "updating go.mod"
for retract in $(
cd go || exit
go mod edit --json | jq -cr '.Retract | if . != null then .[] else empty end'
); do
local low
local high

low=$(jq -r '.Low' <<<"$retract")
high=$(jq -r '.High' <<<"$retract")
echo " dropping retract: [$low, $high]"
go mod edit -dropretract=["$low","$high"]
done

replace_paths "./go/go.mod" "$curr_module_name" "$new_module_name"
}

function install_gha_dependencies() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt install pv lz4 npm jq curl unzip wget make
# version="1.0.0"
local version
version=2.6.0
wget -qO- "https://github.com/orhun/git-cliff/releases/download/v${version}/git-cliff-${version}-$(uname -m)-unknown-linux-gnu.tar.gz" | tar xvz -C /usr/local/bin --strip-components=1 git-cliff-${version}/git-cliff
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install bash direnv pv lz4 git-cliff
chsh -s /usr/local/bin/bash
fi
}

case "$1" in
gotoolchain)
get_gotoolchain
;;
replace-import-path)
shift
replace_import_path "$@"
;;
gotoolchain)
get_gotoolchain
;;
replace-import-path)
shift
replace_import_path "$@"
;;
gha-deps)
shift
install_gha_dependencies
;;
esac
Loading

0 comments on commit 761af08

Please sign in to comment.