gitea-sonarqube-bot/internal/settings/webhook.go
justusbunsi e01096a7fe
Eliminate viper references from token and webhook
Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
2021-10-17 15:57:45 +02:00

37 lines
741 B
Go

package settings
import (
"fmt"
"io/ioutil"
)
type webhook struct {
Secret string
secretFile string
}
func (w *webhook) lookupSecret(errCallback func(string)) {
if w.secretFile == "" {
return
}
content, err := ioutil.ReadFile(w.secretFile)
if err != nil {
errCallback(fmt.Sprintf("Cannot read '%s' or it is no regular file: %s", w.secretFile, err.Error()))
return
}
w.Secret = string(content)
}
func NewWebhook(extractor func(string) string, confContainer string, errCallback func(string)) *webhook {
w := &webhook{
Secret: extractor(fmt.Sprintf("%s.webhook.secret", confContainer)),
secretFile: extractor(fmt.Sprintf("%s.webhook.secretFile", confContainer)),
}
w.lookupSecret(errCallback)
return w
}