Skip to content

Commit 0de2233

Browse files
Merge pull request #7 from codacy/cli-script
Add download script
2 parents 610d50e + 58a37ff commit 0de2233

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

codacy-cli.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env bash
2+
3+
set -e +o pipefail
4+
5+
os_name=$(uname)
6+
arch=$(uname -m)
7+
8+
case "$arch" in
9+
"x86_64")
10+
arch="amd64"
11+
;;
12+
"x86")
13+
arch="386"
14+
;;
15+
esac
16+
17+
download_file() {
18+
local url="$1"
19+
20+
echo "Download url: ${url}"
21+
if command -v curl > /dev/null 2>&1; then
22+
curl -# -LS "$url" -O
23+
elif command -v wget > /dev/null 2>&1; then
24+
wget "$url"
25+
else
26+
fatal "Could not find curl or wget, please install one."
27+
fi
28+
}
29+
30+
download() {
31+
local url="$1"
32+
local file_name="$2"
33+
local output_folder="$3"
34+
local output_filename="$4"
35+
local checksum_url="$5"
36+
local original_folder
37+
original_folder="$(pwd)"
38+
39+
cd "$output_folder"
40+
41+
download_file "$url"
42+
# checksum "$file_name" "$checksum_url"
43+
44+
cd "$original_folder"
45+
}
46+
47+
download_reporter() {
48+
# OS name lower case
49+
suffix=$(echo "$os_name" | tr '[:upper:]' '[:lower:]')
50+
51+
local binary_name="codacy-cli-v2-$suffix"
52+
local reporter_path="$1"
53+
local reporter_folder="$2"
54+
local reporter_filename="$3"
55+
56+
if [ ! -f "$reporter_path" ]
57+
then
58+
echo "$i" "Downloading the codacy cli v2 $binary_name... ($CODACY_CLI_V2_VERSION)"
59+
60+
remote_file="codacy-cli-v2_${CODACY_CLI_V2_VERSION}_${suffix}_${arch}.tar.gz"
61+
binary_url="https://github.com/codacy/codacy-cli-v2/releases/download/${CODACY_CLI_V2_VERSION}/${remote_file}"
62+
# echo $binary_url
63+
# checksum_url="https://github.com/codacy/codacy-coverage-reporter/releases/download/$CODACY_CLI_V2_VERSION/$binary_name.SHA512SUM"
64+
65+
download "$binary_url" "$binary_name" "$reporter_folder" "$reporter_filename" "$checksum_url"
66+
67+
echo "${reporter_folder}/${remote_file}"
68+
tar xzfv "${reporter_folder}/${remote_file}" -C "${reporter_folder}"
69+
else
70+
echo "$i" "Codacy reporter $binary_name already in cache"
71+
fi
72+
}
73+
74+
# Temporary folder for downloaded files
75+
if [ -z "$CODACY_CLI_V2_TMP_FOLDER" ]; then
76+
if [ "$os_name" = "Linux" ]; then
77+
CODACY_CLI_V2_TMP_FOLDER="$HOME/.cache/codacy/codacy-cli-v2"
78+
elif [ "$os_name" = "Darwin" ]; then
79+
CODACY_CLI_V2_TMP_FOLDER="$HOME/Library/Caches/Codacy/codacy-cli-v2"
80+
else
81+
CODACY_CLI_V2_TMP_FOLDER=".codacy-cli-v2"
82+
fi
83+
fi
84+
85+
reporter_filename="codacy-cli-v2"
86+
87+
# if no version is specified, we fetch the latest
88+
if [ -z "$CODACY_CLI_V2_VERSION" ]; then
89+
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)"
90+
echo "Fetching latest version: ${CODACY_CLI_V2_VERSION}"
91+
fi
92+
93+
# Folder containing the binary
94+
reporter_folder="$CODACY_CLI_V2_TMP_FOLDER"/"$CODACY_CLI_V2_VERSION"
95+
96+
# Create the reporter folder if not exists
97+
mkdir -p "$reporter_folder"
98+
99+
# Set binary path
100+
reporter_path="$reporter_folder"/"$reporter_filename"
101+
102+
download_reporter "$reporter_path" "$reporter_folder" "$reporter_filename"
103+
104+
chmod +x "$reporter_path"
105+
run_command="$reporter_path"
106+
107+
if [ -z "$run_command" ]
108+
then
109+
fatal "Codacy cli v2 binary could not be found."
110+
fi
111+
112+
if [ "$#" -eq 1 ] && [ "$1" = "download" ];
113+
then
114+
echo "$g" "Codacy reporter download succeeded";
115+
else
116+
eval "$run_command $*"
117+
fi

0 commit comments

Comments
 (0)