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

Prompt for apply #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions keybase/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ func (c context) BeforeApply(update updater.Update) error {
if err != nil {
c.log.Warningf("Error trying to check in use: %s", err)
}
if inUse {
if cancel := c.PausedPrompt(); cancel {
return fmt.Errorf("Canceled by user from paused prompt")
}
if cancel := c.PausedPrompt(inUse); cancel {
return fmt.Errorf("Canceled by user from paused prompt")
}
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions keybase/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ func (c context) UpdatePrompt(update updater.Update, options updater.UpdateOptio
return c.updatePrompt(promptProgram, update, options, promptOptions, time.Hour)
}

// PausedPrompt is called when the we can't update cause the app is in use.
// We return true if the use wants to cancel the update.
func (c context) PausedPrompt() bool {
// PausedPrompt is called when want to apply the update.
// If inUse is true, then we should warn the user about KBFS mount changing.
// We return true to apply the update, otherwise cancel.
func (c context) PausedPrompt(inUse bool) bool {
promptProgram, err := c.config.promptProgram()
if err != nil {
c.log.Warningf("Error trying to get prompt path: %s", err)
return false
}
cancelUpdate, err := c.pausedPrompt(promptProgram, 5*time.Minute)
cancelUpdate, err := c.pausedPrompt(promptProgram, inUse, 5*time.Minute)
if err != nil {
c.log.Warningf("Error in paused prompt: %s", err)
return false
Expand Down
2 changes: 1 addition & 1 deletion keybase/platform_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c context) UpdatePrompt(update updater.Update, options updater.UpdateOptio
return &updater.UpdatePromptResponse{Action: updater.UpdateActionContinue}, nil
}

func (c context) PausedPrompt() bool {
func (c context) PausedPrompt(inUse bool) bool {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion keybase/platform_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (c context) updaterPromptResultFromFile(path string) (*updaterPromptInputRe
return &result, nil
}

func (c context) PausedPrompt() bool {
func (c context) PausedPrompt(inUse bool) bool {
return false
}

Expand Down
38 changes: 37 additions & 1 deletion keybase/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ type promptInputResult struct {
// pausedPrompt returns whether to cancel update and/or error.
// If the user explicit wants to cancel the update, this may be different from
// an error occurring, in which case
func (c context) pausedPrompt(promptProgram command.Program, timeout time.Duration) (bool, error) {
func (c context) pausedPrompt(promptProgram command.Program, inUse bool, timeout time.Duration) (bool, error) {
if inUse {
return c.pausedPromptInUse(promptProgram, timeout)
}
return c.pausedPromptApply(promptProgram, timeout)
}

func (c context) pausedPromptInUse(promptProgram command.Program, timeout time.Duration) (bool, error) {
const btnForce = "Force update"
const btnCancel = "Try again later"
promptJSONInput, err := json.Marshal(promptInput{
Expand Down Expand Up @@ -116,3 +123,32 @@ func (c context) pausedPrompt(promptProgram command.Program, timeout time.Durati
return false, fmt.Errorf("Unexpected button result: %s", result.Button)
}
}

func (c context) pausedPromptApply(promptProgram command.Program, timeout time.Duration) (bool, error) {
const btnApply = "Apply Update"
const btnCancel = "Cancel"
promptJSONInput, err := json.Marshal(promptInput{
Type: "generic",
Title: "Keybase Update",
Message: "Do you want to apply this update and restart Keybase?",
Buttons: []string{btnApply, btnCancel},
})
if err != nil {
return false, fmt.Errorf("Error generating input: %s", err)
}

var result promptInputResult
if err := command.ExecForJSON(promptProgram.Path, promptProgram.ArgsWith([]string{string(promptJSONInput)}), &result, timeout, c.log); err != nil {
return false, fmt.Errorf("Error running command: %s", err)
}

switch result.Button {
case btnApply:
return false, nil
case btnCancel:
// Cancel update
return true, nil
default:
return false, fmt.Errorf("Unexpected button result: %s", result.Button)
}
}
36 changes: 30 additions & 6 deletions keybase/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,58 @@ func TestPromptError(t *testing.T) {
Path: filepath.Join(os.Getenv("GOPATH"), "bin", "test"),
Args: []string{"err"},
}
cancel, err := testPausedPromptWithProgram(t, promptProgram, time.Second)
cancel, err := testPausedPromptWithProgram(t, promptProgram, true, time.Second)
assert.Error(t, err)
assert.False(t, cancel)

cancelApply, errApply := testPausedPromptWithProgram(t, promptProgram, false, time.Second)
assert.Error(t, errApply)
assert.False(t, cancelApply)
}

func testPausedPromptWithProgram(t *testing.T, promptProgram command.Program, timeout time.Duration) (bool, error) {
func testPausedPromptWithProgram(t *testing.T, promptProgram command.Program, inUse bool, timeout time.Duration) (bool, error) {
cfg, _ := testConfig(t)
ctx := newContext(cfg, testLog)
assert.NotNil(t, ctx)
return ctx.pausedPrompt(promptProgram, timeout)
return ctx.pausedPrompt(promptProgram, inUse, timeout)
}

func TestPausedPromptForce(t *testing.T) {
promptProgram := command.Program{
Path: filepath.Join(os.Getenv("GOPATH"), "bin", "test"),
Args: []string{"echo", `{"button": "Force update"}`},
}
cancel, err := testPausedPromptWithProgram(t, promptProgram, time.Second)
cancel, err := testPausedPromptWithProgram(t, promptProgram, true, time.Second)
assert.NoError(t, err)
assert.False(t, cancel)
}

func TestPausedPromptCancel(t *testing.T) {
func TestPausedPromptTryLater(t *testing.T) {
promptProgram := command.Program{
Path: filepath.Join(os.Getenv("GOPATH"), "bin", "test"),
Args: []string{"echo", `{"button": "Try again later"}`},
}
cancel, err := testPausedPromptWithProgram(t, promptProgram, time.Second)
cancel, err := testPausedPromptWithProgram(t, promptProgram, true, time.Second)
assert.NoError(t, err)
assert.True(t, cancel)
}

func TestPausedPromptApply(t *testing.T) {
promptProgram := command.Program{
Path: filepath.Join(os.Getenv("GOPATH"), "bin", "test"),
Args: []string{"echo", `{"button": "Apply Update"}`},
}
cancel, err := testPausedPromptWithProgram(t, promptProgram, false, time.Second)
assert.NoError(t, err)
assert.False(t, cancel)
}

func TestPausedPromptCancel(t *testing.T) {
promptProgram := command.Program{
Path: filepath.Join(os.Getenv("GOPATH"), "bin", "test"),
Args: []string{"echo", `{"button": "Cancel"}`},
}
cancel, err := testPausedPromptWithProgram(t, promptProgram, false, time.Second)
assert.NoError(t, err)
assert.True(t, cancel)
}
2 changes: 1 addition & 1 deletion updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Version is the updater version
const Version = "0.2.12"
const Version = "0.2.13"

// Updater knows how to find and apply updates
type Updater struct {
Expand Down