-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Ask for user confirmation before restoring
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cli | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
var UserDeclinedErr = errors.New("user declined") | ||
|
||
func Confirm(r io.Reader, defaultVal bool) (err error) { | ||
fmt.Print("Continue? ") | ||
if defaultVal { | ||
fmt.Print("[Y/n]: ") | ||
} else { | ||
fmt.Print("[y/N]: ") | ||
} | ||
|
||
buf := bufio.NewReader(r) | ||
var response string | ||
response, err = buf.ReadString('\n') | ||
if err != nil { | ||
return err | ||
} | ||
|
||
response = strings.ToLower(strings.TrimSpace(response)) | ||
switch response { | ||
case "yes", "y": | ||
return nil | ||
case "no", "n": | ||
return UserDeclinedErr | ||
} | ||
if defaultVal { | ||
return nil | ||
} else { | ||
return UserDeclinedErr | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func testConfirm(response string, defaultVal bool) error { | ||
temp := os.Stdout | ||
os.Stdout = nil | ||
result := Confirm(strings.NewReader(response + "\n"), defaultVal) | ||
os.Stdout = temp | ||
return result | ||
} | ||
|
||
func TestConfirm(t *testing.T) { | ||
type confirmTestCase struct { | ||
response string | ||
defaultVal bool | ||
error error | ||
} | ||
|
||
testCases := []confirmTestCase{ | ||
{response: "yes", defaultVal: true, error: nil}, | ||
{response: "yes", defaultVal: false, error: nil}, | ||
{response: "no", defaultVal: true, error: UserDeclinedErr}, | ||
{response: "no", defaultVal: false, error: UserDeclinedErr}, | ||
{response: "", defaultVal: true, error: nil}, | ||
{response: "", defaultVal: false, error: UserDeclinedErr}, | ||
} | ||
|
||
for key, testCase := range testCases { | ||
err := testConfirm(testCase.response, testCase.defaultVal) | ||
if err != testCase.error { | ||
t.Errorf("case %d: got %v; expected %v", key, err, testCase.error) | ||
} | ||
} | ||
} |