From b9d3b287e8d7b02fbc32c018a0a72a38adece4c2 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 7 Aug 2021 13:02:50 -0400 Subject: [PATCH] track config file changes --- .drone.yml | 10 ++++++++-- go.mod | 1 + go.sum | 3 +++ main.go | 21 ++++++++++++--------- vtuber.go | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/.drone.yml b/.drone.yml index 9dbd4fa..a8eef4f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -7,14 +7,20 @@ steps: commands: - make +- name: create release tar + image: golang + commands: + - tar -czf release-$DRONE_TAG.tar.gz init/simpbot.service simpbot + when: + event: tag + - name: release image: plugins/gitea-release settings: api_key: from_secret: gitea_token files: - - simpbot - - config.yml.sample + - release-$DRONE_TAG.tar.gz base_url: https://git.saintnet.tech when: event: tag diff --git a/go.mod b/go.mod index 7919258..eda437e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module simpbot go 1.15 require ( + github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/spf13/viper v1.7.1 golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 // indirect diff --git a/go.sum b/go.sum index 9fcf4af..4048f32 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8 github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -267,6 +269,7 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c= diff --git a/main.go b/main.go index 444e484..707b466 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,13 @@ package main import ( "fmt" "log" + "os" + "os/signal" "strings" + "syscall" "time" + "github.com/fsnotify/fsnotify" "github.com/spf13/viper" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" @@ -100,15 +104,14 @@ func main() { } } }) - var vtubersRaw []VtuberConfig - err = viper.UnmarshalKey("vtubers", &vtubersRaw) - if err != nil { - panic(err) - } - for _, vt := range vtubersRaw { - log.Printf("adding vtuber %v", vt) - vtubers = append(vtubers, NewVtuber(vt.Name, vt.ChannelID, vt.LiveMsg)) - } + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGUSR1) + vtubers = LoadVtubers() + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + fmt.Println("Config file changed,reloading vtubers:", e.Name) + vtubers = LoadVtubers() + }) go func() { for { diff --git a/vtuber.go b/vtuber.go index 4300ab2..b2b0f30 100644 --- a/vtuber.go +++ b/vtuber.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "log" "net/http" + + "github.com/spf13/viper" ) type VtuberConfig struct { @@ -30,6 +32,20 @@ func NewVtuber(name, channelID, liveMsg string) *Vtuber { } } +func LoadVtubers() []*Vtuber { + var vtubersRaw []VtuberConfig + var vtubers []*Vtuber + err := viper.UnmarshalKey("vtubers", &vtubersRaw) + if err != nil { + panic(err) + } + for _, vt := range vtubersRaw { + log.Printf("adding vtuber %v", vt) + vtubers = append(vtubers, NewVtuber(vt.Name, vt.ChannelID, vt.LiveMsg)) + } + return vtubers +} + func (v *Vtuber) IsLive() bool { return v.CurrentStream != "" }