Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Use separated exit codes for each type of errors #19

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ Options:
-d, --delete Delete an existing alias
-h, --help Display this message and exit
-V, --version Display version information and exit

Exit Codes:
0 OK
1 Invalid argument/option
2 User didn't gave the confirmation to proceed
3 An error occured when creating/restoring the backup of the .bashrc file
4 An error occured when adding/removing the alias
```

For more information, see the malias(1) man page
```

## Contributing

Expand Down
18 changes: 15 additions & 3 deletions doc/man/malias.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,26 @@ Print the current version.
.B \-h, \-\-help
Print the help.

.SH EXIT STATUS
.SH EXIT CODES
.TP
.B 0
if OK
OK

.TP
.B 1
if problems (user didn't gave confirmation to proceed with the adding/deletion, a problem happened during the backup/restore process of the .bashrc file, the added alias is incorrect, ...)
Invalid argument/option

.TP
.B 2
User didn't gave the confirmation to proceed

.TP
.B 3
An error occured when creating/restoring the backup of the .bashrc file

.TP
.B 4
An error occured when adding/removing the alias

.SH USAGE
.TP
Expand Down
52 changes: 37 additions & 15 deletions src/script/malias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ version() {

# Definition of the invalid_argument function: Print an error message, ask the user to check the help and exit
invalid_argument() {
echo -e >&2 "${name}: invalid argument -- '${operation}'\nTry '${name} --help' for more information."
echo -e >&2 "${name}: invalid argument -- '${argument}'\nTry '${name} --help' for more information."
exit 1
}

Expand All @@ -55,10 +55,10 @@ help (Print the help)
quit (Quit Malias)
EOF

read -rp $'\nWhat operation do you want to do? ' operation
operation=$(echo "${operation}" | awk '{print tolower($0)}')
read -rp $'\nWhat operation do you want to do? ' argument
argument=$(echo "${argument}" | awk '{print tolower($0)}')

case "${operation}" in
case "${argument}" in
a|add)
argument="--add"
;;
Expand Down Expand Up @@ -98,11 +98,17 @@ add() {
;;
*)
echo -e >&2 "\nAborting"
exit 1
exit 2
;;
esac

cp -p ~/.bashrc ~/.bashrc-bck_${name}-add-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created" || exit 1
if cp -p ~/.bashrc ~/.bashrc-bck_${name}-add-"${alias_name}"-"$(date +"%d-%m-%Y")"; then
echo -e "\nBackup of the .bashrc file created"
else
echo -e >&2 "ERROR: An error occured when creating the backup of the .bashrc file"
exit 3
fi

echo "alias ${new_alias}" >> ~/.bashrc

source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?)
Expand All @@ -112,9 +118,14 @@ add() {
rm -f ~/.bashrc-bck_${name}-add-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted"
exec bash
else
echo -e >&2 "\nERROR: An error occured when applying the alias\nPlease verify that you typed the alias correctly\nAlso, be aware that the alias name cannot contain space(s). However, it can contain \"-\" (hyphen) or \"_\" (underscore)"
mv -f ~/.bashrc-bck_${name}-add-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "\nERROR: An error occurred when restoring the backup of the ~/.bashrc file\nPlease, check for potential errors in it"
exit 1
echo -e >&2 "\nERROR: An error occured when adding the alias\nPlease verify that you typed the alias correctly\nAlso, be aware that the alias name cannot contain space(s). However, it can contain \"-\" (hyphen) or \"_\" (underscore)"
if mv -f ~/.bashrc-bck_${name}-add-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc; then
echo "Backup of the .bashrc file restored"
else
echo -e >&2 "\nERROR: An error occurred when restoring the backup of the ~/.bashrc file\nPlease, check for potential errors in it"
exit 3
fi
exit 4
fi
}

Expand Down Expand Up @@ -150,23 +161,34 @@ delete() {
;;
*)
echo -e >&2 "\nAborting"
exit 1
exit 2
;;
esac

cp -p ~/.bashrc ~/.bashrc-bck_${name}-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created" || exit 1
sed -i "/^alias ${alias_delete}$/d" ~/.bashrc || exit 1
if cp -p ~/.bashrc ~/.bashrc-bck_${name}-delete-"${alias_name}"-"$(date +"%d-%m-%Y")"; then
echo -e "\nBackup of the .bashrc file created"
else
echo -e >&2 "ERROR: An error occured when creating the backup of the .bashrc file"
exit 3
fi

sed -i "/^alias ${alias_delete}$/d" ~/.bashrc

source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?)

if [ "${source_error}" = 0 ]; then
echo -e "\nAlias ${alias_delete} successfully deleted" || exit 1
echo -e "\nAlias ${alias_delete} successfully deleted"
rm -f ~/.bashrc-bck_${name}-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted"
exec bash
else
echo -e >&2 "\nERROR: An error occured when deleting the alias"
mv -f ~/.bashrc-bck_${name}-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "ERROR: A error occurred when restoring the backup of the ~/.bashrc file\nPlease, check for potential errors in it"
exit 1
if mv -f ~/.bashrc-bck_${name}-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc; then
echo "Backup of the .bashrc file restored"
else
echo -e >&2 "\nERROR: An error occurred when restoring the backup of the ~/.bashrc file\nPlease, check for potential errors in it"
exit 3
fi
exit 4
fi
else
echo -e >&2 "\nError : Invalid input\nPlease, make sure you typed the correct number associated to the alias you want to delete"
Expand Down