Skip to content

Commit 0e9a0e0

Browse files
authored
improve compatibility & trap unexpected errors
* use trap to catch unusual errors * delete main function that does not bring any value
1 parent d0dae13 commit 0e9a0e0

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

gitconfig.sh

+53-57
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@
44
# The script sets up Git configuration globally
55
# and backs up the existing Git configuration files
66

7-
trap 'echo -e "\033[31mError: script failed\033[0m" >&2; exit 1' ERR
7+
trap 'err=$?; echo >&2 -e "\e[1;31mThe script catched this unusual error\033[0m"; exit $err' ERR
88

99
function usage() {
1010
cat 1>&2 <<EOF
1111
The installer for $(basename $0)
1212
1313
USAGE:
14-
bash $(basename $0) [ -b BRANCH_NAME ] [ -e "EMAIL" ] [ -u "USERNAME" ]
14+
./$(basename $0) [ -b "BRANCH_NAME" ] [ -e "EMAIL" ] [ -u "USER_NAME" ]
1515
1616
OPTIONS:
1717
-h Usage
1818
-b The default branch name (default is "main")
1919
-e required: The email that will be associated with commits
20-
-u required: The name that will be associated with commits
20+
-u required: The name that will be associated with commits (e.g, "Martin Scorcese")
2121
EOF
2222
}
2323

2424
function err() {
2525
echo -e "\e[1;31m$1\033[0m" >&2
26-
echo
2726
}
2827

2928
maybe_install_git() {
3029
if ! [ -x "$(command -v git)" ]; then
3130
err "I need Git. Please install it on your system. For example 'sudo apt install -y git' on Debian"
31+
exit 1
3232
fi
3333
}
3434

@@ -48,63 +48,59 @@ function configure_git() {
4848
git config --global alias.grep '!git ls-files | grep -i'
4949
}
5050

51-
main() {
52-
53-
local BACKUP_FOLDER="$HOME/gitconfig-backups"
54-
55-
while getopts "hb:e:u:" opt; do
56-
case "$opt" in
57-
b)
58-
local GIT_DEFAULT_BRANCH=${OPTARG}
59-
;;
60-
e)
61-
local GIT_USER_EMAIL=${OPTARG}
62-
;;
63-
u)
64-
local GIT_USER_NAME=${OPTARG}
65-
;;
66-
h)
67-
usage;
68-
exit 0
69-
;;
70-
: )
71-
err "No parameter provided"
72-
usage;
73-
exit 1
74-
;;
75-
esac
76-
done
77-
78-
shift $((OPTIND-1)) # clear options
79-
80-
mkdir -p "$BACKUP_FOLDER"
81-
cd "$PWD/files"
82-
for entry in $(ls .??*)
83-
do
84-
if [ -f "$HOME/$entry" ]; then
85-
cp -n "$HOME/$entry" "$BACKUP_FOLDER"
86-
fi
51+
BACKUP_FOLDER="$HOME/gitconfig-backups"
8752

88-
cp -n $entry $HOME
89-
done
90-
91-
if [ -z "${GIT_USER_EMAIL-}" ] || [ -z "${GIT_USER_NAME-}" ]; then
92-
usage
93-
exit 1
53+
while getopts "hb:e:u:" opt; do
54+
case "$opt" in
55+
b)
56+
GIT_DEFAULT_BRANCH=${OPTARG}
57+
;;
58+
e)
59+
GIT_USER_EMAIL=${OPTARG}
60+
;;
61+
u)
62+
GIT_USER_NAME=${OPTARG}
63+
;;
64+
h)
65+
usage;
66+
exit 0
67+
;;
68+
: )
69+
err "No parameter provided"
70+
usage;
71+
exit 1
72+
;;
73+
esac
74+
done
75+
76+
shift $((OPTIND-1)) # clear options
77+
78+
mkdir -p "$BACKUP_FOLDER"
79+
cd "$PWD/files"
80+
for entry in $(find . -type f -name '\.*' -print)
81+
do
82+
if [ -f "$HOME/$entry" ]; then
83+
cp -n "$HOME/$entry" "$BACKUP_FOLDER"
9484
fi
9585

96-
maybe_install_git
86+
cp -n $entry $HOME
87+
done
9788

98-
if [ -f "$HOME/.gitconfig" ]; then
99-
cp -n "$HOME/.gitconfig" "$BACKUP_FOLDER"
100-
fi
89+
if [ -z "${GIT_USER_EMAIL-}" ] || [ -z "${GIT_USER_NAME-}" ]; then
90+
err "The script failed to set variables for user email and name"
91+
usage
92+
exit 1
93+
fi
10194

102-
configure_git "${GIT_DEFAULT_BRANCH-main}" "$GIT_USER_EMAIL" "$GIT_USER_NAME"
95+
maybe_install_git
10396

104-
clear
105-
cd -
106-
echo -e "\033[32mdone:\033[0m"
107-
git config --global --list
108-
}
97+
if [ -f "$HOME/.gitconfig" ]; then
98+
cp -n "$HOME/.gitconfig" "$BACKUP_FOLDER"
99+
fi
100+
101+
configure_git "${GIT_DEFAULT_BRANCH-main}" "$GIT_USER_EMAIL" "$GIT_USER_NAME"
109102

110-
main
103+
clear
104+
cd -
105+
echo -e "\033[32mdone:\033[0m"
106+
git config --global --list

0 commit comments

Comments
 (0)