Change set/unset to build/revert and improve output

This commit is contained in:
Kevin MacMartin 2014-08-17 23:18:44 -04:00
parent bf6d496e68
commit f6f31c8a56
3 changed files with 40 additions and 39 deletions

View file

@ -8,14 +8,14 @@ Build and maintain `/etc/hosts` while including lists imported from local and re
## Enable Remote Sources ## ## Enable Remote Sources ##
1. The first time you run the `set` command, `/etc/hosts` will be moved to `/etc/hosts.core`, and the file `/etc/hosts.sources` will be created using the default sources list. The `/etc/hosts` file will then be generated by combining `/etc/hosts.core` and the resulting contents of the downloaded sources in `/etc/hosts.sources`. 1. The first time you run the `build` command, `/etc/hosts` will be moved to `/etc/hosts.core`, and the file `/etc/hosts.sources` will be created using the default sources list. The `/etc/hosts` file will then be generated by combining `/etc/hosts.core` and the resulting contents of the downloaded sources in `/etc/hosts.sources`.
2. You can now open `/etc/hosts.sources` and remove or comment out (by adding `#` in front) any sources you don't wish to use, as well as add new ones. Sources can remote (ie: `http://adaway.org/hosts.txt`) as well as local (ie: `file:///etc/hosts.d/mylist.txt`). 2. You can now open `/etc/hosts.sources` and remove or comment out (by adding `#` in front) any sources you don't wish to use, as well as add new ones. Sources can remote (ie: `http://adaway.org/hosts.txt`) as well as local (ie: `file:///etc/hosts.d/mylist.txt`).
3. If you want to make any changes to the contents of what used to be `/etc/hosts`, you should now make them to `/etc/hosts.core`, and you'll need to rerun the `set` command to have them applied. 3. If you want to make any changes to the contents of what used to be `/etc/hosts`, you should now make them to `/etc/hosts.core`, and you'll need to rerun the `build` command to have them applied.
4. Once you're done customizing your configuration, run the `set` command again and the `/etc/hosts` file will be regenerated using the current list of sources and your up-to-date `/etc/hosts.core` file. 4. Once you're done customizing your configuration, run the `build` command again and the `/etc/hosts` file will be regenerated using the current list of sources and your up-to-date `/etc/hosts.core` file.
## Disable Remote Sources ## ## Disable Remote Sources ##
1. To disable the configured hosts lists, run the `unset` command and `/etc/hosts.core` will be moved back to `/etc/hosts`. 1. To disable the configured hosts lists, run the `revert` command and `/etc/hosts.core` will be moved back to `/etc/hosts`.
* The `/etc/hosts.sources` file will continue to exist until it's manually deleted, and it will be used again the next time remote sources are enabled. * The `/etc/hosts.sources` file will continue to exist until it's manually deleted, and it will be used again the next time remote sources are enabled.
## Notes on /etc/hosts.sources ## ## Notes on /etc/hosts.sources ##
@ -27,8 +27,8 @@ Build and maintain `/etc/hosts` while including lists imported from local and re
## Commands ## ## Commands ##
* `buildhosts set`: generate `/etc/hosts` using `/etc/hosts.core` and the configured sources * `buildhosts build`: generate `/etc/hosts` using `/etc/hosts.core` and the configured sources
* `buildhosts unset`: remove lists imported from hosts list sources and restore `/etc/hosts` * `buildhosts revert`: remove lists imported from hosts list sources and restore `/etc/hosts`
* `buildhosts help`: display the help * `buildhosts help`: display the help
## Extra ## ## Extra ##

View file

@ -6,7 +6,7 @@
# Instructions (as root): # Instructions (as root):
# #
# Enable Remote Sources # Enable Remote Sources
# 1. The first time you run the 'set' command, /etc/hosts will be # 1. The first time you run the 'build' command, /etc/hosts will be
# moved to /etc/hosts.core, and the file /etc/hosts.sources will # moved to /etc/hosts.core, and the file /etc/hosts.sources will
# be created using the default sources list. The /etc/hosts file # be created using the default sources list. The /etc/hosts file
# will then be generated by combining /etc/hosts.core and the # will then be generated by combining /etc/hosts.core and the
@ -16,14 +16,14 @@
# well as add new ones. Sources can remote as well as local. # well as add new ones. Sources can remote as well as local.
# 3. If you want to make any changes to the contents of what used to # 3. If you want to make any changes to the contents of what used to
# be /etc/hosts, you should now make them to /etc/hosts.core, and # be /etc/hosts, you should now make them to /etc/hosts.core, and
# you'll need to rerun the 'set' command to have them applied. # you'll need to rerun the 'build' command to have them applied.
# 4. Once you're done customizing your configuration, run the 'set' # 4. Once you're done customizing your configuration, run the 'build'
# command again and the /etc/hosts file will be regenerated using # command again and the /etc/hosts file will be regenerated using
# the current list of sources and your up-to-date /etc/hosts.core # the current list of sources and your up-to-date /etc/hosts.core
# file. # file.
# #
# Disable Remote Sources # Disable Remote Sources
# 1. To disable the configured hosts lists, run the 'unset' command # 1. To disable the configured hosts lists, run the 'revert' command
# and /etc/hosts.core will be moved back to /etc/hosts. # and /etc/hosts.core will be moved back to /etc/hosts.
# * The /etc/hosts.sources file will continue to exist until it's # * The /etc/hosts.sources file will continue to exist until it's
# manually deleted, and it will be used again the next time remote # manually deleted, and it will be used again the next time remote
@ -63,31 +63,32 @@ BUILDHOSTS_SCRIPT="$(basename $0)"
if [[ "$NOCOL" -eq 1 ]]; then if [[ "$NOCOL" -eq 1 ]]; then
HEADINGCOL="|" HEADINGCOL="|"
else else
HEADINGCOL="\e[1;41m" HEADINGCOL="\e[1;40m"
SUCCESSCOL="\e[1;40m" SUCCESSCOL="\e[1;42m"
FAILCOL="\e[1;40m" FAILCOL="\e[1;41m"
RESETCOL="\e[0m" RESETCOL="\e[0m"
fi fi
# Help function: display help text # Help function: display help text
function buildhosts_help(){ function buildhosts_help(){
echo -e "${HEADINGCOL} ${BUILDHOSTS_SCRIPT}:${RESETCOL} build and maintain /etc/hosts with local and remote hosts list files\n${HEADINGCOL} ${RESETCOL}" echo -e "${BUILDHOSTS_SCRIPT}: build /etc/hosts with your hosts list sources"
echo -e "${HEADINGCOL} commands:${RESETCOL}" echo -e "\nusage: ${BUILDHOSTS_SCRIPT} [COMMAND]"
echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} set:${RESETCOL} add hosts lists to /etc/hosts" echo -e "\ncommands:"
echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} unset:${RESETCOL} remove hosts lists from /etc/hosts" echo -e " build: add hosts lists to /etc/hosts"
echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} help:${RESETCOL} display this help" echo -e " revert: remove hosts lists from /etc/hosts"
echo -e " help: display this help"
[[ "$1" -eq 1 ]] && exit 0 [[ "$1" -eq 1 ]] && exit 0
} }
# Error function: display error then exit unsuccessfully # Error function: display error then exit unsuccessfully
function buildhosts_error() { function buildhosts_error() {
echo -e "${HEADINGCOL} ${RESETCOL}${FAILCOL} ! ERROR:${RESETCOL}${FAILCOL} ${1}${RESETCOL}" echo -e "${HEADINGCOL} ${RESETCOL}${FAILCOL} ! ERROR:${RESETCOL}${FAILCOL} ${1}${RESETCOL}"
[[ "$2" -eq 1 ]] && echo -e "${HEADINGCOL} ${RESETCOL}" && buildhosts_help [[ "$2" -eq 1 ]] && echo && buildhosts_help
exit 1 exit 1
} }
# The set function to generate or regenerate /etc/hosts using /etc/hosts.core and the list of sources # The build function to generate or regenerate /etc/hosts using /etc/hosts.core and the list of sources
function buildhosts_set() { function buildhosts_build() {
# If $HOSTS_SOURCES doesn't exist, generate one using the default list of sources # If $HOSTS_SOURCES doesn't exist, generate one using the default list of sources
if [[ ! -f "$HOSTS_SOURCES" ]]; then if [[ ! -f "$HOSTS_SOURCES" ]]; then
echo -e "${HEADINGCOL} Generating Default Sources:${RESETCOL} ${HOSTS_SOURCES}" echo -e "${HEADINGCOL} Generating Default Sources:${RESETCOL} ${HOSTS_SOURCES}"
@ -141,19 +142,19 @@ function buildhosts_set() {
if [[ -n $(cat "$HOSTS_SYSTEM") ]]; then if [[ -n $(cat "$HOSTS_SYSTEM") ]]; then
echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} Done! ${RESETCOL}" echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} Done! ${RESETCOL}"
else else
echo -e "${HEADINGCOL} ${RESETCOL}${FAILCOL} Failed...${RESETCOL} (unseting to core hosts file)" echo -e "${HEADINGCOL} ${RESETCOL}${FAILCOL} Failed... ${RESETCOL} (reverting hosts file)"
cp "$MAINHOSTS" "$HOSTS_SYSTEM" cp "$MAINHOSTS" "$HOSTS_SYSTEM"
buildhosts_error "The generated hosts file @ ${HOSTS_SYSTEM} could not be created" buildhosts_error "The generated hosts file @ ${HOSTS_SYSTEM} could not be created"
exit 1 exit 1
fi fi
} }
function buildhosts_unset() { function buildhosts_revert() {
# Fail if $HOSTS_CORE doesn't exist # Fail if $HOSTS_CORE doesn't exist
[[ -f "$HOSTS_CORE" ]] || buildhosts_error "The core file ${HOSTS_CORE} does not exist, cannot unset" [[ -f "$HOSTS_CORE" ]] || buildhosts_error "The core file ${HOSTS_CORE} does not exist, cannot revert"
# Fail if $HOSTS_CORE is empty when all comments are removed # Fail if $HOSTS_CORE is empty when all comments are removed
[[ -n $(sed 's|^\ *#.*$||' "$HOSTS_CORE" | tr -d "\n") ]] || buildhosts_error "The core file ${HOSTS_CORE} contains no information, cannot unset" [[ -n $(sed 's|^\ *#.*$||' "$HOSTS_CORE" | tr -d "\n") ]] || buildhosts_error "The core file ${HOSTS_CORE} contains no information, cannot revert"
# Move $HOSTS_CORE to $HOSTS_SYSTEM # Move $HOSTS_CORE to $HOSTS_SYSTEM
echo -e "${HEADINGCOL} Moving:${RESETCOL} ${HOSTS_CORE} to ${HOSTS_SYSTEM}" echo -e "${HEADINGCOL} Moving:${RESETCOL} ${HOSTS_CORE} to ${HOSTS_SYSTEM}"
@ -169,11 +170,11 @@ function buildhosts_unset() {
[[ -n "$1" ]] || buildhosts_error "You must run ${BUILDHOSTS_SCRIPT} with a valid argument" 1 [[ -n "$1" ]] || buildhosts_error "You must run ${BUILDHOSTS_SCRIPT} with a valid argument" 1
case "$1" in case "$1" in
set) build)
buildhosts_set buildhosts_build
;; ;;
unset) revert)
buildhosts_unset buildhosts_revert
;; ;;
help) help)
buildhosts_help 1 buildhosts_help 1

View file

@ -35,10 +35,10 @@
.SH "COMMANDS" .SH "COMMANDS"
. .
.TP .TP
\fBset\fR: generate \fB/etc/hosts\fR using \fB/etc/hosts\.core\fR and the configured sources \fBbuild\fR: generate \fB/etc/hosts\fR using \fB/etc/hosts\.core\fR and the configured sources
.TP .TP
.TP .TP
\fBunset\fR: remove lists imported from hosts list sources and restore \fB/etc/hosts\fR \fBrevert\fR: remove lists imported from hosts list sources and restore \fB/etc/hosts\fR
.TP .TP
.TP .TP
\fBhelp\fR: display the help \fBhelp\fR: display the help
@ -49,7 +49,7 @@
.SS "Enable Remote Sources" .SS "Enable Remote Sources"
.TP .TP
.IP "1." .IP "1."
The first time you run the \fBset\fR command, \fB/etc/hosts\fR will be moved to \fB/etc/hosts\.core\fR, and the file \fB/etc/hosts\.sources\fR will be created using the default sources list\. The \fB/etc/hosts\fR file will then be generated by combining \fB/etc/hosts\.core\fR and the resulting contents of the downloaded sources in \fB/etc/hosts\.sources\fR\. The first time you run the \fBbuild\fR command, \fB/etc/hosts\fR will be moved to \fB/etc/hosts\.core\fR, and the file \fB/etc/hosts\.sources\fR will be created using the default sources list\. The \fB/etc/hosts\fR file will then be generated by combining \fB/etc/hosts\.core\fR and the resulting contents of the downloaded sources in \fB/etc/hosts\.sources\fR\.
.TP .TP
.TP .TP
.BR "2." .BR "2."
@ -57,15 +57,15 @@ You can now open \fB/etc/hosts\.sources\fR and remove or comment out (by adding
.TP .TP
.TP .TP
.BR "3." .BR "3."
If you want to make any changes to the contents of what used to be \fB/etc/hosts\fR, you should now make them to \fB/etc/hosts\.core\fR, and you\'ll need to rerun the \fBset\fR command to have them applied\. If you want to make any changes to the contents of what used to be \fB/etc/hosts\fR, you should now make them to \fB/etc/hosts\.core\fR, and you\'ll need to rerun the \fBbuild\fR command to have them applied\.
.TP .TP
.TP .TP
.BR "4." .BR "4."
Once you\'re done customizing your configuration, run the \fBset\fR command again and the \fB/etc/hosts\fR file will be regenerated using the current list of sources and your up\-to\-date \fB/etc/hosts\.core\fR file\. Once you\'re done customizing your configuration, run the \fBbuild\fR command again and the \fB/etc/hosts\fR file will be regenerated using the current list of sources and your up\-to\-date \fB/etc/hosts\.core\fR file\.
.SS "Disable Remote Sources" .SS "Disable Remote Sources"
.TP .TP
.BR "1." .BR "1."
To disable the configured hosts lists, run the \fBunset\fR command and \fB/etc/hosts\.core\fR will be moved back to \fB/etc/hosts\fR\. To disable the configured hosts lists, run the \fBrevert\fR command and \fB/etc/hosts\.core\fR will be moved back to \fB/etc/hosts\fR\.
.TP .TP
.TP .TP
.BR "*" .BR "*"