From f6f31c8a5684c9eec522533a28a9d3c65a2f23d2 Mon Sep 17 00:00:00 2001 From: Kevin MacMartin Date: Sun, 17 Aug 2014 23:18:44 -0400 Subject: [PATCH] Change set/unset to build/revert and improve output --- README.md | 12 ++++++------ buildhosts | 55 ++++++++++++++++++++++++++-------------------------- buildhosts.1 | 12 ++++++------ 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 92a1008..7ff1bd9 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ Build and maintain `/etc/hosts` while including lists imported from local and re ## 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`). -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. -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. +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 `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 ## -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. ## Notes on /etc/hosts.sources ## @@ -27,8 +27,8 @@ Build and maintain `/etc/hosts` while including lists imported from local and re ## Commands ## -* `buildhosts set`: 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 build`: generate `/etc/hosts` using `/etc/hosts.core` and the configured sources +* `buildhosts revert`: remove lists imported from hosts list sources and restore `/etc/hosts` * `buildhosts help`: display the help ## Extra ## diff --git a/buildhosts b/buildhosts index 3e4677f..49ce430 100755 --- a/buildhosts +++ b/buildhosts @@ -6,7 +6,7 @@ # Instructions (as root): # # 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 # be created using the default sources list. The /etc/hosts file # 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. # 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. -# 4. Once you're done customizing your configuration, run the 'set' +# you'll need to rerun the 'build' command to have them applied. +# 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 -# 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. # * The /etc/hosts.sources file will continue to exist until it's # 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 HEADINGCOL="|" else - HEADINGCOL="\e[1;41m" - SUCCESSCOL="\e[1;40m" - FAILCOL="\e[1;40m" + HEADINGCOL="\e[1;40m" + SUCCESSCOL="\e[1;42m" + FAILCOL="\e[1;41m" RESETCOL="\e[0m" fi # Help function: display help text 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 "${HEADINGCOL} commands:${RESETCOL}" - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} set:${RESETCOL} add hosts lists to /etc/hosts" - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} unset:${RESETCOL} remove hosts lists from /etc/hosts" - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} help:${RESETCOL} display this help" + echo -e "${BUILDHOSTS_SCRIPT}: build /etc/hosts with your hosts list sources" + echo -e "\nusage: ${BUILDHOSTS_SCRIPT} [COMMAND]" + echo -e "\ncommands:" + echo -e " build: add hosts lists to /etc/hosts" + echo -e " revert: remove hosts lists from /etc/hosts" + echo -e " help: display this help" [[ "$1" -eq 1 ]] && exit 0 } # Error function: display error then exit unsuccessfully function buildhosts_error() { 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 } -# The set function to generate or regenerate /etc/hosts using /etc/hosts.core and the list of sources -function buildhosts_set() { +# The build function to generate or regenerate /etc/hosts using /etc/hosts.core and the list of sources +function buildhosts_build() { # If $HOSTS_SOURCES doesn't exist, generate one using the default list of sources if [[ ! -f "$HOSTS_SOURCES" ]]; then echo -e "${HEADINGCOL} Generating Default Sources:${RESETCOL} ${HOSTS_SOURCES}" @@ -103,7 +104,7 @@ function buildhosts_set() { # If $HOSTS_CORE doesn't exist and $HOSTS_SYSTEM does, move $HOSTS_SYSTEM to $HOSTS_CORE if [ -f "$HOSTS_SYSTEM" -a ! -f "$HOSTS_CORE" ]; then - echo -e "${HEADINGCOL} Moving:${RESETCOL} ${HOSTS_SYSTEM} to ${HOSTS_CORE}" + echo -e "${HEADINGCOL} Moving: ${RESETCOL} ${HOSTS_SYSTEM} to ${HOSTS_CORE}" cp "$HOSTS_SYSTEM" "$HOSTS_CORE" fi @@ -111,7 +112,7 @@ function buildhosts_set() { unset TMPHOSTS while read -r source; do if [[ -n "$source" ]]; then - echo -e "${HEADINGCOL} Downloading:${RESETCOL} ${source}" + echo -e "${HEADINGCOL} Downloading: ${RESETCOL} ${source}" # Download the the current source into $SRCDATA and fail if the result is empty SRCDATA=$(curl -C - -s "$source" | tr -d "\r") @@ -131,7 +132,7 @@ function buildhosts_set() { s|^\ *[^1].*$||' <<< "$TMPHOSTS")) # Add the system hosts file to the hosts list and warn if $HOSTS_CORE is missing - echo -e "${HEADINGCOL} Writing:${RESETCOL} ${HOSTS_CORE} and $(wc -l <<< "$TMPHOSTS") host source entries to ${HOSTS_SYSTEM}" + echo -e "${HEADINGCOL} Writing: ${RESETCOL} ${HOSTS_CORE} and $(wc -l <<< "$TMPHOSTS") host source entries to ${HOSTS_SYSTEM}" [[ -f "$HOSTS_CORE" ]] \ && TMPHOSTS=$(cat "$HOSTS_CORE")$(echo -e "\n\n# Generated Host List (${BUILDHOSTS_SCRIPT})")${TMPHOSTS} \ || echo -e "${HEADINGCOL} ${RESETCOL}${FAILCOL} Warning:${RESETCOL} Core hosts file ${HOSTS_CORE} does not exist" @@ -139,21 +140,21 @@ function buildhosts_set() { # Write $TMPHOSTS to $HOSTS_SYSTEM if it's not empty [[ -n "$TMPHOSTS" ]] && echo -e "$TMPHOSTS" > "$HOSTS_SYSTEM" if [[ -n $(cat "$HOSTS_SYSTEM") ]]; then - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} Done!${RESETCOL}" + echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} Done! ${RESETCOL}" 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" buildhosts_error "The generated hosts file @ ${HOSTS_SYSTEM} could not be created" exit 1 fi } -function buildhosts_unset() { +function buildhosts_revert() { # 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 - [[ -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 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 case "$1" in - set) - buildhosts_set + build) + buildhosts_build ;; - unset) - buildhosts_unset + revert) + buildhosts_revert ;; help) buildhosts_help 1 diff --git a/buildhosts.1 b/buildhosts.1 index e7ac98a..2408ae5 100644 --- a/buildhosts.1 +++ b/buildhosts.1 @@ -35,10 +35,10 @@ .SH "COMMANDS" . .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 -\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 \fBhelp\fR: display the help @@ -49,7 +49,7 @@ .SS "Enable Remote Sources" .TP .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 .BR "2." @@ -57,15 +57,15 @@ You can now open \fB/etc/hosts\.sources\fR and remove or comment out (by adding .TP .TP .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 .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" .TP .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 .BR "*"