-
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 #4 from Clarilab/v2
refactor!: remove dependencies and simplify API
- Loading branch information
Showing
9 changed files
with
174 additions
and
766 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
.vscode | ||
.idea | ||
.DS_Store |
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,57 +1,47 @@ | ||
# slacklogger | ||
|
||
This client uses [slack-to-webhook](https://github.com/ashwanthkumar/slack-go-webhook). | ||
This client is used to send messages using a slack webhook url. | ||
|
||
## Installation | ||
|
||
```shell | ||
go get github.com/Clarilab/slacklogger | ||
go get github.com/Clarilab/slacklogger/v2 | ||
``` | ||
|
||
## Importing | ||
```go | ||
import "github.com/Clarilab/slacklogger" | ||
``` | ||
|
||
## Features | ||
```go | ||
// SlackLogger holds all callable methods | ||
type SlackLogger interface { | ||
// Log prints a simple text to slack | ||
Log(text string) | ||
|
||
// LogWithName prints a formated text, containing the name, to slack | ||
LogWithName(name, text string) | ||
import "github.com/Clarilab/slacklogger/v2" | ||
``` | ||
|
||
// LogWithChannel prints a text to a given slack channel | ||
LogWithChannel(channel, text string) | ||
## Examples | ||
|
||
// LogWithName prints a simple text to slack, while using the given url as webhookURL | ||
LogWithURL(url, text string) | ||
### Logging with an instanced logger | ||
|
||
// LogWithURLAndName prints a formated text, containing the name, to slack, while using the given url as webhookURL | ||
LogWithURLAndName(url, name, text string) | ||
```go | ||
webhookURL := "https://hooks.slack.com/..." | ||
environment := "development" | ||
isDebug := false | ||
|
||
// LogWithName prints a simple text to a given slack channel, while using the given url as webhookURL | ||
LogWithChannelAndURL(channel, url, text string) | ||
slacker := slacklogger.NewSlackLogger(webhookURL, environment, isDebug) | ||
|
||
// LogWithURLAndName prints a formated text, containing the name, to a given slack channel | ||
LogWithChannelAndName(channel, name, text string) | ||
slacker.Log("Something weird") | ||
|
||
// LogWithURLAndName prints a formated text, containing the name, to a given slack channel, while using the given url as webhookURL | ||
LogWithChannelAndURLAndName(channel, url, name, text string) | ||
} | ||
// this will result in: env=development Something weird | ||
``` | ||
|
||
## Example | ||
### Logging without an instanced logger | ||
|
||
```go | ||
webhookURL := "https://hooks.slack.com/..." | ||
channel := "Nicenstein" | ||
username := "Botty McBootface" | ||
environment := "prod" | ||
isProd := true | ||
onlyProd := true | ||
environment := "" | ||
isDebug := false | ||
message := "Hello World!" | ||
|
||
slacker := slacklogger.NewSlackLogger(webhookURL, channel, username, environment, isProd, onlyProd) | ||
|
||
slacker.Log("Something weird") | ||
slacklogger.LogWithURL(message, webhookURL, environment, isDebug) | ||
|
||
// this will result in: Hello World! | ||
``` | ||
|
||
If isDebug is set to true, it will print to stdout instead. |
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,86 @@ | ||
package slacklogger | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type Field struct { | ||
Title string `json:"title"` | ||
Value string `json:"value"` | ||
Short bool `json:"short"` | ||
} | ||
|
||
type Action struct { | ||
Type string `json:"type"` | ||
Text string `json:"text"` | ||
Url string `json:"url"` | ||
Style string `json:"style"` | ||
} | ||
|
||
type Attachment struct { | ||
Fallback *string `json:"fallback"` | ||
Color *string `json:"color"` | ||
PreText *string `json:"pretext"` | ||
AuthorName *string `json:"author_name"` | ||
AuthorLink *string `json:"author_link"` | ||
AuthorIcon *string `json:"author_icon"` | ||
Title *string `json:"title"` | ||
TitleLink *string `json:"title_link"` | ||
Text *string `json:"text"` | ||
ImageUrl *string `json:"image_url"` | ||
Fields []*Field `json:"fields"` | ||
Footer *string `json:"footer"` | ||
FooterIcon *string `json:"footer_icon"` | ||
Timestamp *int64 `json:"ts"` | ||
MarkdownIn *[]string `json:"mrkdwn_in"` | ||
Actions []*Action `json:"actions"` | ||
CallbackID *string `json:"callback_id"` | ||
ThumbnailUrl *string `json:"thumb_url"` | ||
} | ||
|
||
type Payload struct { | ||
Parse string `json:"parse,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
IconUrl string `json:"icon_url,omitempty"` | ||
IconEmoji string `json:"icon_emoji,omitempty"` | ||
Channel string `json:"channel,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
LinkNames string `json:"link_names,omitempty"` | ||
Attachments []Attachment `json:"attachments,omitempty"` | ||
UnfurlLinks bool `json:"unfurl_links,omitempty"` | ||
UnfurlMedia bool `json:"unfurl_media,omitempty"` | ||
Markdown bool `json:"mrkdwn,omitempty"` | ||
} | ||
|
||
func (attachment *Attachment) AddField(field Field) *Attachment { | ||
attachment.Fields = append(attachment.Fields, &field) | ||
return attachment | ||
} | ||
|
||
func (attachment *Attachment) AddAction(action Action) *Attachment { | ||
attachment.Actions = append(attachment.Actions, &action) | ||
return attachment | ||
} | ||
|
||
var ErrFailedToMarshalJSON = fmt.Errorf("failed to marshal payload") | ||
|
||
func Send(webhookUrl string, payload Payload) error { | ||
jsonBytes, err := json.Marshal(&payload) | ||
if err != nil { | ||
return ErrFailedToMarshalJSON | ||
} | ||
|
||
resp, err := http.Post(webhookUrl, "application/json", bytes.NewBuffer(jsonBytes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode >= 400 { | ||
return error(fmt.Errorf("Error sending msg. Status: %v", resp.Status)) | ||
} | ||
|
||
return 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 |
---|---|---|
@@ -1,12 +1,3 @@ | ||
module github.com/Clarilab/slacklogger | ||
module github.com/Clarilab/slacklogger/v2 | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/ashwanthkumar/slack-go-webhook v0.0.0-20200209025033-430dd4e66960 | ||
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 // indirect | ||
github.com/parnurzeal/gorequest v0.2.16 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/smartystreets/goconvey v1.8.1 // indirect | ||
moul.io/http2curl v1.0.0 // indirect | ||
) | ||
go 1.20 |
Oops, something went wrong.