Skip to content

Commit 0150620

Browse files
committed
Add set -e modifier to hostnames script.
Replace tabs with spaces.
1 parent ef2bebe commit 0150620

File tree

1 file changed

+72
-71
lines changed

1 file changed

+72
-71
lines changed

bin/hostnames.sh

+72-71
Original file line numberDiff line numberDiff line change
@@ -19,101 +19,102 @@
1919
# https://opensource.org/licenses/MIT
2020
#
2121

22+
set -e
23+
2224
if [ "$1" = '-i' ]; then
23-
IP=$2
24-
shift 2
25+
IP=$2
26+
shift 2
2527
else
26-
IP=127.0.0.1
28+
IP=127.0.0.1
2729
fi
2830

2931
if [ $# = 0 ]; then
30-
# Use "$PWD/hostnames" as default configuration file:
31-
set -- "$PWD/hostnames"
32+
# Use "$PWD/hostnames" as default configuration file:
33+
set -- "$PWD/hostnames"
3234
fi
3335

3436
# Replaces everything but alphanumeric characters with dashes:
3537
sanitize() {
36-
echo "$1" | sed 's/[^a-zA-Z0-9-]/-/g'
38+
echo "$1" | sed 's/[^a-zA-Z0-9-]/-/g'
3739
}
3840

3941
# Returns a marker to identify the hostname settings in /etc/hosts:
4042
marker() {
41-
# Use the config file folder as project name:
42-
project="$(sanitize "$(basename "$(cd "$(dirname "$1")" && pwd)")")"
43-
config_name="$(sanitize "$(basename "$1")")"
44-
echo "## $project $config_name"
43+
# Use the config file folder as project name:
44+
project="$(sanitize "$(basename "$(cd "$(dirname "$1")" && pwd)")")"
45+
config_name="$(sanitize "$(basename "$1")")"
46+
echo "## $project $config_name"
4547
}
4648

4749
# Updates hosts from STDIN with the mappings in the given config file:
4850
map_hostnames() {
49-
marker_base="$(marker "$1")"
50-
marker_start="$marker_base start"
51-
marker_end="$marker_base end"
52-
# Remove the current hostnames section:
53-
sed "/$marker_start/,/$marker_end/d"
54-
# Don't add any entries unless IP is set:
55-
if [ -z "$IP" ]; then return; fi
56-
# Add the new hostname settings:
57-
echo "$marker_start"
58-
while read -r line; do
59-
# Skip empty lines and lines starting with a hash (#):
60-
[ -z "$line" ] || [ "${line#\#}" != "$line" ] && continue
61-
# Add each hostname entry with the $IP as mapping:
62-
printf '%s\t%s\n' "$IP" "$line"
63-
done < "$1"
64-
echo "$marker_end"
51+
marker_base="$(marker "$1")"
52+
marker_start="$marker_base start"
53+
marker_end="$marker_base end"
54+
# Remove the current hostnames section:
55+
sed "/$marker_start/,/$marker_end/d"
56+
# Don't add any entries unless IP is set:
57+
if [ -z "$IP" ]; then return; fi
58+
# Add the new hostname settings:
59+
echo "$marker_start"
60+
while read -r line; do
61+
# Skip empty lines and lines starting with a hash (#):
62+
if [ -z "$line" ] || [ "${line#\#}" != "$line" ]; then continue; fi
63+
# Add each hostname entry with the $IP as mapping:
64+
printf '%s\t%s\n' "$IP" "$line"
65+
done < "$1"
66+
echo "$marker_end"
6567
}
6668

6769
get_hosts_content() {
68-
# Retrieve the current host settings:
69-
hosts_content="$(cat /etc/hosts)"
70-
for file; do
71-
if [ ! -f "$file" ]; then
72-
echo "$file is not a valid file." >&2
73-
continue
74-
fi
75-
# Update the mappings for each configuration file:
76-
hosts_content="$(echo "$hosts_content" | map_hostnames "$file")"
77-
done
78-
echo "$hosts_content"
70+
# Retrieve the current host settings:
71+
hosts_content="$(cat /etc/hosts)"
72+
for file; do
73+
if [ ! -f "$file" ]; then
74+
echo "$file is not a valid file." >&2
75+
continue
76+
fi
77+
# Update the mappings for each configuration file:
78+
hosts_content="$(echo "$hosts_content" | map_hostnames "$file")"
79+
done
80+
echo "$hosts_content"
7981
}
8082

8183
# Updates /etc/hosts with the given content after confirmation from the user:
8284
update_hosts() {
83-
hosts_content="$1"
84-
# Diff /etc/hosts with the new content:
85-
hosts_diff="$(echo "$hosts_content" | diff /etc/hosts -)"
86-
if [ ! "$hosts_diff" ]; then
87-
echo 'No updates to /etc/hosts required.'
88-
return
89-
fi
90-
# Show a confirmation prompt to the user:
91-
echo
92-
echo "$hosts_diff"
93-
echo
94-
echo 'Update /etc/hosts with the given changes?'
95-
echo 'This will require Administrator privileges.'
96-
echo 'Please type "y" if you wish to proceed.'
97-
read -r confirmation
98-
if [ "$confirmation" = "y" ]; then
99-
# Check if we have root access:
100-
if [ "$(id -u)" -eq 0 ]; then
101-
echo "$hosts_content" > /etc/hosts
102-
else
103-
# Get root access and then write the new hosts file:
104-
echo "$hosts_content" | sudo tee /etc/hosts > /dev/null
105-
fi
106-
# Check if the last command failed:
107-
# shellcheck disable=SC2181
108-
if [ $? -eq 0 ]; then
109-
echo "Successfully updated /etc/hosts."
110-
return
111-
else
112-
echo "Update of /etc/hosts failed." >&2
113-
return 1
114-
fi
115-
fi
116-
echo "No updates to /etc/hosts written."
85+
hosts_content="$1"
86+
# Diff /etc/hosts with the new content:
87+
if hosts_diff="$(echo "$hosts_content" | diff /etc/hosts -)"; then
88+
echo 'No updates to /etc/hosts required.'
89+
return
90+
fi
91+
# Show a confirmation prompt to the user:
92+
echo
93+
echo "$hosts_diff"
94+
echo
95+
echo 'Update /etc/hosts with the given changes?'
96+
echo 'This will require Administrator privileges.'
97+
echo 'Please type "y" if you wish to proceed.'
98+
read -r confirmation
99+
if [ "$confirmation" = "y" ]; then
100+
# Check if we have root access:
101+
if [ "$(id -u)" -eq 0 ]; then
102+
echo "$hosts_content" > /etc/hosts
103+
else
104+
# Get root access and then write the new hosts file:
105+
echo "$hosts_content" | sudo tee /etc/hosts > /dev/null
106+
fi
107+
# Check if the last command failed:
108+
# shellcheck disable=SC2181
109+
if [ $? -eq 0 ]; then
110+
echo "Successfully updated /etc/hosts."
111+
return
112+
else
113+
echo "Update of /etc/hosts failed." >&2
114+
return 1
115+
fi
116+
fi
117+
echo "No updates to /etc/hosts written."
117118
}
118119

119120
update_hosts "$(get_hosts_content "$@")"

0 commit comments

Comments
 (0)