diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..7b76cf4 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master"] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.21.0 + + - name: Install golangci-lint + run: | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3 + + - name: Lint + run: golangci-lint run ./... diff --git a/bot_test.go b/bot_test.go index 61df236..ecd2b69 100644 --- a/bot_test.go +++ b/bot_test.go @@ -26,28 +26,32 @@ func TestLogout(t *testing.T) { } bot.MessageHandler = func(msg *Message) { if msg.IsText() && msg.Content == "logout" { - bot.Logout() + if err := bot.Logout(); err != nil { + t.Error(err) + } } } if err := bot.Login(); err != nil { t.Error(err) return } - bot.Block() + _ = bot.Block() } func TestMessageHandle(t *testing.T) { bot := DefaultBot(Desktop) bot.MessageHandler = func(msg *Message) { if msg.IsText() && msg.Content == "ping" { - msg.ReplyText("pong") + if _, err := msg.ReplyText("pong"); err != nil { + t.Error(err) + } } } if err := bot.Login(); err != nil { t.Error(err) return } - bot.Block() + _ = bot.Block() } func TestFriends(t *testing.T) { @@ -106,9 +110,15 @@ func TestPinUser(t *testing.T) { } if friends.Count() > 0 { f := friends.First() - f.Pin() + if err = f.Pin(); err != nil { + t.Error(err) + return + } time.Sleep(time.Second * 5) - f.UnPin() + if err = f.UnPin(); err != nil { + t.Error(err) + return + } } } @@ -125,7 +135,7 @@ func TestSender(t *testing.T) { t.Error(err) return } - bot.Block() + _ = bot.Block() } // TestGetUUID diff --git a/client.go b/client.go index 60bb30c..d6228f8 100644 --- a/client.go +++ b/client.go @@ -494,7 +494,7 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt if err != nil { return nil, err } - if _, err = file.Seek(io.SeekStart, 0); err != nil { + if _, err = file.Seek(0, io.SeekStart); err != nil { return nil, err } @@ -980,6 +980,7 @@ func (c *Client) WebWxRevokeMsg(ctx context.Context, msg *SentMessage, request * } // 校验上传文件 +// nolint:unused func (c *Client) webWxCheckUpload(stat os.FileInfo, request *BaseRequest, fileMd5, fromUserName, toUserName string) (*http.Response, error) { path, err := url.Parse(c.Domain.BaseHost() + webwxcheckupload) if err != nil { diff --git a/cookiejar.go b/cookiejar.go index 434e1e7..cb69b53 100644 --- a/cookiejar.go +++ b/cookiejar.go @@ -56,7 +56,7 @@ type entry struct { // seqNum is a sequence number so that Jar returns cookies in a // deterministic order, even for cookies that have equal Path length and // equal Creation time. This simplifies testing. - seqNum uint64 + seqNum uint64 // nolint:unused } // CookieGroup is a group of cookies diff --git a/global.go b/global.go index 3a2a89e..c671d7f 100644 --- a/global.go +++ b/global.go @@ -113,11 +113,11 @@ const ( // 分块上传时每次上传的文件的大小 chunkSize int64 = (1 << 20) / 2 // 0.5m // 需要检测的文件大小 - needCheckSize int64 = 25 << 20 // 20m + needCheckSize int64 = 25 << 20 // nolint:unused // 最大文件上传大小 - maxFileUploadSize int64 = 50 << 20 // 50m + maxFileUploadSize int64 = 50 << 20 // nolint:unused // 最大图片上传大小 - maxImageUploadSize int64 = 20 << 20 // 20m + maxImageUploadSize int64 = 20 << 20 // nolint:unused ) const TimeFormat = "Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)" diff --git a/message.go b/message.go index 4c31f80..2fbf2f5 100644 --- a/message.go +++ b/message.go @@ -396,7 +396,7 @@ func (m *Message) Card() (*Card, error) { return nil, errors.New("card message required") } var card Card - err := xml.Unmarshal(stringToByte(m.Content), &card) + err := xml.Unmarshal([]byte(m.Content), &card) return &card, err } @@ -406,7 +406,7 @@ func (m *Message) FriendAddMessageContent() (*FriendAddMessage, error) { return nil, errors.New("friend add message required") } var f FriendAddMessage - err := xml.Unmarshal(stringToByte(m.Content), &f) + err := xml.Unmarshal([]byte(m.Content), &f) return &f, err } @@ -416,7 +416,7 @@ func (m *Message) RevokeMsg() (*RevokeMsg, error) { return nil, errors.New("recalled message required") } var r RevokeMsg - err := xml.Unmarshal(stringToByte(m.Content), &r) + err := xml.Unmarshal([]byte(m.Content), &r) return &r, err } @@ -463,7 +463,7 @@ func (m *Message) MediaData() (*AppMessageData, error) { return nil, errors.New("media message required") } var data AppMessageData - if err := xml.Unmarshal(stringToByte(m.Content), &data); err != nil { + if err := xml.Unmarshal([]byte(m.Content), &data); err != nil { return nil, err } return &data, nil diff --git a/parser.go b/parser.go index 31718db..e3b7407 100644 --- a/parser.go +++ b/parser.go @@ -7,11 +7,9 @@ import ( "math/rand" "net/http" "path/filepath" - "reflect" "strconv" "strings" "time" - "unsafe" ) func jsonEncode(v interface{}) (io.Reader, error) { @@ -27,12 +25,12 @@ func jsonEncode(v interface{}) (io.Reader, error) { // GetRandomDeviceId 获取随机设备id func GetRandomDeviceId() string { - rand.Seed(time.Now().Unix()) + rng := rand.New(rand.NewSource(time.Now().Unix())) var builder strings.Builder builder.Grow(16) builder.WriteString("e") for i := 0; i < 15; i++ { - r := rand.Intn(9) + r := rng.Intn(9) builder.WriteString(strconv.Itoa(r)) } return builder.String() @@ -72,7 +70,3 @@ func getMessageType(filename string) string { } return doc } - -func stringToByte(s string) []byte { - return *(*[]byte)(unsafe.Pointer(&*(*reflect.StringHeader)(unsafe.Pointer(&s)))) -} diff --git a/user.go b/user.go index 53fddb6..203b847 100644 --- a/user.go +++ b/user.go @@ -252,7 +252,7 @@ func (u *User) OrderSymbol() string { } symbol = html.UnescapeString(symbol) symbol = strings.ToUpper(symbol) - symbol = regexp.MustCompile("/\\W/ig").ReplaceAllString(symbol, "") + symbol = regexp.MustCompile(`/\W/ig`).ReplaceAllString(symbol, "") if len(symbol) > 0 && symbol[0] < 'A' { return "~" } @@ -386,8 +386,8 @@ func (s *Self) sendTextToUser(username, text string) (*SentMessage, error) { func (s *Self) sendEmoticonToUser(username, md5 string, file io.Reader) (*SentMessage, error) { opt := &CallerWebWxSendAppMsgOptions{ - LoginInfo: s.bot.Storage.LoginInfo, - BaseRequest: s.bot.Storage.Request, + LoginInfo: s.bot.Storage.LoginInfo, + BaseRequest: s.bot.Storage.Request, FromUserName: s.UserName, ToUserName: username, }