Skip to content

Commit 6b6192c

Browse files
committed
🐚 Refactors branching, adjusts error messaging, and formatting...
... Formatting changes; indentation, new lines, line breaks and quotes **Notes** `if` branching that do **not** have `elif` block(s) are simplified, eg... ```bash if [[ "${variable}" == 'spam' ]]; then printf 'Consuming spam from variable -> %s\n' "${variable}" else printf 'Inserting spam into variable' variable='spam' fi ``` ... are changed to... ```bash [[ "${variable}" == 'spam' ]] && { printf 'Reading spam from variable -> %s\n' "${variable}" } || { printf 'Inserting spam into variable' variable='spam' } ``` ------ Checking if a variable is empty with `-z` option... ```bash if [[ -z "${variable}" ]]; then printf >&2 'Empty variable -> %s\n' "${variable}" fi ``` ... is not as trustworthy as checking if character count is above zero... ```bash ((${#variable})) || { printf >&2 'Empty variable -> %s\n' "${variable}" } ``` ... Nor is checking if a variable is assigned with `-n` option... ```bash if [[ -n "${variable}" ]]; then printf 'Detected variable -> %s\n' "${variable}" fi ``` ... as trustworthy as checking if character count is above zero... ```bash ((${#variable})) && { printf 'Detected variable -> %s\n' "${variable}" } ``` ------ Messages printed to standard error (STDERR) are now standardized to show where messages are directed to prior to the content, eg... ```bash printf 'Error message' >&2 ``` ... are changed to... ```bash printf >&2 'Error message' ```
1 parent d5440b5 commit 6b6192c

14 files changed

+238
-203
lines changed

bundle-install

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ usage(){
4343
# -h --help help
4444
# Displays this message and exits
4545
EOF
46-
if [ -n "${_message}" ] && [[ "${_message}" != '0' ]]; then
47-
printf 'Error - %s\n' "${_message}" >&2
48-
fi
46+
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
47+
printf >&2 'Error - %s\n' "${_message}"
48+
}
4949
}
5050

5151
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
@@ -64,36 +64,39 @@ elif ((_license)); then
6464
exit 0
6565
fi
6666

67-
if [ -z "${_repo_name}" ]; then
67+
(("${#_repo_name}")) || {
6868
usage 'missing repository name argument'
6969
exit "1"
70-
fi
70+
}
7171

7272

7373
_git_path="${HOME}/git/${_repo_name}"
74-
if ! [ -d "${_git_path}" ]; then
75-
printf '# No repository %s at %s\n' "${_repo_name}" "${_git_path}" >&2
74+
[[ -d "${_git_path}" ]] || {
75+
printf >&2 '# No repository %s at %s\n' "${_repo_name}" "${_git_path}"
7676
exit 1
77-
fi
77+
}
7878

79-
if ! [ -d "${_git_path}/.git" ]; then
80-
printf '# Repository %s maybe bare, this script cannot handle that right now\n' "${_repo_name}" >&2
79+
80+
[[ -d "${_git_path}/.git" ]] || {
81+
printf >&2 '# Repository %s maybe bare, this script cannot handle that right now\n' "${_repo_name}"
8182
exit 1
82-
fi
83+
}
8384

8485

8586
_bundle_path="${HOME}/.bundle/install"
8687
_pwd="${PWD}"
8788
cd "${_git_path}"
8889
source "${HOME}/.bash_aliases"
8990

90-
if ! [ -f "${_git_path}/Gemfile" ]; then
91+
[[ -f "${_git_path}/Gemfile" ]] || {
9192
bundle init
92-
fi
93+
}
94+
9395

94-
if ! [ -d "${_git_path}/.bundle" ]; then
96+
[[ -d "${_git_path}/.bundle" ]] || {
9597
bundle install --path "${_bundle_path}"
96-
fi
98+
}
99+
97100

98101
bundle install
99102
cd "${_pwd}"

bundle-update

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ usage(){
4646
# -h --help help
4747
# Displays this message and exits
4848
EOF
49-
if [ -n "${_message}" ] && [[ "${_message}" != '0' ]]; then
50-
printf 'Error - %s\n' "${_message}" >&2
51-
fi
49+
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
50+
printf >&2 'Error - %s\n' "${_message}"
51+
}
5252
}
5353

5454
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
@@ -67,40 +67,44 @@ elif ((_license)); then
6767
exit 0
6868
fi
6969

70-
if [ -z "${_repo_name}" ]; then
71-
usage 'missing repository name argument' >&2
70+
(("${#_repo_name}")) || {
71+
usage 'missing repository name argument'
7272
exit "1"
73-
fi
73+
}
7474

7575

7676
_git_path="${HOME}/git/${_repo_name}"
77-
if ! [ -d "${_git_path}" ]; then
78-
printf '# No repository %s at %s\n' "${_repo_name}" "${_git_path}" >&2
77+
[[ -d "${_git_path}" ]] || {
78+
printf >&2 '# No repository %s at %s\n' "${_repo_name}" "${_git_path}"
7979
exit 1
80-
fi
80+
}
8181

82-
if ! [ -d "${_git_path}/.git" ]; then
83-
printf '# Repository %s maybe bare, this script cannot handle that right now\n' "${_repo_name}" >&2
82+
83+
[[ -d "${_git_path}/.git" ]] || {
84+
printf >&2 '# Repository %s maybe bare, this script cannot handle that right now\n' "${_repo_name}"
8485
exit 1
85-
fi
86+
}
8687

8788

8889
_bundle_path="${HOME}/.bundle/install"
8990
_pwd="${PWD}"
9091
cd "${_git_path}"
9192
source "${HOME}/.bash_aliases"
9293

93-
if ! [ -f "${_git_path}/Gemfile" ]; then
94-
printf 'No Gemfile found under %s\n' "${_git_path}" >&2
94+
95+
[[ -f "${_git_path}/Gemfile" ]] || {
96+
printf >&2 'No Gemfile found under %s\n' "${_git_path}"
9597
exit 1
96-
fi
98+
}
99+
97100

98-
if ! [ -d "${_git_path}/.bundle" ]; then
101+
[[ -d "${_git_path}/.bundle" ]] || {
99102
bundle install --path "${_bundle_path}"
100-
fi
103+
}
104+
101105

102106
bundle update
103-
git_add_commit "Updated bundler configurations"
107+
git_add_commit "Updates bundler configurations"
104108
cd "${_pwd}"
105109

106110

copy-theme

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ usage(){
5151
$(for _name in ${_available_theme_names[@]}; do printf '# %s\n' "${_name}"; done)
5252
#
5353
EOF
54-
if [ -n "${_message}" ] && [[ "${_message}" != '0' ]]; then
55-
printf 'Error - %s\n' "${_message}" >&2
56-
fi
54+
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
55+
printf >&2 'Error - %s\n' "${_message}"
56+
}
5757
}
5858

5959
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
@@ -72,10 +72,11 @@ elif ((_license)); then
7272
exit 0
7373
fi
7474

75-
if [ -z "${_repo_name}" ]; then
75+
76+
(("${#_repo_name}")) || {
7677
usage 'missing repository name argument!'
7778
exit "1"
78-
fi
79+
}
7980

8081

8182
source "${HOME}/.bash_aliases" || exit 1
@@ -84,7 +85,7 @@ _gem_user_install_dir="${HOME}/.bundle/install/ruby/${_gem_user_version}/gems"
8485

8586
_available_theme_paths=()
8687
_available_theme_names=()
87-
for _path in ${_gem_user_install_dir}/jekyll-theme-*; do
88+
for _path in "${_gem_user_install_dir}/jekyll-theme-"*; do
8889
## Strip file path and 'jekyll-theme-' header
8990
_name="${_path##*/jekyll-theme-}"
9091
## Strip version from end
@@ -96,22 +97,26 @@ done
9697
_git_path="${HOME}/git/${_repo_name}"
9798
_jekyll_config_yml_path="${_git_path}/_config.yml"
9899
_old_PWD="${PWD}"
99-
if [ -d "${_git_path}" ]; then cd "${_git_path}"; fi
100+
[[ -d "${_git_path}" ]] && { cd "${_git_path}"; }
100101

101102
## To-do, get following results without tripping trap for failure
102103
_git_dir="$(git rev-parse --git-dir 2>/dev/null)"
103-
if [[ "${_git_path}/${_git_dir}" != "${_git_path}/.git" ]] || ! [ -f "${_jekyll_config_yml_path}" ]; then
104-
printf '# Repository %s not initialized correctly\n' "${_repo_name}"
105-
if [ -e "${HOME}/git-shell-commands/jekyll-init" ]; then
106-
printf '# Try: ssh ${USER}@host-or-ip jekyll-init %s\n' "${_repo_name}"
107-
fi
104+
[[ "${_git_path}/${_git_dir}" != "${_git_path}/.git" ]] || ! [[ -f "${_jekyll_config_yml_path}" ]] && {
105+
printf >&2 '# Repository %s not initialized correctly\n' "${_repo_name}"
106+
[[ -e "${HOME}/git-shell-commands/jekyll-init" ]] && {
107+
printf >&2 '# Try: ssh ${USER}@host-or-ip jekyll-init %s\n' "${_repo_name}"
108+
}
108109
exit 1
109-
fi
110-
[[ -n "${_theme_name}" ]] || _theme_name="$(awk '/theme:/{print $2}' "${_jekyll_config_yml_path}")"
111-
if [ -z "${_theme_name}" ]; then
110+
}
111+
112+
[[ -n "${_theme_name}" ]] || {
113+
_theme_name="$(awk '/theme:/{print $2}' "${_jekyll_config_yml_path}")"
114+
}
115+
(("${#_theme_name}")) || {
112116
usage 'cannot find theme name!'
113117
exit "1"
114-
fi
118+
}
119+
115120

116121
for _theme_path in "${_available_theme_paths[@]}"; do
117122
case "${_theme_path}" in
@@ -123,17 +128,24 @@ for _theme_path in "${_available_theme_paths[@]}"; do
123128
;;
124129
esac
125130
done
126-
if [ -z "${_chosen_theme_name}" ] || ! [ -d "${_chosen_theme_path}" ]; then usage; exit "$?"; fi
131+
! (("${#_chosen_theme_name}")) || ! [[ -d "${_chosen_theme_path}" ]] && {
132+
usage
133+
exit "$?";
134+
}
135+
127136

128137
for _path in $(ls -1 "${_chosen_theme_path}"); do
129-
if ! [ -d "${_chosen_theme_path}/${_path}" ]; then continue; fi
138+
[[ -d "${_chosen_theme_path}/${_path}" ]] || {
139+
continue
140+
}
130141
rsync -av --progress "${_chosen_theme_path}/${_path}" "${_git_path}/" --exclude "assets/fonts"
131142
# cp -vr "${_chosen_theme_path}/${_path}" "${_git_path}/"
132143
done
133-
if [ -f "${_chosen_theme_path}/LICENSE" ]; then
144+
[[ -f "${_chosen_theme_path}/LICENSE" ]] && {
134145
cp -v "${_chosen_theme_path}/LICENSE" "${_git_path}/LICENSE_${_chosen_theme_name}"
135-
fi
146+
}
147+
136148

137-
git_add_commit "Added files from ${_chosen_theme_name}"
149+
git_add_commit "Addes files from ${_chosen_theme_name}"
138150

139151
printf '# %s finished\n' "${__NAME__}"

edit-configs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ usage(){
4545
# -h --help help
4646
# Displays this message and exits
4747
EOF
48-
if [ -n "${_message}" ] && [[ "${_message}" != '0' ]]; then
49-
printf 'Error - %s\n' "${_message}" >&2
50-
fi
48+
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
49+
printf >&2 'Error - %s\n' "${_message}"
50+
}
5151
}
5252

5353
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
@@ -63,15 +63,13 @@ elif ((_license)); then
6363
exit 0
6464
fi
6565

66-
67-
if [ -n "${_github_api_token}" ]; then
66+
(("${#_github_api_token}")) && {
6867
mkdir -vp "${_config_dir}"
69-
tee "${_config_dir}/github-api-token" 1>/dev/null <<EOF
70-
${_github_api_token}
71-
EOF
72-
else
68+
tee "${_config_dir}/github-api-token" 1>/dev/null <<<"${_github_api_token}"
69+
} || {
7370
usage 'missing "--github-api-token" argument!'
7471
exit "1"
75-
fi
72+
}
73+
7674

7775
printf '%s finished!\n' "${__NAME__}"

git-checkout

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ usage(){
4747
# -h --help help
4848
# Display this message and exit
4949
EOF
50-
if [ -n "${_message}" ] && [[ "${_message}" != '0' ]]; then
51-
printf 'Error - %s\n' "${_message}" >&2
52-
fi
50+
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
51+
printf >&2 'Error - %s\n' "${_message}"
52+
}
5353
}
5454
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
5555
_valid_args=('--help|-h|help:bool'
@@ -61,10 +61,11 @@ argument_parser '_args' '_valid_args'
6161
_exit_status="$?"
6262

6363
## Catch --branch <name> <repo>
64-
if [ -n "${_branch}" ]; then
64+
(("${#_branch}")) && {
6565
_repo_name="${_branch_name}"
6666
_branch_name=''
67-
fi
67+
}
68+
6869

6970
if ((_help)) || ((_exit_status)); then
7071
usage "${_exit_status}"
@@ -74,32 +75,33 @@ elif ((_license)); then
7475
exit 0
7576
fi
7677

77-
if [ -z "${_repo_name}" ]; then
78+
(("${#_repo_name}")) || {
7879
usage 'missing repository name argument!'
7980
exit "1"
80-
fi
81+
}
8182

82-
if [ -z "${_branch}" ] && [ -z "${_branch_name}" ]; then
83+
! (("${#_branch}")) && ! (("${#_branch_name}")) && {
8384
usage 'missing "--branch" or "--branch-name" argument!'
8485
exit "1"
85-
fi
86+
}
8687

8788

8889
_git_path="${HOME}/git/${_repo_name:?# No repository name provided}"
89-
if ! [ -d "${_git_path}" ]; then
90+
[[ -d "${_git_path}" ]] || {
9091
usage "no repository found with ${_repo_name}"
9192
exit "1"
92-
elif ! [ -d "${_git_path}/.git" ]; then
93+
}
94+
[[ -d "${_git_path}/.git" ]] || {
9395
usage "directory found with ${_repo_name} but not initialized correctly"
9496
exit "1"
95-
fi
97+
}
9698

9799

98100
_git_args=()
99-
if [ -n "${_branch}" ]; then
101+
if (("${#_branch}")); then
100102
_git_args+=('-b' "${_branch}")
101-
elif [ -n "${_branch_name}" ]; then
102-
_git_args+=("${_branch}")
103+
elif (("${#_branch_name}")); then
104+
_git_args+=("${_branch_name}")
103105
else
104106
usage 'missing "--branch" or "--branch-name" argument!'
105107
exit 1
@@ -109,11 +111,13 @@ _old_PWD="${PWD}"
109111
cd "${_git_path}" || exit 1
110112
git checkout ${_git_args[@]}
111113
_git_exit_status="${?}"
112-
[[ "${_old_PWD}" == "${_git_path}" ]] || cd "${_old_PWD}"
114+
[[ "${_old_PWD}" == "${_git_path}" ]] || {
115+
cd "${_old_PWD}"
116+
}
113117

114-
if ((_git_exit_status)); then
115-
printf '%s encountered errors!\n' "${__NAME__}"
118+
((_git_exit_status)) && {
119+
printf >&2 '%s encountered errors!\n' "${__NAME__}"
116120
exit 1
117-
fi
121+
}
118122

119123
printf '%s finished!\n' "${__NAME__}"

0 commit comments

Comments
 (0)