small refactor - move keyring logic to ServerManager

This commit is contained in:
Drew Weymouth
2023-02-11 16:34:08 -08:00
parent aa455d8ccb
commit 0e8950f0fc
2 changed files with 13 additions and 5 deletions
+9
View File
@@ -5,6 +5,7 @@ import (
"github.com/dweymouth/go-subsonic/subsonic" "github.com/dweymouth/go-subsonic/subsonic"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/zalando/go-keyring"
) )
type ServerManager struct { type ServerManager struct {
@@ -39,3 +40,11 @@ func (s *ServerManager) ConnectToServer(conf *ServerConfig, password string) err
func (s *ServerManager) OnServerConnected(cb func()) { func (s *ServerManager) OnServerConnected(cb func()) {
s.onServerConnected = append(s.onServerConnected, cb) s.onServerConnected = append(s.onServerConnected, cb)
} }
func (s *ServerManager) GetServerPassword(server *ServerConfig) (string, error) {
return keyring.Get(AppName, server.ID.String())
}
func (s *ServerManager) SetServerPassword(server *ServerConfig, password string) error {
return keyring.Set(AppName, server.ID.String(), password)
}
+4 -5
View File
@@ -10,7 +10,6 @@ import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
"github.com/20after4/configdir" "github.com/20after4/configdir"
"github.com/zalando/go-keyring"
) )
const ( const (
@@ -51,8 +50,7 @@ func main() {
if defaultServer == nil { if defaultServer == nil {
mainWindow.PromptForFirstServer(func(nick, host, user, pass string) { mainWindow.PromptForFirstServer(func(nick, host, user, pass string) {
server := myApp.Config.AddServer(nick, host, user) server := myApp.Config.AddServer(nick, host, user)
err := keyring.Set(appname, server.ID.String(), pass) if err := myApp.ServerManager.SetServerPassword(server, pass); err != nil {
if err != nil {
log.Printf("error setting keyring credentials: %v", err) log.Printf("error setting keyring credentials: %v", err)
// TODO: handle? // TODO: handle?
} }
@@ -70,13 +68,14 @@ func main() {
mainWindow.Window.Close() mainWindow.Window.Close()
}) })
fyneApp.Run() fyneApp.Run()
// shutdown tasks
myApp.Config.WriteConfigFile(configPath()) myApp.Config.WriteConfigFile(configPath())
myApp.Shutdown() myApp.Shutdown()
} }
func setupServer(app *backend.App, server *backend.ServerConfig) { func setupServer(app *backend.App, server *backend.ServerConfig) {
pass, err := keyring.Get(appname, server.ID.String()) pass, err := app.ServerManager.GetServerPassword(server)
if err != nil { if err != nil {
log.Printf("error getting password from keyring: %v", err) log.Printf("error getting password from keyring: %v", err)
} }