Other(refactor): Move Settings out of frontend

This commit is contained in:
James Houlahan
2022-08-22 12:32:55 +02:00
committed by Jakub
parent 310c6a1ccf
commit 0c7453684b
17 changed files with 178 additions and 129 deletions

View File

@ -66,13 +66,13 @@ func (f *frontendCLI) showAccountInfo(c *ishell.Context) {
func (f *frontendCLI) showAccountAddressInfo(user types.User, address string) {
smtpSecurity := "STARTTLS"
if f.settings.GetBool(settings.SMTPSSLKey) {
if f.bridge.GetBool(settings.SMTPSSLKey) {
smtpSecurity = "SSL"
}
f.Println(bold("Configuration for " + address))
f.Printf("IMAP Settings\nAddress: %s\nIMAP port: %d\nUsername: %s\nPassword: %s\nSecurity: %s\n",
bridge.Host,
f.settings.GetInt(settings.IMAPPortKey),
f.bridge.GetInt(settings.IMAPPortKey),
address,
user.GetBridgePassword(),
"STARTTLS",
@ -80,7 +80,7 @@ func (f *frontendCLI) showAccountAddressInfo(user types.User, address string) {
f.Println("")
f.Printf("SMTP Settings\nAddress: %s\nSMTP port: %d\nUsername: %s\nPassword: %s\nSecurity: %s\n",
bridge.Host,
f.settings.GetInt(settings.SMTPPortKey),
f.bridge.GetInt(settings.SMTPPortKey),
address,
user.GetBridgePassword(),
smtpSecurity,

View File

@ -19,7 +19,6 @@
package cli
import (
"github.com/ProtonMail/proton-bridge/v2/internal/config/settings"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
@ -34,7 +33,6 @@ var log = logrus.WithField("pkg", "frontend/cli") //nolint:gochecknoglobals
type frontendCLI struct {
*ishell.Shell
settings *settings.Settings
eventListener listener.Listener
updater types.Updater
bridge types.Bridger
@ -46,7 +44,6 @@ type frontendCLI struct {
func New( //nolint:funlen
panicHandler types.PanicHandler,
settings *settings.Settings,
eventListener listener.Listener,
updater types.Updater,
bridge types.Bridger,
@ -55,7 +52,6 @@ func New( //nolint:funlen
fe := &frontendCLI{
Shell: ishell.New(),
settings: settings,
eventListener: eventListener,
updater: updater,
bridge: bridge,

View File

@ -75,7 +75,7 @@ func (f *frontendCLI) changeSMTPSecurity(c *ishell.Context) {
f.ShowPrompt(false)
defer f.ShowPrompt(true)
isSSL := f.settings.GetBool(settings.SMTPSSLKey)
isSSL := f.bridge.GetBool(settings.SMTPSSLKey)
newSecurity := "SSL"
if isSSL {
newSecurity = "STARTTLS"
@ -84,7 +84,7 @@ func (f *frontendCLI) changeSMTPSecurity(c *ishell.Context) {
msg := fmt.Sprintf("Are you sure you want to change SMTP setting to %q and restart the Bridge", newSecurity)
if f.yesNoQuestion(msg) {
f.settings.SetBool(settings.SMTPSSLKey, !isSSL)
f.bridge.SetBool(settings.SMTPSSLKey, !isSSL)
f.Println("Restarting Bridge...")
f.restarter.SetToRestart()
f.Stop()
@ -95,14 +95,14 @@ func (f *frontendCLI) changePort(c *ishell.Context) {
f.ShowPrompt(false)
defer f.ShowPrompt(true)
currentPort = f.settings.Get(settings.IMAPPortKey)
currentPort = f.bridge.Get(settings.IMAPPortKey)
newIMAPPort := f.readStringInAttempts("Set IMAP port (current "+currentPort+")", c.ReadLine, f.isPortFree)
if newIMAPPort == "" {
newIMAPPort = currentPort
}
imapPortChanged := newIMAPPort != currentPort
currentPort = f.settings.Get(settings.SMTPPortKey)
currentPort = f.bridge.Get(settings.SMTPPortKey)
newSMTPPort := f.readStringInAttempts("Set SMTP port (current "+currentPort+")", c.ReadLine, f.isPortFree)
if newSMTPPort == "" {
newSMTPPort = currentPort
@ -116,8 +116,8 @@ func (f *frontendCLI) changePort(c *ishell.Context) {
if imapPortChanged || smtpPortChanged {
f.Println("Saving values IMAP:", newIMAPPort, "SMTP:", newSMTPPort)
f.settings.Set(settings.IMAPPortKey, newIMAPPort)
f.settings.Set(settings.SMTPPortKey, newSMTPPort)
f.bridge.Set(settings.IMAPPortKey, newIMAPPort)
f.bridge.Set(settings.SMTPPortKey, newSMTPPort)
f.Println("Restarting Bridge...")
f.restarter.SetToRestart()
f.Stop()
@ -179,7 +179,7 @@ func (f *frontendCLI) showAllMail(c *ishell.Context) {
}
func (f *frontendCLI) enableCacheOnDisk(c *ishell.Context) {
if f.settings.GetBool(settings.CacheEnabledKey) {
if f.bridge.GetBool(settings.CacheEnabledKey) {
f.Println("The local cache is already enabled.")
return
}
@ -196,7 +196,7 @@ func (f *frontendCLI) enableCacheOnDisk(c *ishell.Context) {
}
func (f *frontendCLI) disableCacheOnDisk(c *ishell.Context) {
if !f.settings.GetBool(settings.CacheEnabledKey) {
if !f.bridge.GetBool(settings.CacheEnabledKey) {
f.Println("The local cache is already disabled.")
return
}
@ -213,17 +213,17 @@ func (f *frontendCLI) disableCacheOnDisk(c *ishell.Context) {
}
func (f *frontendCLI) setCacheOnDiskLocation(c *ishell.Context) {
if !f.settings.GetBool(settings.CacheEnabledKey) {
if !f.bridge.GetBool(settings.CacheEnabledKey) {
f.Println("The local cache must be enabled.")
return
}
if location := f.settings.Get(settings.CacheLocationKey); location != "" {
if location := f.bridge.Get(settings.CacheLocationKey); location != "" {
f.Println("The current local cache location is:", location)
}
if location := f.readStringInAttempts("Enter a new location for the cache", c.ReadLine, f.isCacheLocationUsable); location != "" {
if err := f.bridge.MigrateCache(f.settings.Get(settings.CacheLocationKey), location); err != nil {
if err := f.bridge.MigrateCache(f.bridge.Get(settings.CacheLocationKey), location); err != nil {
f.Println("The local cache location could not be changed.")
return
}

View File

@ -47,7 +47,7 @@ func (f *frontendCLI) printCredits(c *ishell.Context) {
}
func (f *frontendCLI) enableAutoUpdates(c *ishell.Context) {
if f.settings.GetBool(settings.AutoUpdateKey) {
if f.bridge.GetBool(settings.AutoUpdateKey) {
f.Println("Bridge is already set to automatically install updates.")
return
}
@ -55,12 +55,12 @@ func (f *frontendCLI) enableAutoUpdates(c *ishell.Context) {
f.Println("Bridge is currently set to NOT automatically install updates.")
if f.yesNoQuestion("Are you sure you want to allow bridge to do this") {
f.settings.SetBool(settings.AutoUpdateKey, true)
f.bridge.SetBool(settings.AutoUpdateKey, true)
}
}
func (f *frontendCLI) disableAutoUpdates(c *ishell.Context) {
if !f.settings.GetBool(settings.AutoUpdateKey) {
if !f.bridge.GetBool(settings.AutoUpdateKey) {
f.Println("Bridge is already set to NOT automatically install updates.")
return
}
@ -68,7 +68,7 @@ func (f *frontendCLI) disableAutoUpdates(c *ishell.Context) {
f.Println("Bridge is currently set to automatically install updates.")
if f.yesNoQuestion("Are you sure you want to stop bridge from doing this") {
f.settings.SetBool(settings.AutoUpdateKey, false)
f.bridge.SetBool(settings.AutoUpdateKey, false)
}
}

View File

@ -20,7 +20,6 @@ package frontend
import (
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/config/settings"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/cli"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/grpc"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/types"
@ -42,7 +41,6 @@ func New(
frontendType string,
showWindowOnStart bool,
panicHandler types.PanicHandler,
settings *settings.Settings,
eventListener listener.Listener,
updater types.Updater,
bridge *bridge.Bridge,
@ -54,7 +52,6 @@ func New(
return grpc.NewService(
showWindowOnStart,
panicHandler,
settings,
eventListener,
updater,
bridgeWrap,
@ -64,7 +61,6 @@ func New(
case "cli":
return cli.New(
panicHandler,
settings,
eventListener,
updater,
bridgeWrap,

View File

@ -50,7 +50,6 @@ type Service struct { // nolint:structcheck
eventStreamDoneCh chan struct{}
panicHandler types.PanicHandler
settings *settings.Settings
eventListener listener.Listener
updater types.Updater
updateCheckMutex sync.Mutex
@ -71,7 +70,6 @@ type Service struct { // nolint:structcheck
func NewService(
showOnStartup bool,
panicHandler types.PanicHandler,
settings *settings.Settings,
eventListener listener.Listener,
updater types.Updater,
bridge types.Bridger,
@ -80,7 +78,6 @@ func NewService(
s := Service{
UnimplementedBridgeServer: UnimplementedBridgeServer{},
panicHandler: panicHandler,
settings: settings,
eventListener: eventListener,
updater: updater,
bridge: bridge,
@ -126,7 +123,7 @@ func (s *Service) initAutostart() {
// TO-DO GODT-1681 Autostart needs to be properly implement for gRPC approach.
s.firstTimeAutostart.Do(func() {
shouldAutostartBeOn := s.settings.GetBool(settings.AutostartKey)
shouldAutostartBeOn := s.bridge.GetBool(settings.AutostartKey)
if s.bridge.IsFirstStart() || shouldAutostartBeOn {
if err := s.bridge.EnableAutostart(); err != nil {
s.log.WithField("prefs", shouldAutostartBeOn).WithError(err).Error("Failed to enable first autostart")
@ -138,7 +135,7 @@ func (s *Service) initAutostart() {
func (s *Service) Loop() error {
defer func() {
s.settings.SetBool(settings.FirstStartGUIKey, false)
s.bridge.SetBool(settings.FirstStartGUIKey, false)
}()
go func() {

View File

@ -129,7 +129,7 @@ func (s *Service) ShowSplashScreen(context.Context, *emptypb.Empty) (*wrapperspb
func (s *Service) IsFirstGuiStart(context.Context, *emptypb.Empty) (*wrapperspb.BoolValue, error) {
s.log.Info("IsFirstGuiStart")
return wrapperspb.Bool(s.settings.GetBool(settings.FirstStartGUIKey)), nil
return wrapperspb.Bool(s.bridge.GetBool(settings.FirstStartGUIKey)), nil
}
func (s *Service) SetIsAutostartOn(_ context.Context, isOn *wrapperspb.BoolValue) (*emptypb.Empty, error) {
@ -238,7 +238,7 @@ func (s *Service) SetColorSchemeName(_ context.Context, name *wrapperspb.StringV
return nil, status.Error(codes.NotFound, "Color scheme not available")
}
s.settings.Set(settings.ColorScheme, name.Value)
s.bridge.Set(settings.ColorScheme, name.Value)
return &emptypb.Empty{}, nil
}
@ -246,10 +246,10 @@ func (s *Service) SetColorSchemeName(_ context.Context, name *wrapperspb.StringV
func (s *Service) ColorSchemeName(context.Context, *emptypb.Empty) (*wrapperspb.StringValue, error) {
s.log.Info("ColorSchemeName")
current := s.settings.Get(settings.ColorScheme)
current := s.bridge.Get(settings.ColorScheme)
if !theme.IsAvailable(theme.Theme(current)) {
current = string(theme.DefaultTheme())
s.settings.Set(settings.ColorScheme, current)
s.bridge.Set(settings.ColorScheme, current)
}
return wrapperspb.String(current), nil
@ -459,12 +459,12 @@ func (s *Service) InstallUpdate(context.Context, *emptypb.Empty) (*emptypb.Empty
func (s *Service) SetIsAutomaticUpdateOn(_ context.Context, isOn *wrapperspb.BoolValue) (*emptypb.Empty, error) {
s.log.WithField("isOn", isOn.Value).Info("SetIsAutomaticUpdateOn")
currentlyOn := s.settings.GetBool(settings.AutoUpdateKey)
currentlyOn := s.bridge.GetBool(settings.AutoUpdateKey)
if currentlyOn == isOn.Value {
return &emptypb.Empty{}, nil
}
s.settings.SetBool(settings.AutoUpdateKey, isOn.Value)
s.bridge.SetBool(settings.AutoUpdateKey, isOn.Value)
s.checkUpdateAndNotify(false)
return &emptypb.Empty{}, nil
@ -473,17 +473,17 @@ func (s *Service) SetIsAutomaticUpdateOn(_ context.Context, isOn *wrapperspb.Boo
func (s *Service) IsAutomaticUpdateOn(context.Context, *emptypb.Empty) (*wrapperspb.BoolValue, error) {
s.log.Info("IsAutomaticUpdateOn")
return wrapperspb.Bool(s.settings.GetBool(settings.AutoUpdateKey)), nil
return wrapperspb.Bool(s.bridge.GetBool(settings.AutoUpdateKey)), nil
}
func (s *Service) IsCacheOnDiskEnabled(context.Context, *emptypb.Empty) (*wrapperspb.BoolValue, error) {
s.log.Info("IsCacheOnDiskEnabled")
return wrapperspb.Bool(s.settings.GetBool(settings.CacheEnabledKey)), nil
return wrapperspb.Bool(s.bridge.GetBool(settings.CacheEnabledKey)), nil
}
func (s *Service) DiskCachePath(context.Context, *emptypb.Empty) (*wrapperspb.StringValue, error) {
s.log.Info("DiskCachePath")
return wrapperspb.String(s.settings.Get(settings.CacheLocationKey)), nil
return wrapperspb.String(s.bridge.Get(settings.CacheLocationKey)), nil
}
func (s *Service) ChangeLocalCache(_ context.Context, change *ChangeLocalCacheRequest) (*emptypb.Empty, error) {
@ -492,10 +492,10 @@ func (s *Service) ChangeLocalCache(_ context.Context, change *ChangeLocalCacheRe
Info("DiskCachePath")
defer func() { _ = s.SendEvent(NewCacheChangeLocalCacheFinishedEvent()) }()
defer func() { _ = s.SendEvent(NewIsCacheOnDiskEnabledChanged(s.settings.GetBool(settings.CacheEnabledKey))) }()
defer func() { _ = s.SendEvent(NewDiskCachePathChanged(s.settings.Get(settings.CacheCompressionKey))) }()
defer func() { _ = s.SendEvent(NewIsCacheOnDiskEnabledChanged(s.bridge.GetBool(settings.CacheEnabledKey))) }()
defer func() { _ = s.SendEvent(NewDiskCachePathChanged(s.bridge.Get(settings.CacheCompressionKey))) }()
if change.EnableDiskCache != s.settings.GetBool(settings.CacheEnabledKey) {
if change.EnableDiskCache != s.bridge.GetBool(settings.CacheEnabledKey) {
if change.EnableDiskCache {
if err := s.bridge.EnableCache(); err != nil {
s.log.WithError(err).Error("Cannot enable disk cache")
@ -513,13 +513,13 @@ func (s *Service) ChangeLocalCache(_ context.Context, change *ChangeLocalCacheRe
path = path[1:]
}
if change.EnableDiskCache && path != s.settings.Get(settings.CacheLocationKey) {
if err := s.bridge.MigrateCache(s.settings.Get(settings.CacheLocationKey), path); err != nil {
if change.EnableDiskCache && path != s.bridge.Get(settings.CacheLocationKey) {
if err := s.bridge.MigrateCache(s.bridge.Get(settings.CacheLocationKey), path); err != nil {
s.log.WithError(err).Error("The local cache location could not be changed.")
_ = s.SendEvent(NewCacheErrorEvent(CacheErrorType_CACHE_CANT_MOVE_ERROR))
return &emptypb.Empty{}, nil
}
s.settings.Set(settings.CacheLocationKey, path)
s.bridge.Set(settings.CacheLocationKey, path)
}
_ = s.SendEvent(NewCacheLocationChangeSuccessEvent())
@ -545,13 +545,13 @@ func (s *Service) IsDoHEnabled(context.Context, *emptypb.Empty) (*wrapperspb.Boo
func (s *Service) SetUseSslForSmtp(_ context.Context, useSsl *wrapperspb.BoolValue) (*emptypb.Empty, error) { //nolint:revive,stylecheck
s.log.WithField("useSsl", useSsl.Value).Info("SetUseSslForSmtp")
if s.settings.GetBool(settings.SMTPSSLKey) == useSsl.Value {
if s.bridge.GetBool(settings.SMTPSSLKey) == useSsl.Value {
return &emptypb.Empty{}, nil
}
defer func() { _ = s.SendEvent(NewMailSettingsUseSslForSmtpFinishedEvent()) }()
s.settings.SetBool(settings.SMTPSSLKey, useSsl.Value)
s.bridge.SetBool(settings.SMTPSSLKey, useSsl.Value)
s.restart()
return &emptypb.Empty{}, nil
@ -560,7 +560,7 @@ func (s *Service) SetUseSslForSmtp(_ context.Context, useSsl *wrapperspb.BoolVal
func (s *Service) UseSslForSmtp(context.Context, *emptypb.Empty) (*wrapperspb.BoolValue, error) { //nolint:revive,stylecheck
s.log.Info("UseSslForSmtp")
return wrapperspb.Bool(s.settings.GetBool(settings.SMTPSSLKey)), nil
return wrapperspb.Bool(s.bridge.GetBool(settings.SMTPSSLKey)), nil
}
func (s *Service) Hostname(context.Context, *emptypb.Empty) (*wrapperspb.StringValue, error) {
@ -572,13 +572,13 @@ func (s *Service) Hostname(context.Context, *emptypb.Empty) (*wrapperspb.StringV
func (s *Service) ImapPort(context.Context, *emptypb.Empty) (*wrapperspb.Int32Value, error) {
s.log.Info("ImapPort")
return wrapperspb.Int32(int32(s.settings.GetInt(settings.IMAPPortKey))), nil
return wrapperspb.Int32(int32(s.bridge.GetInt(settings.IMAPPortKey))), nil
}
func (s *Service) SmtpPort(context.Context, *emptypb.Empty) (*wrapperspb.Int32Value, error) { //nolint:revive,stylecheck
s.log.Info("SmtpPort")
return wrapperspb.Int32(int32(s.settings.GetInt(settings.SMTPPortKey))), nil
return wrapperspb.Int32(int32(s.bridge.GetInt(settings.SMTPPortKey))), nil
}
func (s *Service) ChangePorts(_ context.Context, ports *ChangePortsRequest) (*emptypb.Empty, error) {
@ -586,8 +586,8 @@ func (s *Service) ChangePorts(_ context.Context, ports *ChangePortsRequest) (*em
defer func() { _ = s.SendEvent(NewMailSettingsChangePortFinishedEvent()) }()
s.settings.SetInt(settings.IMAPPortKey, int(ports.ImapPort))
s.settings.SetInt(settings.SMTPPortKey, int(ports.SmtpPort))
s.bridge.SetInt(settings.IMAPPortKey, int(ports.ImapPort))
s.bridge.SetInt(settings.SMTPPortKey, int(ports.SmtpPort))
s.restart()
return &emptypb.Empty{}, nil

View File

@ -19,9 +19,7 @@ package grpc
import (
"context"
"time"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/clientconfig"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
@ -108,25 +106,29 @@ func (s *Service) RemoveUser(_ context.Context, userID *wrapperspb.StringValue)
}
func (s *Service) ConfigureUserAppleMail(_ context.Context, request *ConfigureAppleMailRequest) (*emptypb.Empty, error) {
s.log.WithField("UserID", request.UserID).WithField("Address", request.Address).Info("ConfigureUserAppleMail")
// NOTE: Configure apple mail should be part of bridge (and thus not need to deal with accounts/settings!
user, err := s.bridge.GetUser(request.UserID)
if err != nil {
s.log.WithField("userID", request.UserID).Error("Cannot configure AppleMail for user")
return nil, status.Error(codes.NotFound, "Cannot configure AppleMail for user")
}
/*
s.log.WithField("UserID", request.UserID).WithField("Address", request.Address).Info("ConfigureUserAppleMail")
needRestart, err := clientconfig.ConfigureAppleMail(user, request.Address, s.settings)
if err != nil {
s.log.WithError(err).Error("Apple Mail config failed")
return nil, status.Error(codes.Internal, "Apple Mail config failed")
}
user, err := s.bridge.GetUser(request.UserID)
if err != nil {
s.log.WithField("userID", request.UserID).Error("Cannot configure AppleMail for user")
return nil, status.Error(codes.NotFound, "Cannot configure AppleMail for user")
}
if needRestart {
// There is delay needed for external window to open
time.Sleep(2 * time.Second)
s.restart()
}
needRestart, err := clientconfig.ConfigureAppleMail(user, request.Address, s.settings)
if err != nil {
s.log.WithError(err).Error("Apple Mail config failed")
return nil, status.Error(codes.Internal, "Apple Mail config failed")
}
if needRestart {
// There is delay needed for external window to open
time.Sleep(2 * time.Second)
s.restart()
}
*/
return &emptypb.Empty{}, nil
}

View File

@ -22,6 +22,7 @@ import (
"crypto/tls"
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/config/settings"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
"github.com/ProtonMail/proton-bridge/v2/pkg/pmapi"
)
@ -84,6 +85,13 @@ type Bridger interface {
GetCurrentUserAgent() string
SetCurrentPlatform(string)
Get(settings.Key) string
Set(settings.Key, string)
GetBool(settings.Key) bool
SetBool(settings.Key, bool)
GetInt(settings.Key) int
SetInt(settings.Key, int)
// -- old --
ReportBug(osType, osVersion, description, accountName, address, emailClient string, attachLogs bool) error