Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the bash script in test-init.sh #642

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions scripts/test-init.sh
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
Loading