-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (52 loc) · 1.48 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
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
u "net/url"
"os"
"strings"
"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 client.SendMessageEvent(context.TODO(), rid, event.EventMessage, md)
}
// date looks like 2024-12-10
func getComponents(url string) (attr_path, date string) {
components := strings.Split(url, "/")
attr_path = components[len(components)-2]
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 {
parsedURL, err := u.Parse(*mainURL)
if err != nil {
panic(err)
}
return parsedURL.JoinPath(attr_path).String()
}
func logURL(attr_path, date string) string {
purl := packageURL(attr_path)
// it's safe to just use string concatenation here because we're sure any
// trailing / has been removed by call to packageURL
return fmt.Sprintf("%s/%s.log", purl, date)
}
func fatal(err error) {
slog.Error("error", err)
os.Exit(restartExitCode)
}