|
19 | 19 | # https://opensource.org/licenses/MIT
|
20 | 20 | #
|
21 | 21 |
|
| 22 | +set -e |
| 23 | + |
22 | 24 | if [ "$1" = '-i' ]; then
|
23 |
| - IP=$2 |
24 |
| - shift 2 |
| 25 | + IP=$2 |
| 26 | + shift 2 |
25 | 27 | else
|
26 |
| - IP=127.0.0.1 |
| 28 | + IP=127.0.0.1 |
27 | 29 | fi
|
28 | 30 |
|
29 | 31 | 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" |
32 | 34 | fi
|
33 | 35 |
|
34 | 36 | # Replaces everything but alphanumeric characters with dashes:
|
35 | 37 | sanitize() {
|
36 |
| - echo "$1" | sed 's/[^a-zA-Z0-9-]/-/g' |
| 38 | + echo "$1" | sed 's/[^a-zA-Z0-9-]/-/g' |
37 | 39 | }
|
38 | 40 |
|
39 | 41 | # Returns a marker to identify the hostname settings in /etc/hosts:
|
40 | 42 | 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" |
45 | 47 | }
|
46 | 48 |
|
47 | 49 | # Updates hosts from STDIN with the mappings in the given config file:
|
48 | 50 | 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" |
65 | 67 | }
|
66 | 68 |
|
67 | 69 | 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" |
79 | 81 | }
|
80 | 82 |
|
81 | 83 | # Updates /etc/hosts with the given content after confirmation from the user:
|
82 | 84 | 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." |
117 | 118 | }
|
118 | 119 |
|
119 | 120 | update_hosts "$(get_hosts_content "$@")"
|
0 commit comments