Skip to content

Commit

Permalink
Merge branch 'slog'
Browse files Browse the repository at this point in the history
  • Loading branch information
ayn2op committed Sep 19, 2024
2 parents 328a9c9 + 5ccd4cd commit e56edc7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 40 deletions.
12 changes: 6 additions & 6 deletions cmd/guilds_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"log"
"log/slog"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (gt *GuildsTree) createFolderNode(folder gateway.GuildFolder) {
for _, gID := range folder.GuildIDs {
g, err := discordState.Cabinet.Guild(gID)
if err != nil {
log.Printf("guild %v not found in state: %v\n", gID, err)
slog.Info("guild not found in state", "err", err, "guild", g)
continue
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func (gt *GuildsTree) createChannelNode(n *tview.TreeNode, c discord.Channel) *t
if c.Type != discord.DirectMessage && c.Type != discord.GroupDM {
ps, err := discordState.Permissions(c.ID, discordState.Ready().User.ID)
if err != nil {
log.Println(err)
slog.Error("failed to get permissions", "err", err, "channel_id", c.ID)
return nil
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
case discord.GuildID:
cs, err := discordState.Cabinet.Channels(ref)
if err != nil {
log.Println(err)
slog.Error("failed to get channels", "err", err, "guild_id", ref)
return
}

Expand All @@ -198,7 +198,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {

c, err := discordState.Cabinet.Channel(ref)
if err != nil {
log.Println(err)
slog.Error("failed to get channel", "channel_id", ref)
return
}

Expand All @@ -209,7 +209,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) {
case nil: // Direct messages
cs, err := discordState.PrivateChannels()
if err != nil {
log.Println(err)
slog.Error("failed to get private channels", "err", err)
return
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/main_flex.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"log/slog"

"github.com/ayn2op/discordo/internal/constants"
"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -56,9 +56,12 @@ func (mf *MainFlex) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
return nil
case cfg.Keys.Logout:
app.Stop()

if err := keyring.Delete(constants.Name, "token"); err != nil {
log.Fatal(err)
slog.Error("failed to delete token from keyring", "err", err)
return nil
}

return nil
case cfg.Keys.ToggleGuildsTree:
// The guilds tree is visible if the numbers of items is two.
Expand Down
15 changes: 8 additions & 7 deletions cmd/message_input.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"log/slog"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -80,10 +80,11 @@ func (mi *MessageInput) send() {
return
}

logger := slog.With("channel_id", mainFlex.guildsTree.selectedChannelID)
if mi.replyMessageIdx != -1 {
ms, err := discordState.Cabinet.Messages(mainFlex.guildsTree.selectedChannelID)
if err != nil {
log.Println(err)
logger.Error("failed to get messages", "err", err)
return
}

Expand All @@ -99,13 +100,13 @@ func (mi *MessageInput) send() {

go func() {
if _, err := discordState.SendMessageComplex(mainFlex.guildsTree.selectedChannelID, data); err != nil {
log.Println("failed to send message:", err)
slog.Error("failed to send message", "err", err)
}
}()
} else {
go func() {
if _, err := discordState.SendMessage(mainFlex.guildsTree.selectedChannelID, text); err != nil {
log.Println("failed to send message:", err)
slog.Error("failed to send message", "err", err)
}
}()
}
Expand All @@ -125,7 +126,7 @@ func (mi *MessageInput) editor() {

f, err := os.CreateTemp("", constants.TmpFilePattern)
if err != nil {
log.Println(err)
slog.Error("failed to create temporary file", "err", err)
return
}
_, _ = f.WriteString(mi.GetText())
Expand All @@ -141,14 +142,14 @@ func (mi *MessageInput) editor() {
app.Suspend(func() {
err := cmd.Run()
if err != nil {
log.Println(err)
slog.Error("failed to run command", "err", err, "command", cmd)
return
}
})

msg, err := os.ReadFile(f.Name())
if err != nil {
log.Println(err)
slog.Error("failed to read temporary file", "err", err, "name", f.Name())
return
}

Expand Down
25 changes: 13 additions & 12 deletions cmd/messages_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -56,7 +56,7 @@ func newMessagesText() *MessagesText {
func (mt *MessagesText) drawMsgs(cID discord.ChannelID) {
ms, err := discordState.Messages(cID, uint(cfg.MessagesLimit))
if err != nil {
log.Println(err)
slog.Error("failed to get messages", "err", err, "channel_id", cID)
return
}

Expand Down Expand Up @@ -200,7 +200,7 @@ func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
func (mt *MessagesText) _select(name string) {
ms, err := discordState.Cabinet.Messages(mainFlex.guildsTree.selectedChannelID)
if err != nil {
log.Println(err)
slog.Error("failed to get messages", "err", err, "channel_id", mainFlex.guildsTree.selectedChannelID)
return
}

Expand Down Expand Up @@ -252,21 +252,21 @@ func (mt *MessagesText) _select(name string) {
func (mt *MessagesText) yank() {
msg, err := mt.getSelectedMessage()
if err != nil {
log.Println(err)
slog.Error("failed to get selected message", "err", err)
return
}

err = clipboard.WriteAll(msg.Content)
if err != nil {
log.Println("failed to write to clipboard:", err)
slog.Error("failed to write to clipboard", "err", err)
return
}
}

func (mt *MessagesText) open() {
msg, err := mt.getSelectedMessage()
if err != nil {
log.Println(err)
slog.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -278,7 +278,7 @@ func (mt *MessagesText) open() {
for _, a := range attachments {
go func() {
if err := open.Start(a.URL); err != nil {
log.Println(err)
slog.Error("failed to open URL", "err", err, "url", a.URL)
}
}()
}
Expand All @@ -295,7 +295,7 @@ func (mt *MessagesText) reply(mention bool) {

msg, err := mt.getSelectedMessage()
if err != nil {
log.Println(err)
slog.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -309,7 +309,7 @@ func (mt *MessagesText) delete() {

msg, err := mt.getSelectedMessage()
if err != nil {
log.Println(err)
slog.Error("failed to get selected message", "err", err)
return
}

Expand All @@ -330,17 +330,18 @@ func (mt *MessagesText) delete() {
}

if err := discordState.DeleteMessage(mainFlex.guildsTree.selectedChannelID, msg.ID, ""); err != nil {
log.Println(err)
slog.Error("failed to delete message", "err", err, "channel_id", mainFlex.guildsTree.selectedChannelID, "message_id", msg.ID)
return
}

if err := discordState.MessageRemove(mainFlex.guildsTree.selectedChannelID, msg.ID); err != nil {
log.Println(err)
slog.Error("failed to delete message", "err", err, "channel_id", mainFlex.guildsTree.selectedChannelID, "message_id", msg.ID)
return
}

ms, err := discordState.Cabinet.Messages(mainFlex.guildsTree.selectedChannelID)
if err != nil {
log.Println(err)
slog.Error("failed to delete message", "err", err, "channel_id", mainFlex.guildsTree.selectedChannelID)
return
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"log/slog"

"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/logger"
Expand Down Expand Up @@ -33,12 +33,14 @@ func Run(token string) error {
lf := NewLoginForm(func(token string, err error) {
if err != nil {
app.Stop()
log.Fatal(err)
slog.Error("failed to login", "err", err)
return
}

if err := openState(token); err != nil {
app.Stop()
log.Fatal(err)
slog.Error("failed to open state", "err", err)
return
}

app.SetRoot(mainFlex, true)
Expand Down
4 changes: 2 additions & 2 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"context"
"log"
"log/slog"
"runtime"
"slices"

Expand Down Expand Up @@ -45,7 +45,7 @@ func openState(token string) error {
func (s *State) onRequest(r httpdriver.Request) error {
req, ok := r.(*httpdriver.DefaultRequest)
if ok {
log.Printf("method = %s; url = %s\n", req.Method, req.URL)
slog.Info("new HTTP request", "method", req.Method, "path", req.URL.Path)
}

return nil
Expand Down
10 changes: 6 additions & 4 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package logger

import (
"log"
"log/slog"
"os"
"path/filepath"

Expand Down Expand Up @@ -30,12 +30,14 @@ func Load() error {
return err
}

f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}

log.SetOutput(f)
log.SetFlags(log.LstdFlags | log.Lshortfile)
l := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{
AddSource: true,
}))
slog.SetDefault(l)
return nil
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@ package main

import (
"flag"
"log"
"log/slog"

"github.com/ayn2op/discordo/cmd"
"github.com/ayn2op/discordo/internal/constants"
"github.com/zalando/go-keyring"
)

func main() {
// Declare and parse all flags first
token := flag.String("token", "", "authentication token")
flag.Parse()

// If no token was provided, look it up in the keyring
if *token == "" {
t, err := keyring.Get(constants.Name, "token")
if err != nil {
log.Println("failed to get token from keyring:", err)
slog.Info("failed to get token from keyring", "err", err)
} else {
*token = t
}
}

if err := cmd.Run(*token); err != nil {
log.Fatal(err)
slog.Error("failed to run", "err", err)
}
}

0 comments on commit e56edc7

Please sign in to comment.