-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vignesh Skanda <[email protected]>
- Loading branch information
1 parent
2e10082
commit ce3a466
Showing
1 changed file
with
26 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,46 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
set -e # Exit immediately if a command exits with a non-zero status. | ||
|
||
# URL of the CSV file to download | ||
url="https://kanaries-app.s3.ap-northeast-1.amazonaws.com/public-datasets/bike_sharing_dc.csv" | ||
|
||
# Directory to download the file to | ||
dir="./tests" | ||
|
||
# Filename to download | ||
filename="bike_sharing_dc.csv" | ||
|
||
# Create the directory if it doesn't exist | ||
mkdir -p "$dir" || { | ||
echo "Error: could not create directory '$dir'" >&2 | ||
mkdir -p "$dir" | ||
|
||
# Check available space in the target directory | ||
available_space=$(df -h --output=avail "$dir" | awk '{print $1}') | ||
required_space=$(curl -s -I "$url" | awk '/Content-Length/{print $2}' | numfmt --to=iec) | ||
if [ "$available_space" -lt "$required_space" ]; then | ||
echo "Error: not enough available space in '$dir' to download the file" >&2 | ||
exit 1 | ||
} | ||
fi | ||
|
||
# Download the file using curl | ||
# Download the file using curl or wget | ||
if command -v curl >/dev/null; then | ||
curl -f "$url" -o "$dir/bike_sharing_dc.csv" || { | ||
echo "Error: could not download file from '$url'" >&2 | ||
exit 1 | ||
} | ||
echo "Using curl to download the file..." | ||
curl -s -f "$url" -o "$dir/$filename" | ||
elif command -v wget >/dev/null; then | ||
wget -q --show-progress "$url" -O "$dir/bike_sharing_dc.csv" || { | ||
echo "Error: could not download file from '$url'" >&2 | ||
exit 1 | ||
} | ||
echo "Using wget to download the file..." | ||
wget -q --show-progress "$url" -O "$dir/$filename" | ||
else | ||
echo "Error: could not find curl or wget to download the file" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Check if the file was downloaded successfully | ||
if [ ! -f "$dir/bike_sharing_dc.csv" ]; then | ||
echo "Error: file '$dir/bike_sharing_dc.csv' was not downloaded successfully" >&2 | ||
if [ ! -f "$dir/$filename" ]; then | ||
echo "Error: file '$dir/$filename' was not downloaded successfully" >&2 | ||
exit 1 | ||
else | ||
echo "File downloaded successfully: '$dir/$filename'" | ||
fi | ||
|
||
# Remove the file on error | ||
trap 'rm -f "$dir/$filename"' ERR |