Other: Factory reset

This commit is contained in:
James Houlahan
2022-10-13 03:26:31 +02:00
parent cec44be7c3
commit a74b025de3
8 changed files with 62 additions and 12 deletions

View File

@ -240,6 +240,10 @@ func (f *frontendCLI) deleteEverything(c *ishell.Context) {
f.bridge.FactoryReset(context.Background())
c.Println("Everything cleared")
f.restarter.Set(true, false)
f.Stop()
}
func (f *frontendCLI) changeMode(c *ishell.Context) {

View File

@ -24,6 +24,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/constants"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/pkg/restarter"
"github.com/abiosoft/ishell"
"github.com/sirupsen/logrus"
@ -34,14 +35,16 @@ var log = logrus.WithField("pkg", "frontend/cli") //nolint:gochecknoglobals
type frontendCLI struct {
*ishell.Shell
bridge *bridge.Bridge
bridge *bridge.Bridge
restarter *restarter.Restarter
}
// New returns a new CLI frontend configured with the given options.
func New(bridge *bridge.Bridge, eventCh <-chan events.Event) *frontendCLI {
func New(bridge *bridge.Bridge, restarter *restarter.Restarter, eventCh <-chan events.Event) *frontendCLI {
fe := &frontendCLI{
Shell: ishell.New(),
bridge: bridge,
Shell: ishell.New(),
bridge: bridge,
restarter: restarter,
}
// Clear commands.

View File

@ -27,6 +27,7 @@ import (
"net"
"path/filepath"
"sync"
"time"
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/certs"
@ -298,6 +299,9 @@ func (s *Service) finishLogin() {
return
}
eventCh, done := s.bridge.GetEvents(events.UserLoggedIn{})
defer done()
userID, err := s.bridge.LoginUser(context.Background(), s.authClient, s.auth, s.password)
if err != nil {
s.log.WithError(err).Errorf("Finish login failed")
@ -305,18 +309,34 @@ func (s *Service) finishLogin() {
return
}
s.waitForUserChangeDone(eventCh, userID)
s.log.WithField("userID", userID).Debug("Login finished")
_ = s.SendEvent(NewLoginFinishedEvent(userID))
}
func (s *Service) waitForUserChangeDone(eventCh <-chan events.Event, userID string) {
for {
select {
case event := <-eventCh:
if login, ok := event.(events.UserLoggedIn); ok && login.UserID == userID {
return
}
case <-time.After(2 * time.Second):
s.log.WithField("ID", userID).Warning("Login finished but user not added within 2 seconds")
return
}
}
}
func (s *Service) triggerReset() {
defer func() {
_ = s.SendEvent(NewResetFinishedEvent())
}()
if err := s.bridge.FactoryReset(context.Background()); err != nil {
s.log.WithError(err).Error("Failed to reset")
}
s.bridge.FactoryReset(context.Background())
}
func newTLSConfig() (*tls.Config, []byte, error) {