styx/plugins/pastebin.go
Christopher Talib b2da64a9d7 Enh/modular arch
2020-02-25 10:05:31 +01:00

78 lines
1.5 KiB
Go

package plugins
import (
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gitlab.dcso.lolcat/LABS/styx/models"
)
// PastebinPlugin defines the general PastebinPlugin structure.
type PastebinPlugin struct {
StopChan chan bool
StoppedChan chan bool
Running bool
}
// Initialize initialises the certstream configuration.
func (p *PastebinPlugin) Initialize() bool {
if !viper.GetBool("pastebin.activated") {
return false
}
logrus.Info("pastebin plugin is activated")
return true
}
// Run runs the Pastebin plugin.
func (p *PastebinPlugin) Run(wg *sync.WaitGroup) {
if !p.Running {
p.StoppedChan = make(chan bool)
wg.Add(1)
go p.doRun()
p.Running = true
}
}
// Stop stops the Pastebin plugin.
func (p *PastebinPlugin) Stop(wg *sync.WaitGroup) {
if p.Running {
p.StoppedChan = make(chan bool)
close(p.StopChan)
<-p.StopChan
wg.Done()
p.Running = false
}
}
func (p *PastebinPlugin) doRun() {
for {
select {
default:
pastes, err := models.QueryPastes()
if err != nil {
logrus.Error(err)
}
for _, p := range pastes {
paste, err := models.FetchPaste(p)
if err != nil {
logrus.Error("cannot fetch paste", err)
}
fp := models.FullPaste{
Meta: p,
Full: paste,
}
res := models.BuildPasteNode(&fp)
// if elastic {
// e.StorePaste(fp)
// }
models.SavePaste("paste_formatted.json", res)
time.Sleep(1 * time.Second)
}
time.Sleep(3 * time.Second)
}
}
}