-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
172 lines (159 loc) · 4.63 KB
/
main.sh
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
170
171
172
#!/bin/bash
# create a users.json file if it does not exist
if [ ! -f users.json ]; then
echo "[]" > users.json
fi
# create a notes directory if it does not exist
if [ ! -d notes ]; then
mkdir notes
fi
# function to show the main menu
main_menu() {
echo -e "\n📝 Welcome to the Note Taking App 📝"
echo -e "1. 📝 Create a Note"
echo -e "2. 📖 View Notes"
echo -e "3. 📝 Update a Note"
echo -e "4. 🗑️ Delete a Note"
echo -e "5. ❌ Exit"
read -p "Choose an option: " option
case $option in
1)
create_note
;;
2)
view_notes
;;
3)
update_note
;;
4)
delete_note
;;
5)
exit 0
;;
*)
echo -e "\e[31m❌ Invalid option\e[0m"
main_menu
;;
esac
}
# function to create a note
create_note() {
read -p "Enter the title of the note: " title
read -p "Enter the body of the note: " body
echo "{\"title\": \"$title\", \"body\": \"$body\"}" > notes/$title.json
echo -e "\e[32m📝 Note created successfully\e[0m"
main_menu
}
# function to view notes
view_notes() {
echo -e "1. 📖 View all notes"
echo -e "2. 📖 View a specific note"
read -p "Choose an option: " option
case $option in
1)
for note in notes/*.json; do
title=$(jq -r '.title' $note)
body=$(jq -r '.body' $note)
echo -e "\n\e[34mTitle: $title\e[0m"
echo -e "\e[34mBody: $body\e[0m"
done
;;
2)
read -p "Enter the title of the note: " title
if [ -f notes/$title.json ]; then
body=$(jq -r '.body' notes/$title.json)
echo -e "\n\e[34mTitle: $title\e[0m"
echo -e "\e[34mBody: $body\e[0m"
else
echo -e "\e[31m❌ Note does not exist\e[0m"
fi
;;
*)
echo -e "\e[31m❌ Invalid option\e[0m"
view_notes
;;
esac
main_menu
}
# function to update a note
update_note() {
read -p "Enter the title of the note: " title
if [ -f notes/$title.json ]; then
read -p "Enter the new body of the note: " body
echo "{\"title\": \"$title\", \"body\": \"$body\"}" > notes/$title.json
echo -e "\e[32m📝 Note updated successfully\e[0m"
else
echo -e "\e[31m❌ Note does not exist\e[0m"
fi
main_menu
}
# function to delete a note
delete_note() {
read -p "Enter the title of the note: " title
if [ -f notes/$title.json ]; then
rm notes/$title.json
echo -e "\e[32m📝 Note deleted successfully\e[0m"
else
echo -e "\e[31m❌ Note does not exist\e[0m"
fi
main_menu
}
# function to register a user
register() {
read -p "Enter your username: " username
read -s -p "Enter your password: " password
echo
hash=$(echo -n $password | sha256sum | awk '{print $1}')
# echo "Registering user: $username with hash: $hash" # Debug statement
if ! grep -q "\"username\": \"$username\"" users.json; then
jq ". += [{\"username\": \"$username\", \"password\": \"$hash\"}]" users.json > temp.json
mv temp.json users.json
echo -e "\n\e[32m🎉 User registered successfully\e[0m"
main_menu
else
echo -e "\n\e[31m❌ Username already exists\e[0m"
register
fi
}
# function to login a user
login() {
read -p "Enter your username: " username
read -s -p "Enter your password: " password
echo
hash=$(echo -n $password | sha256sum | awk '{print $1}')
# echo "Logging in user: $username with hash: $hash" # Debug statement
stored_hash=$(jq -r --arg username "$username" '.[] | select(.username == $username) | .password' users.json)
if [ "$hash" == "$stored_hash" ]; then
echo -e "\n\e[32m✅ Login successful\e[0m"
main_menu
else
echo -e "\n\e[31m❌ Invalid username or password\e[0m"
login
fi
}
# check if the user is registered or not
if [ $(jq '. | length' users.json) -eq 0 ]; then
echo -e "📝 Welcome to the Note Taking App 📝"
register
else
echo -e "📝 Welcome to the Note Taking App 📝"
echo -e "1. 📝 Login"
echo -e "2. 📝 Register"
read -p "Choose an option: " option
case $option in
1)
login
;;
2)
register
;;
*)
echo -e "\e[31m❌ Invalid option\e[0m"
;;
esac
fi
main_menu
# Tools and libraries used in the project
# jq: https://stedolan.github.io/jq/