-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
263 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
package model | ||
|
||
import "time" | ||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/hako/durafmt" | ||
) | ||
|
||
// Journal holds ftpgrab entries and status | ||
type Journal struct { | ||
Entries []Entry | ||
Entries []Entry `json:"entries,omitempty"` | ||
Count struct { | ||
Success int | ||
Error int | ||
Skip int | ||
} | ||
Status string | ||
Duration time.Duration | ||
Success int `json:"success,omitempty"` | ||
Error int `json:"error,omitempty"` | ||
Skip int `json:"skip,omitempty"` | ||
} `json:"count,omitempty"` | ||
Status string `json:"status,omitempty"` | ||
Duration time.Duration `json:"duration,omitempty"` | ||
} | ||
|
||
// Entry represents a journal entry | ||
type Entry struct { | ||
File string | ||
StatusType string | ||
StatusText string | ||
File string `json:"file,omitempty"` | ||
StatusType string `json:"status_type,omitempty"` | ||
StatusText string `json:"status_text,omitempty"` | ||
} | ||
|
||
// EntryStatus represents entry status | ||
type EntryStatus string | ||
|
||
func (j Journal) MarshalJSON() ([]byte, error) { | ||
type Alias Journal | ||
return json.Marshal(&struct { | ||
Alias | ||
Duration string `json:"duration,omitempty"` | ||
}{ | ||
Alias: (Alias)(j), | ||
Duration: durafmt.ParseShort(j.Duration).String(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
// Notif holds data necessary for notification configuration | ||
type Notif struct { | ||
Mail Mail `yaml:"mail,omitempty"` | ||
Webhook Webhook `yaml:"webhook,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
// Webhook holds webhook notification configuration details | ||
type Webhook struct { | ||
Enable bool `yaml:"enable,omitempty"` | ||
Endpoint string `yaml:"endpoint,omitempty"` | ||
Method string `yaml:"method,omitempty"` | ||
Headers map[string]string `yaml:"headers,omitempty"` | ||
Timeout int `yaml:"timeout,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package notif | ||
|
||
import ( | ||
"github.com/ftpgrab/ftpgrab/internal/journal" | ||
"github.com/ftpgrab/ftpgrab/internal/model" | ||
"github.com/ftpgrab/ftpgrab/internal/notif/mail" | ||
"github.com/ftpgrab/ftpgrab/internal/notif/notifier" | ||
"github.com/ftpgrab/ftpgrab/internal/notif/webhook" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Client represents an active webhook notification object | ||
type Client struct { | ||
cfg model.Notif | ||
app model.App | ||
cmn model.Common | ||
notifiers []notifier.Notifier | ||
} | ||
|
||
// New creates a new notification instance | ||
func New(config model.Notif, app model.App, cmn model.Common) (*Client, error) { | ||
var c = &Client{ | ||
cfg: config, | ||
app: app, | ||
cmn: cmn, | ||
notifiers: []notifier.Notifier{}, | ||
} | ||
|
||
// Add notifiers | ||
if config.Mail.Enable { | ||
c.notifiers = append(c.notifiers, mail.New(config.Mail, app, cmn)) | ||
} | ||
if config.Webhook.Enable { | ||
c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app, cmn)) | ||
} | ||
|
||
log.Debug().Msgf("%d notifier(s) created", len(c.notifiers)) | ||
return c, nil | ||
} | ||
|
||
// Send creates and sends notifications to notifiers | ||
func (c *Client) Send(jnl journal.Client) { | ||
for _, n := range c.notifiers { | ||
log.Debug().Msgf("Sending %s notification...", n.Name()) | ||
if err := n.Send(jnl); err != nil { | ||
log.Error().Err(err).Msgf("%s notification failed", n.Name()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package notifier | ||
|
||
import "github.com/ftpgrab/ftpgrab/internal/journal" | ||
|
||
// Handler is a notifier interface | ||
type Handler interface { | ||
Name() string | ||
Send(jnl journal.Client) error | ||
} | ||
|
||
// Notifier represents an active notifier object | ||
type Notifier struct { | ||
Handler | ||
} |
Oops, something went wrong.