From d5377d55744373a9a06018daf829a2cb0a978a88 Mon Sep 17 00:00:00 2001 From: M Mou Date: Thu, 26 Mar 2020 18:01:22 -0700 Subject: [PATCH] add snooze CLI command --- protocol.go | 2 +- service/main.go | 14 +++++++++++++- updater.go | 25 +++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/protocol.go b/protocol.go index a16f871a..4fd965b0 100644 --- a/protocol.go +++ b/protocol.go @@ -107,7 +107,7 @@ type UpdatePromptOptions struct { type UpdatePromptResponse struct { Action UpdateAction `json:"action"` AutoUpdate bool `json:"autoUpdate"` - SnoozeDuration int `json:"snooze_duration"` + SnoozeDuration int `json:"snooze_duration"` // in seconds } // UpdateUI is a UI interface diff --git a/service/main.go b/service/main.go index 23a1090d..4cf8c122 100644 --- a/service/main.go +++ b/service/main.go @@ -22,6 +22,7 @@ type flags struct { appName string pathToKeybase string command string + ignoreSnooze bool } func main() { @@ -39,6 +40,7 @@ func loadFlags() (flags, []string) { flag.BoolVar(&f.logToFile, "log-to-file", false, "Log to file") flag.StringVar(&f.pathToKeybase, "path-to-keybase", "", "Path to keybase executable") flag.StringVar(&f.appName, "app-name", defaultAppName(), "App name") + flag.BoolVar(&f.ignoreSnooze, "ignore-snooze", true, "Ignore snooze, if not in service mode") flag.Parse() args := flag.Args() return f, args @@ -97,6 +99,12 @@ func run(f flags) { ulog.Error(err) os.Exit(1) } + case "snooze": + ctx, updater := keybase.NewUpdaterContext(f.appName, f.pathToKeybase, ulog, keybase.Check) + if err := updater.Snooze(ctx); err != nil { + ulog.Error(err) + os.Exit(1) + } case "service", "": svc := serviceFromFlags(f, ulog) svc.Run() @@ -120,7 +128,11 @@ func serviceFromFlags(f flags, ulog logger) *service { } func updateCheckFromFlags(f flags, ulog logger) error { - ctx, updater := keybase.NewUpdaterContext(f.appName, f.pathToKeybase, ulog, keybase.Check) + mode := keybase.CheckPassive + if f.ignoreSnooze { + mode = keybase.Check + } + ctx, updater := keybase.NewUpdaterContext(f.appName, f.pathToKeybase, ulog, mode) _, err := updater.Update(ctx) return err } diff --git a/updater.go b/updater.go index 9ff3c968..15afdb9a 100644 --- a/updater.go +++ b/updater.go @@ -93,6 +93,26 @@ func (u *Updater) SetTickDuration(dur time.Duration) { u.tickDuration = dur } +// Snoozes an update if there is one +func (u *Updater) Snooze(ctx Context) error { + options := ctx.UpdateOptions() + update, err := u.checkForUpdate(ctx, options) + if err != nil { + return findErr(err) + } + if update == nil || !update.NeedUpdate { + // No update available + return nil + } + updatePromptResponse := UpdatePromptResponse{ + Action: UpdateActionSnooze, + AutoUpdate: false, // not used + SnoozeDuration: 86400, // seconds in 24 hrs + } + ctx.ReportAction(updatePromptResponse, update, options) + return nil +} + // Update checks, downloads and performs an update func (u *Updater) Update(ctx Context) (*Update, error) { options := ctx.UpdateOptions() @@ -102,7 +122,7 @@ func (u *Updater) Update(ctx Context) (*Update, error) { } // update returns the update received, and an error if the update was not -// performed. The error with be of type Error. The error may be due to the user +// performed. The error will be of type Error. The error may be due to the user // (or system) canceling an update, in which case error.IsCancel() will be true. func (u *Updater) update(ctx Context, options UpdateOptions) (*Update, error) { update, err := u.checkForUpdate(ctx, options) @@ -148,6 +168,7 @@ func (u *Updater) update(ctx Context, options UpdateOptions) (*Update, error) { case UpdateActionAuto: ctx.ReportAction(updatePromptResponse, update, options) case UpdateActionSnooze: + updatePromptResponse.SnoozeDuration = 86400 // seconds in 24 hrs ctx.ReportAction(updatePromptResponse, update, options) return update, CancelErr(fmt.Errorf("Snoozed update")) case UpdateActionCancel: @@ -258,7 +279,7 @@ func (u *Updater) checkForUpdate(ctx Context, options UpdateOptions) (*Update, e func (u *Updater) NeedUpdate(ctx Context) (upToDate bool, err error) { update, err := u.checkForUpdate(ctx, ctx.UpdateOptions()) if err != nil { - return false, err + return false, findErr(err) } return update.NeedUpdate, nil }