Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
add snooze CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
mmou committed Mar 27, 2020
1 parent 0f55567 commit d5377d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type flags struct {
appName string
pathToKeybase string
command string
ignoreSnooze bool
}

func main() {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
25 changes: 23 additions & 2 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d5377d5

Please sign in to comment.