noop file and type refactor

This commit is contained in:
AJ ONeal 2019-06-24 20:00:01 -06:00
parent 560a4f0c57
commit 94c0dfa2a0
3 changed files with 91 additions and 89 deletions

View File

@ -69,7 +69,7 @@ func main() {
done := make(chan struct{}, 1) done := make(chan struct{}, 1)
allWebhooks := make(map[string]watchdog.ConfigWebhook) allWebhooks := make(map[string]watchdog.Webhook)
for i := range config.Webhooks { for i := range config.Webhooks {
h := config.Webhooks[i] h := config.Webhooks[i]

2
go.sum
View File

@ -1,4 +1,2 @@
git.rootprojects.org/root/go-gitver v1.1.0 h1:ANQUnUXYgbDR+WaMcI+PQQjLnxlCbAZCD/zivkrf8fY=
git.rootprojects.org/root/go-gitver v1.1.0/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI=
git.rootprojects.org/root/go-gitver v1.1.1 h1:5b0lxnTYnft5hqpln0XCrJaGPH0SKzhPaazVAvAlZ8I= git.rootprojects.org/root/go-gitver v1.1.1 h1:5b0lxnTYnft5hqpln0XCrJaGPH0SKzhPaazVAvAlZ8I=
git.rootprojects.org/root/go-gitver v1.1.1/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI= git.rootprojects.org/root/go-gitver v1.1.1/go.mod h1:Rj1v3TBhvdaSphFEqMynUYwAz/4f+wY/+syBTvRrmlI=

View File

@ -20,7 +20,7 @@ type Dog struct {
Keywords string Keywords string
Recover string Recover string
Webhooks []string Webhooks []string
AllWebhooks map[string]ConfigWebhook AllWebhooks map[string]Webhook
Logger chan string Logger chan string
error error error error
failures int failures int
@ -172,6 +172,11 @@ func (d *Dog) notify(hardFail bool) {
continue continue
} }
d.notifyOne(h, hardFail)
}
}
func (d *Dog) notifyOne(h Webhook, hardFail bool) {
// TODO do this in main on config init // TODO do this in main on config init
if "" == h.Method { if "" == h.Method {
h.Method = "POST" h.Method = "POST"
@ -195,7 +200,7 @@ func (d *Dog) notify(hardFail bool) {
bodyBuf, err := json.Marshal(h.JSON) bodyBuf, err := json.Marshal(h.JSON)
if nil != err { if nil != err {
d.Logger <- fmt.Sprintf("[Notify] JSON Marshal Error for '%s': %s", h.Name, err) d.Logger <- fmt.Sprintf("[Notify] JSON Marshal Error for '%s': %s", h.Name, err)
continue return
} }
// `{{` should be left alone // `{{` should be left alone
body = strings.NewReader(strings.Replace(string(bodyBuf), "{{ .Name }}", d.Name, -1)) body = strings.NewReader(strings.Replace(string(bodyBuf), "{{ .Name }}", d.Name, -1))
@ -205,7 +210,7 @@ func (d *Dog) notify(hardFail bool) {
req, err := http.NewRequest(h.Method, h.URL, body) req, err := http.NewRequest(h.Method, h.URL, body)
if nil != err { if nil != err {
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Network Error for '%s': %s", h.Name, err) d.Logger <- fmt.Sprintf("[Notify] HTTP Client Network Error for '%s': %s", h.Name, err)
continue return
} }
if 0 != len(h.Form) { if 0 != len(h.Form) {
@ -234,12 +239,12 @@ func (d *Dog) notify(hardFail bool) {
resp, err := client.Do(req) resp, err := client.Do(req)
if nil != err { if nil != err {
d.Logger <- fmt.Sprintf("[Notify] HTTP Client Error for '%s': %s", h.Name, err) d.Logger <- fmt.Sprintf("[Notify] HTTP Client Error for '%s': %s", h.Name, err)
continue return
} }
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
d.Logger <- fmt.Sprintf("[Notify] Response Error for '%s': %s", h.Name, resp.Status) d.Logger <- fmt.Sprintf("[Notify] Response Error for '%s': %s", h.Name, resp.Status)
continue return
} }
// TODO json vs xml vs txt // TODO json vs xml vs txt
@ -249,17 +254,16 @@ func (d *Dog) notify(hardFail bool) {
err = decoder.Decode(&data) err = decoder.Decode(&data)
if err != nil { if err != nil {
d.Logger <- fmt.Sprintf("[Notify] Response Body Error for '%s': %s", h.Name, resp.Status) d.Logger <- fmt.Sprintf("[Notify] Response Body Error for '%s': %s", h.Name, resp.Status)
continue return
} }
// TODO some sort of way to determine if data is successful (keywords) // TODO some sort of way to determine if data is successful (keywords)
d.Logger <- fmt.Sprintf("[Notify] Success? %#v", data) d.Logger <- fmt.Sprintf("[Notify] Success? %#v", data)
}
} }
type Config struct { type Config struct {
Watches []ConfigWatch `json:"watches"` Watches []ConfigWatch `json:"watches"`
Webhooks []ConfigWebhook `json:"webhooks"` Webhooks []Webhook `json:"webhooks"`
} }
type ConfigWatch struct { type ConfigWatch struct {
@ -270,7 +274,7 @@ type ConfigWatch struct {
RecoverScript string `json:"recover_script"` RecoverScript string `json:"recover_script"`
} }
type ConfigWebhook struct { type Webhook struct {
Name string `json:"name"` Name string `json:"name"`
Method string `json:"method"` Method string `json:"method"`
URL string `json:"url"` URL string `json:"url"`