-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
84 lines (67 loc) · 1.99 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"strings"
"time"
"github.com/andybalholm/brotli"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
)
const restartExitCode = 100
func newReqWithUA(url string) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "https://github.com/asymmetric/nixpkgs-update-notifier")
return req, nil
}
func sendMarkdown(text string, rid id.RoomID) (*mautrix.RespSendEvent, error) {
md := format.RenderMarkdown(text, true, true)
return clients.matrix.SendMessageEvent(context.TODO(), rid, event.EventMessage, md)
}
// Given a log url, returns its date.
//
// date looks like 2024-12-10
func getDate(url string) (date string) {
components := strings.Split(url, "/")
date = strings.Trim(components[len(components)-1], ".log")
return
}
// Returns the full package URL by appending its attr_path to the base URL.
func packageURL(attr_path string) string {
return fmt.Sprintf("%s/%s", strings.Trim(*mainURL, "/"), strings.Trim(attr_path, "/"))
}
func logURL(attr_path, date string) string {
purl := packageURL(attr_path)
return fmt.Sprintf("%s/%s.log", purl, date)
}
// This one should be used if there's an irrecoverable problem, e.g. IO with the DB.
func fatal(err error) {
slog.Error("error", err)
os.Exit(restartExitCode)
}
// TODO: log duration of the whole thing to Info
// Fetches the packages.json.br, unpacks it and parses it.
func fetchPackagesJSON() {
slog.Debug("downloading packages.json.br")
start := time.Now()
resp, err := http.Get(packagesURL)
if err != nil {
panic(err)
}
defer resp.Body.Close()
slog.Debug("downloaded packages.json.br")
slog.Debug("parsing packages.json")
if err := json.NewDecoder(brotli.NewReader(resp.Body)).Decode(&jsblob); err != nil {
panic(err)
}
slog.Info("package.json handling completed", "elapsed", time.Since(start))
}