fix some bug and standardize the code (#2)

This commit is contained in:
Li4n0
2021-04-22 16:40:22 +08:00
committed by GitHub
parent 960b2ab5ea
commit 72c6150517
10 changed files with 45 additions and 55 deletions
+6 -5
View File
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
"github.com/pkg/errors"
)
var _ Bot = (*DingTalk)(nil)
@@ -37,8 +38,8 @@ func (d *DingTalk) buildPayload(r record.Record) string {
MsgType: "markdown",
Markdown: dingMarkdown{
Title: "New Connection",
Text: "**<font color=\"#e96900\" face=\"Fira Code\" size=\"3\">New Connection</font>**\n" +
formatRecordField(r, "> **<font color=\"#e96900\" face=\"Fira Code\">%s: </font>**<font color=\"#e96900\" face=\"Fira Code\">%v</font>\n"),
Text: "**<font color='#e96900' face='Fira Code' size='3'>New Connection</font>**\n" +
formatRecordField(r, "> **<font color='#e96900' face='Fira Code'>%s: </font>**<font color='#e96900' face='Fira Code'>%v</font>\n"),
},
At: []dingAt{
{
@@ -54,16 +55,16 @@ func (d *DingTalk) buildPayload(r record.Record) string {
}
func (d *DingTalk) notice(r record.Record) error {
resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
return fmt.Errorf("HTTP request: %v", err)
return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read HTTP response body: %v", err)
return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
+4 -3
View File
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
"github.com/pkg/errors"
)
var _ Bot = (*Lark)(nil)
@@ -69,16 +70,16 @@ func (d *Lark) buildPayload(r record.Record) string {
}
func (d *Lark) notice(r record.Record) error {
resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
return fmt.Errorf("HTTP request: %v", err)
return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read HTTP response body: %v", err)
return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
+4 -3
View File
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
"github.com/pkg/errors"
)
var _ Bot = (*Slack)(nil)
@@ -67,16 +68,16 @@ func (d *Slack) buildPayload(r record.Record) string {
}
func (d *Slack) notice(r record.Record) error {
resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
return fmt.Errorf("HTTP request: %v", err)
return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read HTTP response body: %v", err)
return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
+5 -4
View File
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
"github.com/pkg/errors"
)
var _ Bot = (*Weixin)(nil)
@@ -31,7 +32,7 @@ func (w *Weixin) buildPayload(r record.Record) string {
ToUser: "@all",
MsgType: "markdown",
Markdown: weixinMarkdown{
Content: "<font color=\"#e96900\" face=\"Fira Code\" size=3>New Connection</font>\n" +
Content: "<font color='#e96900' face='Fira Code' size=3>New Connection</font>\n" +
formatRecordField(r, `> **<font color="#e96900" face="Fira Code">%s: </font>**<font color="#e96900" face="Fira Code">%v</font>`),
},
}
@@ -43,15 +44,15 @@ func (w *Weixin) buildPayload(r record.Record) string {
}
func (w *Weixin) notice(r record.Record) error {
resp, err := http.DefaultClient.Post(w.URL, "application/json", strings.NewReader(w.buildPayload(r)))
resp, err := http.Post(w.URL, "application/json", strings.NewReader(w.buildPayload(r)))
if err != nil {
return fmt.Errorf("HTTP request: %v", err)
return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read HTTP response body: %v", err)
return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}