From 567b65df8d6016f3f70d4cf2b19c4c83eecf9967 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Wed, 10 Feb 2021 17:04:39 +0100 Subject: [PATCH] feat: autoupdates CLI commands --- internal/frontend/cli/frontend.go | 57 ++++++++++++++++++++---- internal/frontend/cli/system.go | 36 ++++++++++------ internal/frontend/cli/updates.go | 72 ++++++++++++++++++++++++++++++- 3 files changed, 143 insertions(+), 22 deletions(-) diff --git a/internal/frontend/cli/frontend.go b/internal/frontend/cli/frontend.go index a0b782ee..9118b5b1 100644 --- a/internal/frontend/cli/frontend.go +++ b/internal/frontend/cli/frontend.go @@ -102,10 +102,6 @@ func New( //nolint[funlen] Aliases: []string{"p"}, Func: fe.changePort, }) - changeCmd.AddCmd(&ishell.Cmd{Name: "proxy", - Help: "allow or disallow bridge to securely connect to proton via a third party when it is being blocked", - Func: fe.toggleAllowProxy, - }) changeCmd.AddCmd(&ishell.Cmd{Name: "smtp-security", Help: "change port numbers of IMAP and SMTP servers.(alias: ssl, starttls)", Aliases: []string{"ssl", "starttls"}, @@ -113,13 +109,56 @@ func New( //nolint[funlen] }) fe.AddCmd(changeCmd) + // DoH commands. + dohCmd := &ishell.Cmd{Name: "proxy", + Help: "allow or disallow bridge to securely connect to proton via a third party when it is being blocked", + } + dohCmd.AddCmd(&ishell.Cmd{Name: "allow", + Help: "allow bridge to securely connect to proton via a third party when it is being blocked", + Func: fe.allowProxy, + }) + dohCmd.AddCmd(&ishell.Cmd{Name: "disallow", + Help: "disallow bridge to securely connect to proton via a third party when it is being blocked", + Func: fe.disallowProxy, + }) + fe.AddCmd(dohCmd) + + // Updates commands. + updatesCmd := &ishell.Cmd{Name: "updates", + Help: "manage bridge updates", + } + updatesCmd.AddCmd(&ishell.Cmd{Name: "check", + Help: "check for Bridge updates", + Func: fe.checkUpdates, + }) + autoUpdatesCmd := &ishell.Cmd{Name: "autoupdates", + Help: "manage bridge updates", + } + updatesCmd.AddCmd(autoUpdatesCmd) + autoUpdatesCmd.AddCmd(&ishell.Cmd{Name: "enable", + Help: "automatically keep bridge up to date", + Func: fe.enableAutoUpdates, + }) + autoUpdatesCmd.AddCmd(&ishell.Cmd{Name: "disable", + Help: "require bridge to be manually updated", + Func: fe.disableAutoUpdates, + }) + updatesChannelCmd := &ishell.Cmd{Name: "channel", + Help: "switch updates channel", + } + updatesCmd.AddCmd(updatesChannelCmd) + updatesChannelCmd.AddCmd(&ishell.Cmd{Name: "early", + Help: "switch to the early-access updates channel", + Func: fe.selectEarlyChannel, + }) + updatesChannelCmd.AddCmd(&ishell.Cmd{Name: "stable", + Help: "switch to the stable updates channel", + Func: fe.selectStableChannel, + }) + fe.AddCmd(updatesCmd) + // Check commands. checkCmd := &ishell.Cmd{Name: "check", Help: "check internet connection or new version."} - checkCmd.AddCmd(&ishell.Cmd{Name: "updates", - Help: "check for Bridge updates. (aliases: u, v, version)", - Aliases: []string{"u", "version", "v"}, - Func: fe.checkUpdates, - }) checkCmd.AddCmd(&ishell.Cmd{Name: "internet", Help: "check internet connection. (aliases: i, conn, connection)", Aliases: []string{"i", "con", "connection"}, diff --git a/internal/frontend/cli/system.go b/internal/frontend/cli/system.go index ebe582d6..0b7396a0 100644 --- a/internal/frontend/cli/system.go +++ b/internal/frontend/cli/system.go @@ -132,19 +132,31 @@ func (f *frontendCLI) changePort(c *ishell.Context) { } } -func (f *frontendCLI) toggleAllowProxy(c *ishell.Context) { +func (f *frontendCLI) allowProxy(c *ishell.Context) { if f.settings.GetBool(settings.AllowProxyKey) { - f.Println("Bridge is currently set to use alternative routing to connect to Proton if it is being blocked.") - if f.yesNoQuestion("Are you sure you want to stop bridge from doing this") { - f.settings.SetBool(settings.AllowProxyKey, false) - f.bridge.DisallowProxy() - } - } else { - f.Println("Bridge is currently set to NOT use alternative routing to connect to Proton if it is being blocked.") - if f.yesNoQuestion("Are you sure you want to allow bridge to do this") { - f.settings.SetBool(settings.AllowProxyKey, true) - f.bridge.AllowProxy() - } + f.Println("Bridge is already set to use alternative routing to connect to Proton if it is being blocked.") + return + } + + f.Println("Bridge is currently set to NOT use alternative routing to connect to Proton if it is being blocked.") + + if f.yesNoQuestion("Are you sure you want to allow bridge to do this") { + f.settings.SetBool(settings.AllowProxyKey, true) + f.bridge.AllowProxy() + } +} + +func (f *frontendCLI) disallowProxy(c *ishell.Context) { + if !f.settings.GetBool(settings.AllowProxyKey) { + f.Println("Bridge is already set to NOT use alternative routing to connect to Proton if it is being blocked.") + return + } + + f.Println("Bridge is currently set to use alternative routing to connect to Proton if it is being blocked.") + + if f.yesNoQuestion("Are you sure you want to stop bridge from doing this") { + f.settings.SetBool(settings.AllowProxyKey, false) + f.bridge.DisallowProxy() } } diff --git a/internal/frontend/cli/updates.go b/internal/frontend/cli/updates.go index 01c84845..4316517f 100644 --- a/internal/frontend/cli/updates.go +++ b/internal/frontend/cli/updates.go @@ -21,11 +21,23 @@ import ( "strings" "github.com/ProtonMail/proton-bridge/internal/bridge" + "github.com/ProtonMail/proton-bridge/internal/config/settings" + "github.com/ProtonMail/proton-bridge/internal/updater" "github.com/abiosoft/ishell" ) func (f *frontendCLI) checkUpdates(c *ishell.Context) { - f.Println("Your version is up to date.") + version, err := f.updater.Check() + if err != nil { + f.Println("An error occurred while checking for updates.") + return + } + + if f.updater.IsUpdateApplicable(version) { + f.Println("An update is available.") + } else { + f.Println("Your version is up to date.") + } } func (f *frontendCLI) printCredits(c *ishell.Context) { @@ -33,3 +45,61 @@ func (f *frontendCLI) printCredits(c *ishell.Context) { f.Println(pkg) } } + +func (f *frontendCLI) enableAutoUpdates(c *ishell.Context) { + if f.settings.GetBool(settings.AutoUpdateKey) { + f.Println("Bridge is already set to automatically install updates.") + return + } + + 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) + } +} + +func (f *frontendCLI) disableAutoUpdates(c *ishell.Context) { + if !f.settings.GetBool(settings.AutoUpdateKey) { + f.Println("Bridge is already set to NOT automatically install updates.") + return + } + + 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) + } +} + +func (f *frontendCLI) selectEarlyChannel(c *ishell.Context) { + if f.bridge.GetUpdateChannel() == updater.EarlyChannel { + f.Println("Bridge is already on the early-access update channel.") + return + } + + f.Println("Bridge is currently on the stable update channel.") + + if f.yesNoQuestion("Are you sure you want to switch to the early-access update channel") { + if err := f.bridge.SetUpdateChannel(updater.EarlyChannel); err != nil { + f.Println("There was a problem switching update channel.") + } + } +} + +func (f *frontendCLI) selectStableChannel(c *ishell.Context) { + if f.bridge.GetUpdateChannel() == updater.StableChannel { + f.Println("Bridge is already on the stable update channel.") + return + } + + f.Println("Bridge is currently on the early-access update channel.") + f.Println("Switching to the stable channel may reset all data!") + + if f.yesNoQuestion("Are you sure you want to switch to the stable update channel") { + if err := f.bridge.SetUpdateChannel(updater.StableChannel); err != nil { + f.Println("There was a problem switching update channel.") + } + f.restarter.SetToRestart() + } +}