Skip to content

Commit b5c3f27

Browse files
committed
bootstrap script v1
1 parent bf284e3 commit b5c3f27

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

bootstrap.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Define variables
6+
BASE_URL="https://github.com/cgsdev0/bash-stack/releases/latest/download"
7+
ZIP_NAME="template.zip"
8+
9+
# Function to display usage instructions
10+
show_usage() {
11+
echo "Usage: $0 <project-name>"
12+
}
13+
14+
# Function to download and extract the framework zip file
15+
download_framework() {
16+
echo "Downloading the framework..."
17+
if ! curl -sSL "$BASE_URL/$ZIP_NAME" -o "$TMP_DIR/$ZIP_NAME"; then
18+
echo "Error: Failed to download the framework."
19+
rm -rf "$TMP_DIR"
20+
exit 1
21+
fi
22+
echo "Extracting the framework..."
23+
if ! unzip -q "$TMP_DIR/$ZIP_NAME" -d "$TMP_DIR"; then
24+
echo "Error: Failed to extract the framework."
25+
rm -rf "$TMP_DIR"
26+
exit 1
27+
fi
28+
rm "$TMP_DIR/$ZIP_NAME"
29+
}
30+
31+
# Function to check if the project name is valid
32+
is_valid_project_name() {
33+
local name="$1"
34+
if [[ ! "$name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
35+
return 1
36+
fi
37+
return 0
38+
}
39+
40+
# Function to set up a new project
41+
setup_project() {
42+
echo "Creating a new project '$PROJECT_NAME'..."
43+
mkdir "$PROJECT_NAME"
44+
cp -r "$TMP_DIR/template"/* "$PROJECT_NAME"
45+
cd "$PROJECT_NAME"
46+
echo "Project '$PROJECT_NAME' is ready!"
47+
}
48+
49+
# Main script
50+
main() {
51+
TMP_DIR=$(mktemp -d)
52+
53+
# Check if a project name is provided as an argument
54+
if [ -z "$1" ]; then
55+
# Prompt the user for the project name interactively
56+
read -p "Enter a project name: " PROJECT_NAME
57+
if [ -z "$PROJECT_NAME" ]; then
58+
show_usage
59+
rm -rf "$TMP_DIR"
60+
exit 1
61+
fi
62+
else
63+
PROJECT_NAME="$1"
64+
fi
65+
66+
# Check if the project name is valid
67+
if ! is_valid_project_name "$PROJECT_NAME"; then
68+
echo "Error: Invalid project name. Project name can only contain letters, numbers, dashes, and underscores."
69+
rm -rf "$TMP_DIR"
70+
exit 1
71+
fi
72+
73+
# Check if the project directory already exists
74+
if [ -d "$PROJECT_NAME" ]; then
75+
echo "Error: Project directory '$PROJECT_NAME' already exists."
76+
rm -rf "$TMP_DIR"
77+
exit 1
78+
fi
79+
80+
# Download and extract the framework
81+
download_framework
82+
83+
# Set up the project
84+
setup_project
85+
86+
# Clean up temporary directory
87+
rm -rf "$TMP_DIR"
88+
}
89+
90+
# Run the main script
91+
main "$@"

0 commit comments

Comments
 (0)