mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-17 15:40:12 +01:00
feat(notifications): add json template
This commit is contained in:
parent
14b235a542
commit
be2302f059
4 changed files with 92 additions and 14 deletions
|
|
@ -35,5 +35,6 @@ var commonTemplates = map[string]string{
|
||||||
no containers matched filter
|
no containers matched filter
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}`,
|
{{- end -}}`,
|
||||||
}
|
|
||||||
|
|
||||||
|
`json.v1`: `{{ . | ToJSON }}`,
|
||||||
|
}
|
||||||
|
|
|
||||||
70
pkg/notifications/json.go
Normal file
70
pkg/notifications/json.go
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
t "github.com/containrrr/watchtower/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JSONMap = map[string]interface{}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler
|
||||||
|
func (d Data) MarshalJSON() ([]byte, error) {
|
||||||
|
var entries = make([]JSONMap, len(d.Entries))
|
||||||
|
for i, entry := range d.Entries {
|
||||||
|
entries[i] = JSONMap{
|
||||||
|
`level`: entry.Level,
|
||||||
|
`message`: entry.Message,
|
||||||
|
`data`: entry.Data,
|
||||||
|
`time`: entry.Time,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var report JSONMap
|
||||||
|
if d.Report != nil {
|
||||||
|
report = JSONMap{
|
||||||
|
`scanned`: marshalReports(d.Report.Scanned()),
|
||||||
|
`updated`: marshalReports(d.Report.Updated()),
|
||||||
|
`failed`: marshalReports(d.Report.Failed()),
|
||||||
|
`skipped`: marshalReports(d.Report.Skipped()),
|
||||||
|
`stale`: marshalReports(d.Report.Stale()),
|
||||||
|
`fresh`: marshalReports(d.Report.Fresh()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(JSONMap{
|
||||||
|
`report`: report,
|
||||||
|
`title`: d.Title,
|
||||||
|
`host`: d.Host,
|
||||||
|
`entries`: entries,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func marshalReports(reports []t.ContainerReport) []JSONMap {
|
||||||
|
jsonReports := make([]JSONMap, len(reports))
|
||||||
|
for i, report := range reports {
|
||||||
|
jsonReports[i] = JSONMap{
|
||||||
|
`id`: report.ID(),
|
||||||
|
`name`: report.Name(),
|
||||||
|
`currentImageId`: report.CurrentImageID(),
|
||||||
|
`latestImageId`: report.LatestImageID(),
|
||||||
|
`imageName`: report.ImageName(),
|
||||||
|
`state`: report.State(),
|
||||||
|
}
|
||||||
|
if errorMessage := report.Error(); errorMessage != "" {
|
||||||
|
jsonReports[i][`error`] = errorMessage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jsonReports
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ json.Marshaler = &Data{}
|
||||||
|
|
||||||
|
func toJSON(v interface{}) string {
|
||||||
|
if bytes, err := json.MarshalIndent(v, "", " "); err != nil {
|
||||||
|
LocalLog.Errorf("failed to marshal JSON in notification template: %v", err)
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return string(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
19
pkg/notifications/model.go
Normal file
19
pkg/notifications/model.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
t "github.com/containrrr/watchtower/pkg/types"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StaticData is the part of the notification template data model set upon initialization
|
||||||
|
type StaticData struct {
|
||||||
|
Title string
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data is the notification template data model
|
||||||
|
type Data struct {
|
||||||
|
StaticData
|
||||||
|
Entries []*log.Entry
|
||||||
|
Report t.Report
|
||||||
|
}
|
||||||
|
|
@ -210,6 +210,7 @@ func getShoutrrrTemplate(tplString string, legacy bool) (tpl *template.Template,
|
||||||
funcs := template.FuncMap{
|
funcs := template.FuncMap{
|
||||||
"ToUpper": strings.ToUpper,
|
"ToUpper": strings.ToUpper,
|
||||||
"ToLower": strings.ToLower,
|
"ToLower": strings.ToLower,
|
||||||
|
"ToJSON": toJSON,
|
||||||
"Title": cases.Title(language.AmericanEnglish).String,
|
"Title": cases.Title(language.AmericanEnglish).String,
|
||||||
}
|
}
|
||||||
tplBase := template.New("").Funcs(funcs)
|
tplBase := template.New("").Funcs(funcs)
|
||||||
|
|
@ -240,16 +241,3 @@ func getShoutrrrTemplate(tplString string, legacy bool) (tpl *template.Template,
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticData is the part of the notification template data model set upon initialization
|
|
||||||
type StaticData struct {
|
|
||||||
Title string
|
|
||||||
Host string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Data is the notification template data model
|
|
||||||
type Data struct {
|
|
||||||
StaticData
|
|
||||||
Entries []*log.Entry
|
|
||||||
Report t.Report
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue