refactor(keyring): new package
This commit is contained in:
parent
a5b847afe9
commit
45003fca41
4 changed files with 31 additions and 10 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
internal/keyring/keyring.go
Normal file
23
internal/keyring/keyring.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue