Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
169 lines (144 loc) · 4.64 KB

File metadata and controls

executable file
·
169 lines (144 loc) · 4.64 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
source scripts/languages.bash
# Set up .env file if it doesn't exist
function setup_env_file() {
echo ".env file not found. Let's create one!"
# Prompt user for NICKNAME
read -r -p "Enter your nickname (default: Unknown): " nickname
nickname=${nickname:-Unknown}
# Prompt user for LANGUAGE
echo "Select your preferred language:"
languages=("cpp" "java" "python" "python3" "c" "csharp" "javascript" "typescript" "php" "swift" "kotlin" "dart" "go" "ruby" "scala" "rust" "racket" "erlang" "elixir")
for i in "${!languages[@]}"; do
echo "$((i + 1))) ${languages[$i]}"
done
while true; do
read -r -p "Enter the number (default: 4): " language_index
language_index=${language_index:-4}
if [[ $language_index -ge 1 && $language_index -le ${#languages[@]} ]]; then
language=${languages[$((language_index - 1))]}
break
else
echo "Invalid input. Please enter a number between 1 and ${#languages[@]}."
fi
done
# Create .env file with user input or default values
echo "NICKNAME=$nickname" >.env
echo "LANGUAGE=$language" >>.env
echo ".env file created with the following values:"
echo "NICKNAME: $nickname"
echo "LANGUAGE: $language"
}
# Load environment variables from .env file
function load_env_vars() {
if [ ! -f .env ]; then
setup_env_file
fi
# shellcheck disable=SC2046
export $(sed <.env 's/#.*//g' | xargs | envsubst)
# Check if NICKNAME and LANGUAGE variables are set
if [ -z "$NICKNAME" ] || [ -z "$LANGUAGE" ]; then
echo "Error: Required environment variables NICKNAME and/or LANGUAGE are not set."
echo "Please set NICKNAME and LANGUAGE in the .env file with appropriate values."
exit 1
fi
# Check if the specified language is valid
if [[ ! "${!language_extensions[@]}" =~ $LANGUAGE ]]; then
echo "Error: Invalid language specified in the .env file."
echo "Please set LANGUAGE to one of the following valid languages:"
echo "${!language_extensions[@]}"
exit 1
fi
}
# Check if Bash version meets the minimum requirement
function check_bash_version() {
local required_version=$1
local bash_version
bash_version=$(bash --version | head -n1 | awk '{print $4}' | sed 's/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/')
if [[ $bash_version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
IFS='.' read -r -a required_parts <<<"$required_version"
local required_major=${required_parts[0]}
local required_minor=${required_parts[1]}
local required_patch=${required_parts[2]}
if ((major > required_major || (\
major == required_major && minor > required_minor) || (\
major == required_major && minor == required_minor && patch >= required_patch))); then
return 0
else
return 1
fi
else
return 1
fi
}
# Check if a command is installed
function check_command() {
local command="$1"
if ! command -v "$command" &>/dev/null; then
echo "The $command command is not installed."
read -r -p "Do you want to install $command? (Y/n): " install_command
case "$install_command" in
[nN] | [nN][oO])
echo "Installation of $command has been rejected. Exiting the script."
exit 1
;;
*)
echo "Proceeding with the installation of $command..."
brew install "$command"
;;
esac
fi
}
# Generates the solution code template with question details and author information
function make_solution_code() {
local question_id=$1
local question_name=$2
local question_url=$3
local code=$4
local comment=${language_comments[$LANGUAGE]}
local nickname
if [ -n "$NICKNAME" ]; then
nickname=$NICKNAME
else
nickname=Unknown
fi
local content
content=$(
cat <<EOF
${comment}
${comment}$question_id. $question_name
${comment}$question_url
${comment}Dale-Study
${comment}
${comment}Created by $nickname on $(date "+%Y/%m/%d").
${comment}
$code
EOF
)
echo "$content"
}
# Saves the solution code to a file in the appropriate directory based on the question slug and author's nickname
function save_file() {
local DIR
local title_slug="$1"
local content="$2"
local nickname
local language_extension
local solution_folder
local solution_file
if [ -n "$NICKNAME" ]; then
nickname=$NICKNAME
else
nickname=Unknown
fi
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
language_extension=${language_extensions[$LANGUAGE]}
solution_folder="$DIR/../$title_slug"
mkdir -p "$solution_folder"
solution_file="$solution_folder/$nickname.$language_extension"
echo "$content" >"$solution_file"
echo "File creation completed"
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.