1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-06-05 07:50:50 +00:00

Merge branch 'master' into exact-matches

* master:
  Exit with "Not found" message on failed search in `enable`/`disable`
  Bump version to 1.5.0
  Print confirmation feedback in `add` on success.
  Declare and assign separately to avoid masking return values.
  Add `brew tap` command to `brew install` in Readme.
This commit is contained in:
William Melody 2015-09-12 13:28:31 -07:00
commit 9cb7576bb5
3 changed files with 34 additions and 10 deletions

View File

@ -14,7 +14,7 @@ A command line program with shortcuts for managing hosts file entries.
To install with [Homebrew](http://brew.sh/): To install with [Homebrew](http://brew.sh/):
brew install alphabetum/taps/hosts brew tap alphabetum/taps && brew install alphabetum/taps/hosts
### bpkg ### bpkg

40
hosts
View File

@ -36,7 +36,7 @@ IFS=$'\n\t'
# Globals # Globals
############################################################################### ###############################################################################
_VERSION="1.4.3" _VERSION="1.5.0"
# DEFAULT_COMMAND # DEFAULT_COMMAND
# #
@ -690,15 +690,23 @@ add() {
else else
if [[ -n ${comment} ]] if [[ -n ${comment} ]]
then then
local formatted_comment=$(_join " " "${comment[@]}") local formatted_comment
formatted_comment=$(_join " " "${comment[@]}")
printf "%s\t%s\t# %s\n" \ printf "%s\t%s\t# %s\n" \
"${ip}" \ "${ip}" \
"${hostname}" \ "${hostname}" \
"${formatted_comment}" >> "${HOSTS_PATH}" "${formatted_comment}" >> "${HOSTS_PATH}"
printf "Added:\n%s\t%s\t# %s\n" \
"${ip}" \
"${hostname}" \
"${formatted_comment}"
else else
printf "%s\t%s\n" \ printf "%s\t%s\n" \
"${ip}" \ "${ip}" \
"${hostname}" >> "${HOSTS_PATH}" "${hostname}" >> "${HOSTS_PATH}"
printf "Added:\n%s\t%s\n" \
"${ip}" \
"${hostname}"
fi fi
fi fi
} }
@ -723,10 +731,15 @@ disable() {
else else
_debug printf "disable() \$search_term: %s\n" "$search_term" _debug printf "disable() \$search_term: %s\n" "$search_term"
local targets=$( local targets
targets=$(
sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}" sed -n "s/^\([^#]*${search_term}.*\)$/\1/p" "${HOSTS_PATH}"
) )
_debug printf "disable() \$targets: %s\n" "$targets" _debug printf "disable() \$targets: %s\n" "$targets"
if [[ -z "${targets}" ]]
then
_die printf "Not found: %s\n" "${search_term}"
fi
printf "Disabling:\n%s\n" "${targets}" printf "Disabling:\n%s\n" "${targets}"
@ -788,7 +801,14 @@ enable() {
else else
local target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/" local target_regex="s/^\#disabled: \(.*${search_term}.*\)$/\1/"
local targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}") local targets
targets=$(sed -n "${target_regex}p" "${HOSTS_PATH}")
_debug printf "enable() \$targets: %s\n" "$targets"
if [[ -z "${targets}" ]]
then
_die printf "Not found: %s\n" "${search_term}"
fi
printf "Enabling:\n%s\n" "${targets}" printf "Enabling:\n%s\n" "${targets}"
# -i '' - in place edit. BSD sed requires extension argument, for GNU # -i '' - in place edit. BSD sed requires extension argument, for GNU
@ -836,7 +856,8 @@ Description:
EOM EOM
list() { list() {
# Get the disabled records up front for the two cases where they are needed. # Get the disabled records up front for the two cases where they are needed.
local disabled_records=$( local disabled_records
disabled_records=$(
sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}" sed -n "s/^\#disabled: \(.*\)$/\1/p" "${HOSTS_PATH}"
) )
@ -887,7 +908,8 @@ remove() {
# #
# - Note double periods in regular expression in order to emulate /.+/, # - Note double periods in regular expression in order to emulate /.+/,
# which apparently doesn't work properly with all versions of sed. # which apparently doesn't work properly with all versions of sed.
local target_records=$( local target_records
target_records=$(
sed -n \ sed -n \
-e "s/^\(${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \ -e "s/^\(${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \
-e "s/^\(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \ -e "s/^\(..*[${_TAB_SPACE_}]${search_string}[${_TAB_SPACE_}]..*\)$/\1/p" \
@ -949,10 +971,12 @@ show() {
if [[ -n "$1" ]] if [[ -n "$1" ]]
then then
# Run `sed` before `grep` to avoid conflict that supress `sed` output. # Run `sed` before `grep` to avoid conflict that supress `sed` output.
local disabled_records=$( local disabled_records
disabled_records=$(
sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}" sed -n "s/^\#\(disabled: .*$1.*\)$/\1/p" "${HOSTS_PATH}"
) )
local enabled_records=$( local enabled_records
enabled_records=$(
grep "^[^#]*$1" "${HOSTS_PATH}" grep "^[^#]*$1" "${HOSTS_PATH}"
) )
# Output disabled records secondly for better organization. # Output disabled records secondly for better organization.

View File

@ -1,6 +1,6 @@
{ {
"name": "hosts", "name": "hosts",
"version": "1.4.3", "version": "1.5.0",
"description": "A command line tool for managing hosts file entries.", "description": "A command line tool for managing hosts file entries.",
"global": true, "global": true,
"install": "make install" "install": "make install"