59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
Repos []RepoConfig `json:"repos"`
|
|
Interval int `json:"interval"` // Polling interval in seconds
|
|
NtfyTopic string `json:"ntfy_topic"`
|
|
}
|
|
|
|
func loadConfig(file string) (Config, error) {
|
|
var config Config
|
|
fh, err := os.OpenFile(file, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
log.Fatalf("Failed to open config file: %v", err)
|
|
return Config{}, err
|
|
}
|
|
data, err := io.ReadAll(fh)
|
|
if err != nil {
|
|
log.Fatalf("Failed to read config file: %v", err)
|
|
return Config{}, err
|
|
}
|
|
if err := json.Unmarshal(data, &config); err != nil {
|
|
log.Fatalf("Failed to parse config file: %v", err)
|
|
return Config{}, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
type Notifier interface {
|
|
Notify(repo Repo, tag string) bool
|
|
}
|
|
|
|
type NtfyNotifier struct {
|
|
Topic string
|
|
}
|
|
|
|
func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
|
ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
|
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner(), repo.Name(), tag)
|
|
|
|
_, err := http.Post(ntfyURL, "text/plain", bytes.NewBuffer([]byte(message)))
|
|
if err != nil {
|
|
log.Printf("Failed to send notification: %v", err)
|
|
return false
|
|
}
|
|
log.Printf("Notification sent: %s", message)
|
|
return true
|
|
}
|