-
Notifications
You must be signed in to change notification settings - Fork 14
/
CmdUpdate.go
60 lines (57 loc) · 1.39 KB
/
CmdUpdate.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
50
51
52
53
54
55
56
57
58
59
60
package wpfinger
import (
"fmt"
"github.com/schollz/progressbar/v3"
"io"
"net/http"
"os"
"path"
"time"
)
type CmdUpdateDb struct {
}
func (builder *CmdUpdateDb) Run() error {
configDir := EnsureAndGetConfigDirectory()
updateUri := "https://media.leakix.net/wpfinger/update.db"
resp, err := http.Head(updateUri)
if err != nil {
panic(err)
}
lastModifiedString := resp.Header.Get("Last-Modified")
lastModifiedTime, err := time.Parse(time.RFC1123, lastModifiedString)
if err != nil {
panic(err)
}
localDbFile, err := os.Stat(path.Join(configDir, "database.db"))
if err != nil && !os.IsNotExist(err) {
panic(err)
}
if err == nil && !lastModifiedTime.After(localDbFile.ModTime()) && resp.ContentLength == localDbFile.Size() {
fmt.Println("Database is up to date")
return nil
}
downloadResp, err := http.Get(updateUri)
if err != nil {
panic(err)
}
if downloadResp.StatusCode != 200 {
panic("error downloading update: " + resp.Status)
}
localDbForUpdate, err := os.OpenFile(path.Join(configDir, "database.db"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer localDbForUpdate.Close()
bar := progressbar.DefaultBytes(
resp.ContentLength,
"Updating database ...",
)
n, err := io.Copy(io.MultiWriter(localDbForUpdate, bar), downloadResp.Body)
if err != nil {
panic(err)
}
if n != resp.ContentLength {
panic("download incomplete")
}
return nil
}