Creating config if not exists via templating
This commit is contained in:
parent
416145dfaa
commit
0b11ac87cd
3 changed files with 63 additions and 8 deletions
48
src/main.go
48
src/main.go
|
@ -1,12 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed beretta.json
|
||||||
|
var defconfig string
|
||||||
|
|
||||||
type RepoUpdate struct {
|
type RepoUpdate struct {
|
||||||
repo Repo
|
repo Repo
|
||||||
Tag string
|
Tag string
|
||||||
|
@ -22,7 +28,7 @@ func notifierThread(c chan RepoUpdate, n Notifier) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
log.Println("Checking", repo.IdTuple())
|
log.Println("Checking: ", repo.IdTuple())
|
||||||
tag, err := repo.GetLatestTag()
|
tag, err := repo.GetLatestTag()
|
||||||
isNewVersion, err_2 := db.CheckAndStore(repo.IdTuple(), tag.Name)
|
isNewVersion, err_2 := db.CheckAndStore(repo.IdTuple(), tag.Name)
|
||||||
|
|
||||||
|
@ -33,6 +39,7 @@ func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
} else if isNewVersion {
|
} else if isNewVersion {
|
||||||
c <- RepoUpdate{repo, tag.Name}
|
c <- RepoUpdate{repo, tag.Name}
|
||||||
}
|
}
|
||||||
|
log.Println("Checking done: ", repo.IdTuple())
|
||||||
|
|
||||||
sleepTime := time.Duration(rand.Intn(avgInterval)) * time.Second
|
sleepTime := time.Duration(rand.Intn(avgInterval)) * time.Second
|
||||||
time.Sleep(sleepTime)
|
time.Sleep(sleepTime)
|
||||||
|
@ -41,25 +48,54 @@ func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
conf_path := flag.String("config", "beretta.json", "The path to the config file")
|
||||||
conf := flag.String("config", "./beretta.json", "The path to the config file")
|
db_path := flag.String("db", "cache.db", "The path to the cache database")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
log.Printf("Using config path: %s", *conf)
|
tmpl, err := template.New("config").Parse(defconfig)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
randtk, err := randToken(8)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
"TopicToken": randtk,
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(*conf_path); os.IsNotExist(err) {
|
||||||
|
log.Printf("Writing new config file: %s", *conf_path)
|
||||||
|
file, err := os.Create(*conf_path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = tmpl.Execute(file, data)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error writing config file: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Using config path: %s", *conf_path)
|
||||||
|
log.Printf("Using db path: %s", *db_path)
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
config, err := loadConfig(*conf)
|
config, err := loadConfig(*conf_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load configuration: %v", err)
|
log.Fatalf("Failed to load configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create database
|
// Create database
|
||||||
db, err := NewSQLiteDb("tags.db")
|
db, err := NewSQLiteDb(*db_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create database: %v", err)
|
log.Fatalf("Failed to create database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
notifier := NtfyNotifier{Topic: config.NtfyTopic}
|
notifier := NtfyNotifier{Topic: config.NtfyTopic}
|
||||||
|
log.Printf("Sending notifications to: %s", notifier.Url())
|
||||||
|
|
||||||
c := make(chan RepoUpdate)
|
c := make(chan RepoUpdate)
|
||||||
|
|
||||||
|
|
14
src/random_token.go
Normal file
14
src/random_token.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
func randToken(n int) (string, error) {
|
||||||
|
bytes := make([]byte, n)
|
||||||
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(bytes), nil
|
||||||
|
}
|
|
@ -39,6 +39,7 @@ func loadConfig(file string) (Config, error) {
|
||||||
|
|
||||||
type Notifier interface {
|
type Notifier interface {
|
||||||
Notify(repo Repo, tag string) bool
|
Notify(repo Repo, tag string) bool
|
||||||
|
Url() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type NtfyNotifier struct {
|
type NtfyNotifier struct {
|
||||||
|
@ -46,10 +47,10 @@ type NtfyNotifier struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
||||||
ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
// ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
||||||
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner(), repo.Name(), tag)
|
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner(), repo.Name(), tag)
|
||||||
|
|
||||||
_, err := http.Post(ntfyURL, "text/plain", bytes.NewBuffer([]byte(message)))
|
_, err := http.Post(n.Url(), "text/plain", bytes.NewBuffer([]byte(message)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to send notification: %v", err)
|
log.Printf("Failed to send notification: %v", err)
|
||||||
return false
|
return false
|
||||||
|
@ -57,3 +58,7 @@ func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
||||||
log.Printf("Notification sent: %s", message)
|
log.Printf("Notification sent: %s", message)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n NtfyNotifier) Url() string {
|
||||||
|
return fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue