From 45003fca41297d3e82de9b8a38ad6a976b6016b5 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Thu, 6 Nov 2025 21:10:22 -0500 Subject: [PATCH] refactor(keyring): new package --- cmd/application.go | 5 ++--- cmd/root.go | 5 ++--- internal/keyring/keyring.go | 23 +++++++++++++++++++++++ internal/login/form.go | 8 ++++---- 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 internal/keyring/keyring.go diff --git a/cmd/application.go b/cmd/application.go index 17d39c6..cfcb09a 100644 --- a/cmd/application.go +++ b/cmd/application.go @@ -4,12 +4,11 @@ import ( "log/slog" "github.com/ayn2op/discordo/internal/config" - "github.com/ayn2op/discordo/internal/consts" + "github.com/ayn2op/discordo/internal/keyring" "github.com/ayn2op/discordo/internal/login" "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" "github.com/gdamore/tcell/v2" - "github.com/zalando/go-keyring" ) const ( @@ -130,7 +129,7 @@ func (a *application) onPagesInputCapture(event *tcell.EventKey) *tcell.EventKey case a.cfg.Keys.Logout: a.quit() - if err := keyring.Delete(consts.Name, "token"); err != nil { + if err := keyring.DeleteToken(); err != nil { slog.Error("failed to delete token from keyring", "err", err) return nil } diff --git a/cmd/root.go b/cmd/root.go index 13cde3c..c3e3338 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,12 +8,11 @@ import ( "os" "github.com/ayn2op/discordo/internal/config" - "github.com/ayn2op/discordo/internal/consts" + "github.com/ayn2op/discordo/internal/keyring" "github.com/ayn2op/discordo/internal/logger" "github.com/ayn2op/tview" "github.com/diamondburned/arikawa/v3/utils/ws" "github.com/diamondburned/ningen/v3" - "github.com/zalando/go-keyring" ) var ( @@ -54,7 +53,7 @@ func Run() error { token := *tokenFlag if token == "" { - token, err = keyring.Get(consts.Name, "token") + token, err = keyring.GetToken() if err != nil { slog.Info("failed to retrieve token from keyring", "err", err) } diff --git a/internal/keyring/keyring.go b/internal/keyring/keyring.go new file mode 100644 index 0000000..d4d77db --- /dev/null +++ b/internal/keyring/keyring.go @@ -0,0 +1,23 @@ +package keyring + +import ( + "github.com/ayn2op/discordo/internal/consts" + "github.com/zalando/go-keyring" +) + +const ( + keyringService = consts.Name + keyringUser = "token" +) + +func GetToken() (string, error) { + return keyring.Get(keyringService, keyringUser) +} + +func SetToken(s string) error { + return keyring.Set(keyringService, keyringUser, s) +} + +func DeleteToken() error { + return keyring.Delete(keyringService, keyringUser) +} diff --git a/internal/login/form.go b/internal/login/form.go index 632d321..9c8dbbc 100644 --- a/internal/login/form.go +++ b/internal/login/form.go @@ -5,12 +5,11 @@ import ( "log/slog" "github.com/ayn2op/discordo/internal/config" - "github.com/ayn2op/discordo/internal/consts" "github.com/ayn2op/discordo/internal/http" + "github.com/ayn2op/discordo/internal/keyring" "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" "github.com/diamondburned/arikawa/v3/api" - "github.com/zalando/go-keyring" "golang.design/x/clipboard" ) @@ -91,7 +90,7 @@ func (f *Form) login() { return } - go keyring.Set(consts.Name, "token", resp.Token) + go keyring.SetToken(resp.Token) if f.done != nil { f.done(resp.Token) @@ -131,7 +130,8 @@ func (f *Form) loginWithQR() { return } - go keyring.Set(consts.Name, "token", token) + go keyring.SetToken(token) + f.RemovePage(qrPageName) if f.done != nil { f.done(token)