mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-16 15:16:44 +00:00
feat: add teardown to app base
This commit is contained in:
@ -77,6 +77,8 @@ type Base struct {
|
|||||||
usage string // the app's usage description
|
usage string // the app's usage description
|
||||||
command string // the command used to launch the app (either the exe path or the launcher path)
|
command string // the command used to launch the app (either the exe path or the launcher path)
|
||||||
restart bool // whether the app is currently set to restart
|
restart bool // whether the app is currently set to restart
|
||||||
|
|
||||||
|
teardown []func() error // actions to perform when app is exiting
|
||||||
}
|
}
|
||||||
|
|
||||||
func New( // nolint[funlen]
|
func New( // nolint[funlen]
|
||||||
@ -269,6 +271,11 @@ func (b *Base) SetToRestart() {
|
|||||||
b.restart = true
|
b.restart = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddTeardownAction adds an action to perform during app teardown.
|
||||||
|
func (b *Base) AddTeardownAction(fn func() error) {
|
||||||
|
b.teardown = append(b.teardown, fn)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc { // nolint[funlen]
|
func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc { // nolint[funlen]
|
||||||
return func(c *cli.Context) error {
|
return func(c *cli.Context) error {
|
||||||
defer b.CrashHandler.HandlePanic()
|
defer b.CrashHandler.HandlePanic()
|
||||||
@ -316,14 +323,24 @@ func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.restart {
|
if err := b.doTeardown(); err != nil {
|
||||||
return b.restartApp(false)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := b.Versioner.RemoveOldVersions(); err != nil {
|
if b.restart {
|
||||||
return err
|
return b.restartApp(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Base) doTeardown() error {
|
||||||
|
for _, action := range b.teardown {
|
||||||
|
if err := action(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@ -108,6 +108,9 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen]
|
|||||||
// Bridge supports no-window option which we should use for autostart.
|
// Bridge supports no-window option which we should use for autostart.
|
||||||
b.Autostart.Exec = append(b.Autostart.Exec, "--no-window")
|
b.Autostart.Exec = append(b.Autostart.Exec, "--no-window")
|
||||||
|
|
||||||
|
// We want to remove old versions if the app exits successfully.
|
||||||
|
b.AddTeardownAction(b.Versioner.RemoveOldVersions)
|
||||||
|
|
||||||
f := frontend.New(
|
f := frontend.New(
|
||||||
constants.Version,
|
constants.Version,
|
||||||
constants.BuildVersion,
|
constants.BuildVersion,
|
||||||
|
|||||||
@ -54,6 +54,9 @@ func run(b *base.Base, c *cli.Context) error {
|
|||||||
frontendMode = "qt"
|
frontendMode = "qt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We want to remove old versions if the app exits successfully.
|
||||||
|
b.AddTeardownAction(b.Versioner.RemoveOldVersions)
|
||||||
|
|
||||||
f := frontend.NewImportExport(
|
f := frontend.NewImportExport(
|
||||||
constants.Version,
|
constants.Version,
|
||||||
constants.BuildVersion,
|
constants.BuildVersion,
|
||||||
|
|||||||
Reference in New Issue
Block a user