bell-system/main.go
2024-02-08 10:54:48 -04:00

74 lines
1.6 KiB
Go

package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"gopkg.in/yaml.v2"
)
type config struct {
Horn string `yaml:"horn"`
Times []string `yaml:"times"`
}
func (config *config) getConf() *config {
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
yamlFile, err := os.ReadFile("config.yaml")
if err != nil {
logger.Error("yamlFile.Get", logger.Args(":", err))
}
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
logger.Error("Unmarshal", logger.Args(":", err))
}
logger.Info("Config loaded", logger.Args("Times", config.Times, "API URL", config.Horn))
return config
}
func main() {
pterm.Print("\n\n")
s, _ := pterm.DefaultBigText.WithLetters(
putils.LettersFromStringWithStyle("Bell ", pterm.FgRed.ToStyle()),
putils.LettersFromStringWithStyle("System", pterm.FgBlue.ToStyle())).
Srender()
pterm.DefaultCenter.Println(s)
pterm.DefaultCenter.Println("By Cody (" + pterm.LightBlue("https://oki.cx/tree") + ")")
pterm.DefaultCenter.Println("v1.0")
pterm.Print("\n")
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
var config config
config.getConf()
logger.Info("Bell system activated!")
for {
ctime := time.Now().Format("15:04:05")
for _, time := range config.Times {
if time == ctime {
resp, err := http.Get(config.Horn)
if err != nil {
logger.Error(fmt.Sprintf("%s", err))
}
logger.Info(fmt.Sprintf("Sending HTTP request to %s", config.Horn), logger.Args("Reason", "Config File", "Time", ctime, "Status Code", resp.Status))
}
}
time.Sleep(time.Second)
}
}