Skip to content

Commit

Permalink
use log package
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmsaaaam committed Apr 9, 2024
1 parent 8d43478 commit 32eb3b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
15 changes: 11 additions & 4 deletions daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package main

import (
"fmt"
"log"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -38,13 +38,16 @@ func main() {

message := createMessage(yesterDay, channels, count)
botClient := slack.New(os.Getenv("SLACK_BOT_TOKEN"))
botClient.PostMessage(os.Getenv("SLACK_CHANNEL_ID"), slack.MsgOptionText(message, false))
_, _, err := botClient.PostMessage(os.Getenv("SLACK_CHANNEL_ID"), slack.MsgOptionText(message, false))
if err != nil {
log.Printf("can not post: %v\n", err)
}
}

func (client SlackClient) getConversationsForUser() []slack.Channel {
conversations, _, err := client.GetConversationsForUser(&slack.GetConversationsForUserParameters{})
if err != nil {
fmt.Println(err)
log.Printf("can not get channels: %v\n", err)
}
return conversations
}
Expand All @@ -56,7 +59,11 @@ func createChannels(conversations []slack.Channel, now time.Time, yesterDay time
var count int
for _, conversation := range conversations {
params := slack.GetConversationHistoryParameters{ChannelID: conversation.ID, Limit: 1000, Latest: latest, Oldest: oldest}
conversationHistory, _ := userClient.GetConversationHistory(&params)
conversationHistory, err := userClient.GetConversationHistory(&params)
if err != nil {
log.Printf("can not get history channelID: %s, %v\n", conversation.ID, err)
continue
}
channel := Channel{name: conversation.Name, id: conversation.ID}
for _, message := range conversationHistory.Messages {
count++
Expand Down
21 changes: 11 additions & 10 deletions daily_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"bytes"
"embed"
"log"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestGetConversationsForUser(t *testing.T) {
{
name: "usersConversationsError",
apiRes: "testdata/usersConversations/error.json",
want: want{channels: []slack.Channel{}, print: "invalid_auth"},
want: want{channels: []slack.Channel{}, print: "can not get channels: invalid_auth"},
},
}
for _, tt := range tests {
Expand All @@ -56,21 +57,21 @@ func TestGetConversationsForUser(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Helper()

orgStdout := os.Stdout
var buf bytes.Buffer
log.SetOutput(&buf)
defaultFlags := log.Flags()
log.SetFlags(0)
defer func() {
os.Stdout = orgStdout
log.SetOutput(os.Stderr)
log.SetFlags(defaultFlags)
buf.Reset()
}()
r, w, _ := os.Pipe()
os.Stdout = w

got := SlackClient{client}.getConversationsForUser()

if len(got) != len(tt.want.channels) {
t.Errorf("add() = %v, want %v", got, tt.want.channels)
}
w.Close()
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
t.Fatalf("failed to read buf: %v", err)
}
gotPrint := strings.TrimRight(buf.String(), "\n")
if gotPrint != tt.want.print {
t.Errorf("add() = %v, want %v", gotPrint, tt.want.print)
Expand Down

0 comments on commit 32eb3b6

Please sign in to comment.