Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshcn committed Jan 6, 2024
1 parent 2ac64bb commit 9b1af3d
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: release

on:
push:
tags:
- 'v*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: v1.23.0
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
dist
46 changes: 46 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
project_name: hosts-go
before:
hooks:
- go mod tidy
builds:
- id: hosts-go
main: ./
binary: hosts-go
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- "386"
- amd64
- arm
- arm64
goarm:
- "6"
- "7"
ignore:
- goos: windows
goarch: arm
- goos: darwin
goarch: arm
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}}

archives:
- format: binary

changelog:
use: github
groups:
- title: Features
regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$'
order: 0
- title: "Bug fixes"
regexp: '^.*?(?:bug|fix)(\([[:word:]]+\))??!?:.+$'
order: 1
- title: Others
order: 999
54 changes: 54 additions & 0 deletions README.EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# hosts-go

[中文](README.md) | English

## Overview

hosts-go is a command-line tool for fetching and merging hosts files from the internet. It can periodically fetch hosts files from specified URLs and merge them into the local hosts file.

## Installation

To install hosts-go to your system, use the following command:

```
go install github.com/hunshcn/hosts-go
```

## Usage

hosts-go provides the following command-line options:

- `--url` or `-u`: Specify the URLs to fetch hosts files from. Multiple URLs can be specified.
- `--test` or `-t`: Only output the merged hosts file content.
- `--content-only`: Only output the fetched hosts file content.
- `--service` or `-s`: Install or uninstall hosts-go as a system service.
- `--duration` or `-d`: Specify the duration between each fetch of hosts files. The default is 1 hour.
- `--reload-command`:Command to execute after successfully updating the hosts file.

### Examples

Fetch and merge hosts files:

```
hosts-go --url https://gitlab.com/ineo6/hosts
```

Install hosts-go as a system service:

```
hosts-go -s install
```

Uninstall hosts-go service:

```
hosts-go -s uninstall
```

### Notes

- Before running the hosts-go command, make sure you have sufficient permissions to read and write the hosts file.

## License

MIT License.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# hosts-go

中文 | [English](README.EN.md)

## 概述

hosts-go 是一个用于从互联网上获取和合并 hosts 文件的命令行工具。它可以定期从指定的 URL 获取 hosts 文件,并将其合并到本地的 hosts 文件中。

## 安装

使用以下命令将 hosts-go 安装到您的系统中:

```
go install github.com/hunshcn/hosts-go
```

## 使用
> [!NOTE]
> v1.0.0 发布之前可能进行较大 API 变更。
hosts-go 提供了以下命令行选项:

- `--url``-u`:指定要获取 hosts 文件的 URL。可以指定多个 URL。
- `--test``-t`:仅输出合并后的 hosts 文件内容。
- `--content-only`:仅输出获取的 hosts 文件内容。
- `--service``-s`:安装或卸载 hosts-go 作为系统服务。
- `--duration``-d`:指定更新 hosts 文件的时间间隔,默认为 1 小时。
- `--reload-command`:在更新成功 hosts 文件后执行的命令。

### 示例

获取并合并 hosts 文件:

```
hosts-go -u https://gitlab.com/ineo6/hosts
```

安装 hosts-go 作为系统服务:

```
hosts-go -s install
```

卸载 hosts-go 服务:

```
hosts-go -s uninstall
```

### 注意事项

- 在运行 hosts-go 之前,请确保您具有足够的权限来读取和写入 hosts 文件。

## License

MIT License.
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/hunshcn/hosts-go

go 1.21

require (
github.com/kardianos/service v1.2.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 10 additions & 0 deletions hosts_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"os"
"path/filepath"
)

func init() {
systemHostsPath = filepath.Join(os.Getenv("SystemRoot"), "System32", "drivers", "etc", "hosts")
}
Loading

0 comments on commit 9b1af3d

Please sign in to comment.