-
Notifications
You must be signed in to change notification settings - Fork 2
/
hosts-file.go
47 lines (38 loc) · 852 Bytes
/
hosts-file.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
package main
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"time"
)
type hostsFileEntry struct {
hostname *string
ip *string
ipResovledFrom *string
}
type hostsFileEntries []*hostsFileEntry
func hostsFileLocation() *string {
var hostsFile string
if runtime.GOOS == "windows" {
hostsFile = os.Getenv("SystemRoot") + "\\System32\\drivers\\etc\\hosts"
} else {
hostsFile = "/etc/hosts"
}
return &hostsFile
}
func createHostsBackup(hostsFileLocation *string) {
input, err := ioutil.ReadFile(*hostsFileLocation)
if err != nil {
fmt.Println(err)
return
}
backupLocation := *hostsFileLocation + ".backup-" + strconv.FormatInt(time.Now().Unix(), 10)
err = ioutil.WriteFile(backupLocation, input, 0644)
if err != nil {
fmt.Println("Error creating", backupLocation)
fmt.Println(err)
return
}
}