Skip to content

Commit

Permalink
Refactor Slack stage 1
Browse files Browse the repository at this point in the history
- Get rid of URL option and use default slack API url.
- Add Token and Channels options
- rename post method to postBody
- move token from query params to header
- render channels param for files.upload from options
- add .idea to gitignore
- upgrade golang.org/x/sys version due to build fail golang/go#49219
  • Loading branch information
XCiber committed Apr 15, 2022
1 parent 21c8672 commit 10ee760
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -102,6 +102,8 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

.idea/

### JetBrains Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

Expand Down
6 changes: 4 additions & 2 deletions cmd/slack.go
Expand Up @@ -11,7 +11,6 @@ import (
)

var slackOptions = vendors.SlackOptions{
URL: envGet("SLACK_URL", "").(string),
Timeout: envGet("SLACK_TIMEOUT", 30).(int),
Insecure: envGet("SLACK_INSECURE", false).(bool),
Message: envGet("SLACK_MESSAGE", "").(string),
Expand All @@ -20,6 +19,8 @@ var slackOptions = vendors.SlackOptions{
Content: envGet("SLACK_CONTENT", "").(string),
Output: envGet("SLACK_OUTPUT", "").(string),
OutputQuery: envGet("SLACK_OUTPUT_QUERY", "").(string),
Token: envGet("SLACK_TOKEN", "").(string),
Channels: strings.Split(envGet("SLACK_CHANNELS", "").(string), ","),
}

func slackNew(stdout *common.Stdout) common.Messenger {
Expand Down Expand Up @@ -55,7 +56,6 @@ func NewSlackCommand() *cobra.Command {
}

flags := slackCmd.PersistentFlags()
flags.StringVar(&slackOptions.URL, "slack-url", slackOptions.URL, "Slack URL")
flags.IntVar(&slackOptions.Timeout, "slack-timeout", slackOptions.Timeout, "Slack timeout")
flags.BoolVar(&slackOptions.Insecure, "slack-insecure", slackOptions.Insecure, "Slack insecure")
flags.StringVar(&slackOptions.Message, "slack-message", slackOptions.Message, "Slack message")
Expand All @@ -64,6 +64,8 @@ func NewSlackCommand() *cobra.Command {
flags.StringVar(&slackOptions.Content, "slack-content", slackOptions.Content, "Slack content")
flags.StringVar(&slackOptions.Output, "slack-output", slackOptions.Output, "Slack output")
flags.StringVar(&slackOptions.OutputQuery, "slack-output-query", slackOptions.OutputQuery, "Slack output query")
flags.StringVar(&slackOptions.Token, "slack-token", slackOptions.Token, "Slack token")
flags.StringSliceVar(&slackOptions.Channels, "slack-channel", slackOptions.Channels, "Slack channels")

slackCmd.AddCommand(&cobra.Command{
Use: "send",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -12,5 +12,5 @@ require (
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -20,5 +20,7 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
44 changes: 37 additions & 7 deletions vendors/slack.go
Expand Up @@ -2,16 +2,24 @@ package vendors

import (
"bytes"
"github.com/devopsext/utils"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strings"
)

"github.com/devopsext/utils"
const baseURL = "https://slack.com/api/"

const (
filesUpload = "files.upload"
chatPostMessage = "chat.postMessage"
)

type SlackOptions struct {
URL string
Token string
Channels []string
Timeout int
Insecure bool
Message string
Expand Down Expand Up @@ -46,7 +54,7 @@ func (s *Slack) getChannel(URL string) string {
return u.Query().Get("channels")
}

func (s *Slack) post(URL, contentType string, body bytes.Buffer, message string) ([]byte, error) {
func (s *Slack) postBody(URL string, query url.Values, contentType string, body bytes.Buffer) ([]byte, error) {

reader := bytes.NewReader(body.Bytes())

Expand All @@ -56,6 +64,8 @@ func (s *Slack) post(URL, contentType string, body bytes.Buffer, message string)
}

req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+s.options.Token)
req.URL.RawQuery = query.Encode()

resp, err := s.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -93,7 +103,13 @@ func (s *Slack) SendCustom(URL, message, title, content string) ([]byte, error)
if err := w.Close(); err != nil {
return nil, err
}
return s.post(URL, w.FormDataContentType(), body, message)

q := url.Values{}
q.Add("channels", strings.Join(s.options.Channels, ","))

URL = s.filesUploadURL()

return s.postBody(URL, q, w.FormDataContentType(), body)
}

func (s *Slack) SendCustomFile(URL, message, fileName, title string, content []byte) ([]byte, error) {
Expand Down Expand Up @@ -124,15 +140,29 @@ func (s *Slack) SendCustomFile(URL, message, fileName, title string, content []b
if err := w.Close(); err != nil {
return nil, err
}
return s.post(URL, w.FormDataContentType(), body, message)

q := url.Values{}
q.Add("channels", strings.Join(s.options.Channels, ","))

URL = s.filesUploadURL()

return s.postBody(URL, q, w.FormDataContentType(), body)
}

func (s *Slack) Send() ([]byte, error) {
return s.SendCustom(s.options.URL, s.options.Message, s.options.Title, s.options.Content)
return s.SendCustom("", s.options.Message, s.options.Title, s.options.Content)
}

func (s *Slack) SendFile() ([]byte, error) {
return s.SendCustomFile(s.options.URL, s.options.Message, s.options.FileName, s.options.Title, []byte(s.options.Content))
return s.SendCustomFile("", s.options.Message, s.options.FileName, s.options.Title, []byte(s.options.Content))
}

func (s *Slack) filesUploadURL() string {
return baseURL + filesUpload
}

func (s *Slack) chatPostMessageURL() string {
return baseURL + chatPostMessage
}

func NewSlack(options SlackOptions) *Slack {
Expand Down

0 comments on commit 10ee760

Please sign in to comment.