initial commit
This commit is contained in:
69
lib/runner.go
Normal file
69
lib/runner.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Fetcher interface {
|
||||
Fetch() string
|
||||
}
|
||||
|
||||
func Runner() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("Warning: .env file not found, using environment variables")
|
||||
}
|
||||
|
||||
intervalMinutes, err := strconv.Atoi(os.Getenv("INTERVAL_MINUTES"))
|
||||
if err != nil {
|
||||
intervalMinutes = 5
|
||||
}
|
||||
|
||||
config := Config{
|
||||
WikiBaseURL: os.Getenv("WIKI_BASE_URL"),
|
||||
WikiToken: os.Getenv("WIKI_TOKEN"),
|
||||
RedmineBaseURL: os.Getenv("REDMINE_BASE_URL"),
|
||||
RedmineKey: os.Getenv("REDMINE_KEY"),
|
||||
GiteaToken: os.Getenv("GITEA_TOKEN"),
|
||||
GiteaBaseURL: os.Getenv("GITEA_BASE_URL"),
|
||||
GiteaRepos: strings.Split(os.Getenv("GITEA_REPOS"), ","),
|
||||
Webhook: os.Getenv("DISCORD_WEBHOOK"),
|
||||
Interval: time.Duration(intervalMinutes) * time.Minute,
|
||||
}
|
||||
|
||||
cache := Cache{}
|
||||
cache.Load()
|
||||
discord := DiscordSender{Config: config}
|
||||
|
||||
var fetchers []Fetcher
|
||||
for _, repo := range config.GiteaRepos {
|
||||
if repo == "" {
|
||||
continue
|
||||
}
|
||||
giteaURL := fmt.Sprintf("%s/api/v1/repos/%s/commits", config.GiteaBaseURL, strings.TrimSpace(repo))
|
||||
fetchers = append(fetchers, &GiteaFetcher{URL: giteaURL, Token: config.GiteaToken, Cache: &cache})
|
||||
}
|
||||
|
||||
fetchers = append(fetchers, &WikiFetcher{Config: config, Cache: &cache})
|
||||
fetchers = append(fetchers, &RedmineFetcher{Config: config, Cache: &cache})
|
||||
|
||||
for {
|
||||
fmt.Println("Update")
|
||||
|
||||
for _, f := range fetchers {
|
||||
if msg := f.Fetch(); msg != "" {
|
||||
discord.Send(msg)
|
||||
}
|
||||
}
|
||||
|
||||
cache.Save()
|
||||
|
||||
time.Sleep(config.Interval)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user