-
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.
Merge pull request #1 from kemokemo/add-delete-flag
add delete flag
- Loading branch information
Showing
7 changed files
with
323 additions
and
12 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
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,32 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// ** original code: https://gist.github.com/r0l1/3dcbb0c8f6cfe9c66ab8008f55f8f28b | ||
// askForConfirmation returns whether or not the user agrees to the process. | ||
func askForConfirmation(s string, in io.Reader, out io.Writer, retry int) (bool, error) { | ||
reader := bufio.NewReader(in) | ||
|
||
for ; retry > 0; retry-- { | ||
fmt.Fprintf(out, "%s [y/n]: ", s) | ||
|
||
res, err := reader.ReadString('\n') | ||
if err != nil { | ||
return false, fmt.Errorf("failed to read user input: %s", err) | ||
} | ||
|
||
res = strings.ToLower(strings.TrimSpace(res)) | ||
if res == "y" || res == "yes" { | ||
return true, nil | ||
} else if res == "n" || res == "no" { | ||
return false, nil | ||
} | ||
} | ||
|
||
return false, fmt.Errorf("retry count has been reached") | ||
} |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func Test_askForConfirmation(t *testing.T) { | ||
type args struct { | ||
s string | ||
in io.Reader | ||
retry int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
wantOut string | ||
wantErr bool | ||
}{ | ||
{"yes", args{"Are you sure to delete it?", bytes.NewBufferString("y\n"), 2}, true, "Are you sure to delete it? [y/n]: ", false}, | ||
{"no", args{"Are you sure to delete it?", bytes.NewBufferString("n\n"), 2}, false, "Are you sure to delete it? [y/n]: ", false}, | ||
{"no EOF", args{"Are you sure to delete it?", bytes.NewBufferString("hoge"), 2}, false, "Are you sure to delete it? [y/n]: ", true}, | ||
{"invalid input", args{"Are you sure to delete it?", bytes.NewBufferString("foo\nbar\n"), 2}, false, "Are you sure to delete it? [y/n]: Are you sure to delete it? [y/n]: ", true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out := &bytes.Buffer{} | ||
got, err := askForConfirmation(tt.args.s, tt.args.in, out, tt.args.retry) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("askForConfirmation() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("askForConfirmation() = %v, want %v", got, tt.want) | ||
} | ||
if gotOut := out.String(); gotOut != tt.wantOut { | ||
t.Errorf("askForConfirmation() = %v, want %v", gotOut, tt.wantOut) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.