forked from stay-sharp/hosts_for_google_service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
由于hosts检测脚本有很多不必要的判断/操作,且依赖 dos2unix 的高版本。 因此,重新修正检测方式,使用 file 替代 dos2unix 来侦测 DOS 换行符, 取消设置环境变量才检验日期和格式的限制。 同时也精简了格式化hosts的脚本。 Signed-off-by: Andy Deng <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
language: c | ||
sudo: requried | ||
sudo: false | ||
dist: trusty | ||
|
||
before_install: | ||
- sudo apt-get update -qq | ||
- sudo apt-get install -qq -y gettext | ||
|
||
install: | ||
- wget -q https://github.com/racaljk/hosts/releases/download/checkhosts/dos2unix-7.3.4.tar.gz | ||
- tar xf dos2unix-7.3.4.tar.gz | ||
- cd dos2unix-7.3.4 && make && sudo make prefix=/usr install | ||
- cd .. | ||
|
||
script: | ||
# Be permissive, unset following enviroment | ||
- export STRICT_HOSTS_FORMAT=1 | ||
- bash tools/checkhosts.sh hosts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,41 @@ | ||
#!/bin/bash | ||
|
||
hosts_date() { | ||
local repo_date=$(git log --date=short -n1 "$1" | egrep "[0-9]+-[0-9]+-[0-9]+" -o -m1) | ||
local cur_date=$(date +%F) | ||
|
||
# detect hosts file changes | ||
if git diff --exit-code "$1" > /dev/null; then | ||
# hosts file isn't changed. | ||
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(echo "$repo_date")/" "$1" | ||
else | ||
# hosts file is changed. | ||
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(echo "$cur_date")/" "$1" | ||
fi | ||
# https://github.com/racaljk/hosts | ||
|
||
date_amend() | ||
{ | ||
# check if hosts file changes | ||
if git diff --exit-code "$1" > /dev/null; then | ||
# hosts file is not changed, | ||
# the date string will be set to the git log records. | ||
local repo_date=$(git log --date=short -n1 "$1" | \ | ||
grep -Pom1 "\d+-\d+-\d+") | ||
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$repo_date/" "$1" | ||
else | ||
# hosts file has been changed, | ||
# set date string to the system date. | ||
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(date +%F)/" "$1" | ||
fi | ||
} | ||
|
||
hosts_line() { | ||
local your_os=$(uname -s | cut -c 1-5) | ||
|
||
if [ "$your_os" = "MINGW" -o "$your_os" = "CYGWI" ]; then | ||
echo -e "Note: Please check line endings in Windows by yourself." | ||
else | ||
dos2unix -q "$1" | ||
fi | ||
eol_amend() | ||
{ | ||
dos2unix -q "$1" | ||
} | ||
|
||
hosts_format() { | ||
local cur="\([0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+\)[[:blank:]]\+" | ||
local fmt="\1$(echo -e "\t")" | ||
style_amend() | ||
{ | ||
local orig="\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)[[:blank:]]\+" | ||
local modified="\1$(echo -e "\t")" | ||
|
||
sed -i "s/$(echo "$cur")/$(echo "$fmt")/g" "$1" | ||
sed -i "s/$orig/$modified/g" "$1" | ||
} | ||
|
||
if [ "$1" = "" ]; then | ||
echo -e "Error, requires an argument!\n\n" | ||
echo -e "Example:\n\ntools/Hindent hosts" | ||
exit 1 | ||
if [ -z "$1" ]; then | ||
echo "Usage: $0 [your-hosts-file]" | ||
exit 1 | ||
fi | ||
|
||
hosts_line "$1" | ||
hosts_date "$1" | ||
hosts_format "$1" | ||
|
||
eol_amend "$1" | ||
date_amend "$1" | ||
style_amend "$1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters