11#! /usr/bin/env bash
22
3+
34set -e +o pipefail
45
6+ # Set up paths first
7+ bin_name=" codacy-cli-v2"
8+
9+ # Determine OS-specific paths
510os_name=$( uname)
611arch=$( uname -m)
712
@@ -14,16 +19,60 @@ case "$arch" in
1419 ;;
1520esac
1621
22+ if [ -z " $CODACY_CLI_V2_TMP_FOLDER " ]; then
23+ if [ " $( uname) " = " Linux" ]; then
24+ CODACY_CLI_V2_TMP_FOLDER=" $HOME /.cache/codacy/codacy-cli-v2"
25+ elif [ " $( uname) " = " Darwin" ]; then
26+ CODACY_CLI_V2_TMP_FOLDER=" $HOME /Library/Caches/Codacy/codacy-cli-v2"
27+ else
28+ CODACY_CLI_V2_TMP_FOLDER=" .codacy-cli-v2"
29+ fi
30+ fi
31+
32+ version_file=" $CODACY_CLI_V2_TMP_FOLDER /version.yaml"
33+
34+
35+ get_version_from_yaml () {
36+ if [ -f " $version_file " ]; then
37+ local version=$( grep -o ' version: *"[^"]*"' " $version_file " | cut -d' "' -f2)
38+ if [ -n " $version " ]; then
39+ echo " $version "
40+ return 0
41+ fi
42+ fi
43+ return 1
44+ }
45+
46+ get_latest_version () {
47+ local response
48+ if [ -n " $GH_TOKEN " ]; then
49+ response=$( curl -Lq --header " Authorization: Bearer $GH_TOKEN " " https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2> /dev/null)
50+ else
51+ response=$( curl -Lq " https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2> /dev/null)
52+ fi
53+
54+ handle_rate_limit " $response "
55+ local version=$( echo " $response " | grep -m 1 tag_name | cut -d' "' -f4)
56+ echo " $version "
57+ }
58+
59+ handle_rate_limit () {
60+ local response=" $1 "
61+ if echo " $response " | grep -q " API rate limit exceeded" ; then
62+ fatal " Error: GitHub API rate limit exceeded. Please try again later"
63+ fi
64+ }
65+
1766download_file () {
1867 local url=" $1 "
1968
20- echo " Download url : ${url} "
69+ echo " Downloading from URL : ${url} "
2170 if command -v curl > /dev/null 2>&1 ; then
2271 curl -# -LS " $url " -O
2372 elif command -v wget > /dev/null 2>&1 ; then
2473 wget " $url "
2574 else
26- fatal " Could not find curl or wget, please install one."
75+ fatal " Error: Could not find curl or wget, please install one."
2776 fi
2877}
2978
@@ -40,51 +89,50 @@ download_cli() {
4089
4190 local bin_folder=" $1 "
4291 local bin_path=" $2 "
92+ local version=" $3 "
4393
4494 if [ ! -f " $bin_path " ]; then
45- echo " Downloading the codacy cli v2 version ( $CODACY_CLI_V2_VERSION ) "
95+ echo " 📥 Downloading CLI version $version ... "
4696
47- remote_file=" codacy-cli-v2_${CODACY_CLI_V2_VERSION } _${suffix} _${arch} .tar.gz"
48- url=" https://github.com/codacy/codacy-cli-v2/releases/download/${CODACY_CLI_V2_VERSION } /${remote_file} "
97+ remote_file=" codacy-cli-v2_${version } _${suffix} _${arch} .tar.gz"
98+ url=" https://github.com/codacy/codacy-cli-v2/releases/download/${version } /${remote_file} "
4999
50100 download " $url " " $bin_folder "
51101 tar xzfv " ${bin_folder} /${remote_file} " -C " ${bin_folder} "
102+ else
103+ echo " ✓ Using cached CLI version $version "
52104 fi
53105}
54106
55- # Temporary folder for downloaded files
56- if [ -z " $CODACY_CLI_V2_TMP_FOLDER " ]; then
57- if [ " $os_name " = " Linux" ]; then
58- CODACY_CLI_V2_TMP_FOLDER=" $HOME /.cache/codacy/codacy-cli-v2"
59- elif [ " $os_name " = " Darwin" ]; then
60- CODACY_CLI_V2_TMP_FOLDER=" $HOME /Library/Caches/Codacy/codacy-cli-v2"
61- else
62- CODACY_CLI_V2_TMP_FOLDER=" .codacy-cli-v2"
63- fi
107+ # Warn if CODACY_CLI_V2_VERSION is set and update is requested
108+ if [ -n " $CODACY_CLI_V2_VERSION " ] && [ " $1 " = " update" ]; then
109+ echo " ⚠️ Warning: Performing update with forced version $CODACY_CLI_V2_VERSION "
110+ echo " This might prevent updating to the latest version"
64111fi
65112
66- # if no version is specified, we fetch the latest
67- if [ -z " $CODACY_CLI_V2_VERSION " ]; then
68- if [ -n " $GH_TOKEN " ]; then
69- CODACY_CLI_V2_VERSION=" $( curl -Lq --header " Authorization: Bearer $GH_TOKEN " " https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2> /dev/null | grep -m 1 tag_name | cut -d' "' -f4) "
70- else
71- CODACY_CLI_V2_VERSION=" $( curl -Lq " https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2> /dev/null | grep -m 1 tag_name | cut -d' "' -f4) "
72- fi
113+ # Ensure version.yaml exists and is up to date
114+ if [ ! -f " $version_file " ] || [ " $1 " = " update" ]; then
115+ echo " ℹ️ Fetching latest version..."
116+ version=$( get_latest_version)
117+ echo " version: \" $version \" " > " $version_file "
73118fi
74119
75- # Folder containing the binary
76- bin_folder=" ${CODACY_CLI_V2_TMP_FOLDER} /${CODACY_CLI_V2_VERSION} "
77- # Create the folder if not exists
78- mkdir -p " $bin_folder "
120+ # Set the version to use
121+ if [ -n " $CODACY_CLI_V2_VERSION " ]; then
122+ version=" $CODACY_CLI_V2_VERSION "
123+ else
124+ version=$( get_version_from_yaml)
125+ fi
79126
80- # name of the binary
81- bin_name=" codacy-cli-v2"
82127
83- # Set binary path
128+ # Set up version-specific paths
129+ bin_folder=" ${CODACY_CLI_V2_TMP_FOLDER} /${version} "
130+
131+ mkdir -p " $bin_folder "
84132bin_path=" $bin_folder " /" $bin_name "
85133
86- # download the tool
87- download_cli " $bin_folder " " $bin_path "
134+ # Download the tool if not already installed
135+ download_cli " $bin_folder " " $bin_path " " $version "
88136chmod +x " $bin_path "
89137
90138run_command=" $bin_path "
@@ -93,7 +141,7 @@ if [ -z "$run_command" ]; then
93141fi
94142
95143if [ " $# " -eq 1 ] && [ " $1 " = " download" ]; then
96- echo " $g " " Codacy cli v2 download succeeded" ;
144+ echo " Codacy cli v2 download succeeded"
97145else
98146 eval " $run_command $* "
99147fi
0 commit comments