-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from PSNAppz/improve_config
Improve configuration
- Loading branch information
Showing
14 changed files
with
375 additions
and
304 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package publisher | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type FilePublisher struct { | ||
file *os.File | ||
} | ||
|
||
func (f FilePublisher) Publish(message string) error { | ||
lineBytes := []byte(fmt.Sprintf("%s\n", message)) | ||
_, err := f.file.Write(lineBytes) | ||
return err | ||
} | ||
|
||
func (f FilePublisher) Type() string { | ||
return "file" | ||
} | ||
|
||
func NewFilePublisher(settings map[string]interface{}) (*FilePublisher, error) { | ||
name, ok := settings["name"] | ||
if !ok { | ||
return nil, fmt.Errorf("file must have a name") | ||
} | ||
|
||
nameStr, ok := name.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("file name must be a string, found: %+v", name) | ||
} | ||
|
||
targetDir, ok := settings["directory"] | ||
if !ok { | ||
return nil, fmt.Errorf("target directory must be specified") | ||
} | ||
|
||
targetDirStr, ok := targetDir.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("directory must be a string found %+v", targetDir) | ||
} | ||
|
||
absDir, err := filepath.Abs(targetDirStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
targetFilePath := filepath.Join(absDir, nameStr) | ||
file, err := os.OpenFile(targetFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FilePublisher{file: file}, nil | ||
} |
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,71 @@ | ||
package publisher | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Publisher is an interface that publishes messages to a data source | ||
type Publisher interface { | ||
Type() string | ||
Publish(message string) error | ||
} | ||
|
||
func CreatePublishers(pluginSettings map[string]interface{}) ([]Publisher, error) { | ||
publisherSettings, err := parsePublisherSettings(pluginSettings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
publishers := []Publisher{} | ||
for _, setting := range publisherSettings { | ||
publisher, err := NewPublisher(setting) | ||
if err != nil { | ||
return nil, err | ||
|
||
} | ||
publishers = append(publishers, publisher) | ||
} | ||
return publishers, nil | ||
} | ||
|
||
func NewPublisher(setting map[string]interface{}) (Publisher, error) { | ||
publisherType, ok := setting["type"] | ||
if !ok { | ||
return nil, fmt.Errorf("publish setting must specify a type, found %v", publisherType) | ||
} | ||
|
||
var publisher Publisher | ||
var err error | ||
|
||
switch publisherType { | ||
case "slack": | ||
publisher, err = NewSlackPublisher(setting) | ||
case "file": | ||
publisher, err = NewFilePublisher(setting) | ||
} | ||
|
||
return publisher, err | ||
} | ||
|
||
func parsePublisherSettings(settings map[string]interface{}) ([]map[string]interface{}, error) { | ||
publisherList, ok := settings["publishers"] | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
publisherInterfaceList, ok := publisherList.([]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("publisher list is incorrectly configured, found %+v", publisherList) | ||
} | ||
|
||
publisherSettings := []map[string]interface{}{} | ||
for _, publisherInterface := range publisherInterfaceList { | ||
publisherSetting, ok := publisherInterface.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("publisher settting is incorrectly configured, found %+v", publisherSetting) | ||
} | ||
publisherSettings = append(publisherSettings, publisherSetting) | ||
} | ||
|
||
return publisherSettings, nil | ||
} |
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,45 @@ | ||
package publisher | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type SlackPublisher struct { | ||
api *slack.Client | ||
channelID string | ||
} | ||
|
||
func (s SlackPublisher) Type() string { | ||
return "slack" | ||
} | ||
|
||
func (s SlackPublisher) Publish(message string) error { | ||
_, _, err := s.api.PostMessage(s.channelID, slack.MsgOptionText(message, false)) | ||
return err | ||
} | ||
|
||
func NewSlackPublisher(settings map[string]interface{}) (*SlackPublisher, error) { | ||
channelID, ok := settings["channelID"] | ||
if !ok { | ||
return nil, fmt.Errorf("channel ID is required to send Slack notification") | ||
} | ||
|
||
channelIDStr, ok := channelID.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("channel ID must be a string, found: %+v", channelID) | ||
} | ||
|
||
token, ok := settings["token"] | ||
if !ok { | ||
return nil, fmt.Errorf("API token required to send Slack notifications") | ||
} | ||
|
||
tokenStr, ok := token.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("API token must be a string, found: %+v", tokenStr) | ||
} | ||
|
||
return &SlackPublisher{api: slack.New(tokenStr, slack.OptionDebug(true)), channelID: channelIDStr}, nil | ||
} |
Oops, something went wrong.