Bladeren bron

refactor(keyring): new package

ayn2op 5 maanden geleden
bovenliggende
commit
45003fca41
4 gewijzigde bestanden met toevoegingen van 31 en 10 verwijderingen
  1. 2 3
      cmd/application.go
  2. 2 3
      cmd/root.go
  3. 23 0
      internal/keyring/keyring.go
  4. 4 4
      internal/login/form.go

+ 2 - 3
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
 		}

+ 2 - 3
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)
 		}

+ 23 - 0
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)
+}

+ 4 - 4
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)