-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (44 loc) · 848 Bytes
/
main.go
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
)
func main() {
if runtime.GOOS == "windows" {
os.Chdir("c:/windows/system32/drivers/etc")
done()
} else {
os.Chdir("/etc")
done()
}
}
func isExist(name string) bool {
_, err := os.Stat(name)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func done() {
fmt.Println("Read remote data...")
res, err := http.Get("https://raw.githubusercontent.com/racaljk/hosts/master/hosts")
if err != nil {
fmt.Println(err.Error())
return
}
defer res.Body.Close()
hostsByte, _ := ioutil.ReadAll(res.Body)
if !isExist("hosts.bak") {
os.Rename("hosts", "hosts.bak")
}
fmt.Println("Write remote data...")
err = ioutil.WriteFile("hosts", hostsByte, 0644)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Write done!")
}