-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (75 loc) · 1.74 KB
/
main.go
File metadata and controls
86 lines (75 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"time"
"github.com/xuhe2/github520cli/utils"
)
func main() {
// update hosts file once
if len(os.Args) == 1 {
UpdateHosts()
}
// has more args
// if arg has `--auto/-a`, update hosts file every 1 hour
args := os.Args[1:]
for _, arg := range args {
switch arg {
case "--auto", "-a":
handleAutoArg()
}
}
}
func handleAutoArg() {
ticker := time.NewTicker(time.Hour)
for range ticker.C {
UpdateHosts()
}
}
func UpdateHosts() {
// call api for hosts file content
url := "https://raw.hellogithub.com/hosts"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
newHostsContent := string(body)
if utils.CheckConfigAvailable(newHostsContent) {
slog.Info("config available")
} else {
slog.Error("config not available")
os.Exit(1)
}
// get hosts config file
hostFilePath := utils.GetHostsFilePath()
hostFile, err := os.Open(hostFilePath)
if err != nil {
panic(err)
}
defer hostFile.Close()
oldHostsContentBytes, err := io.ReadAll(hostFile)
if err != nil {
panic(err)
}
oldHostsContent := string(oldHostsContentBytes)
// analy diff and update hosts file
// analy diff
diffs := utils.AnalyConfigDiff(oldHostsContent, newHostsContent)
for _, diff := range diffs {
fmt.Println(diff)
}
// update hosts file
updatedHostsContent := utils.UpdateConfigFileContent(oldHostsContent, newHostsContent)
// `0644`表示文件所有者有读写权限,其他用户只有读权限
if err := os.WriteFile(hostFilePath, []byte(updatedHostsContent), 0644); err != nil {
panic(err)
}
}