Skip to content

Commit a250d55

Browse files
committed
feat: Implement common utility functions in common.bash
1 parent b3d9626 commit a250d55

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

scripts/common.bash

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/languages.bash
4+
5+
# Load environment variables from .env file
6+
function load_env_vars() {
7+
if [ -f .env ]; then
8+
# shellcheck disable=SC2046
9+
export echo $(sed <.env 's/#.*//g' | xargs | envsubst)
10+
11+
# Check if required variables are set and not equal to default values
12+
if [ "$NICKNAME" = "your_nickname" ] || [ "$LANGUAGE" = "choose_your_language" ]; then
13+
echo "Error: Required environment variables are set to default values."
14+
echo "Please update NICKNAME and LANGUAGE in the .env file with appropriate values."
15+
exit 1
16+
fi
17+
18+
# Check if the specified language is valid
19+
20+
if [[ ! "${!language_extensions[@]}" =~ $LANGUAGE ]]; then
21+
echo "Error: Invalid language specified in the .env file."
22+
echo "Please set LANGUAGE to one of the following valid languages:"
23+
echo "${!language_extensions[@]}"
24+
exit 1
25+
fi
26+
fi
27+
}
28+
29+
# Check if a command is installed
30+
function check_command() {
31+
local command="$1"
32+
if ! command -v "$command" &>/dev/null; then
33+
echo "The $command command is not installed."
34+
read -r -p "Do you want to install $command? (Y/n): " install_command
35+
case "$install_command" in
36+
[nN] | [nN][oO])
37+
echo "Installation of $command has been rejected. Exiting the script."
38+
exit 1
39+
;;
40+
*)
41+
echo "Proceeding with the installation of $command..."
42+
brew install "$command"
43+
;;
44+
esac
45+
fi
46+
}
47+
48+
# Generates the solution code template with question details and author information
49+
function make_solution_code() {
50+
local question_id=$1
51+
local question_name=$2
52+
local question_url=$3
53+
local code=$4
54+
local comment=${language_comments[$LANGUAGE]}
55+
local nickname
56+
if [ -n "$NICKNAME" ]; then
57+
nickname=$NICKNAME
58+
else
59+
nickname=Unknown
60+
fi
61+
local content
62+
content=$(
63+
cat <<EOF
64+
${comment}
65+
${comment}$question_id. $question_name
66+
${comment}$question_url
67+
${comment}Dale-Study
68+
${comment}
69+
${comment}Created by $nickname on $(date "+%Y/%m/%d").
70+
${comment}
71+
72+
$code
73+
74+
EOF
75+
)
76+
echo "$content"
77+
}
78+
79+
# Saves the solution code to a file in the appropriate directory based on the question slug and author's nickname
80+
function save_file() {
81+
local DIR
82+
local title_slug="$1"
83+
local content="$2"
84+
local nickname
85+
local language_extension
86+
local solution_folder
87+
local solution_file
88+
89+
if [ -n "$NICKNAME" ]; then
90+
nickname=$NICKNAME
91+
else
92+
nickname=Unknown
93+
fi
94+
95+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
96+
language_extension=${language_extensions[$LANGUAGE]}
97+
98+
solution_folder="$DIR/../$title_slug"
99+
mkdir -p "$solution_folder"
100+
101+
solution_file="$solution_folder/$nickname.$language_extension"
102+
echo "$content" >"$solution_file"
103+
echo "File creation completed"
104+
}

0 commit comments

Comments
 (0)