diff --git a/helper/password/password.go b/helper/password/password.go index 102fbe802332..641da8239e74 100644 --- a/helper/password/password.go +++ b/helper/password/password.go @@ -8,6 +8,7 @@ import ( "io" "os" "os/signal" + "strings" ) var ErrInterrupted = errors.New("interrupted") @@ -34,7 +35,7 @@ func Read(f *os.File) (string, error) { case <-ch: return "", ErrInterrupted case <-doneCh: - return result, resultErr + return removeiTermDelete(result), resultErr } } @@ -62,3 +63,7 @@ func readline(f *os.File) (string, error) { return string(resultBuf), nil } + +func removeiTermDelete(input string) string { + return strings.TrimPrefix(input, "\x20\x7f") +} diff --git a/helper/password/password_test.go b/helper/password/password_test.go new file mode 100644 index 000000000000..c44526d7ab21 --- /dev/null +++ b/helper/password/password_test.go @@ -0,0 +1,27 @@ +package password + +import "testing" + +type testCase struct { + name string + input string + expected string +} + +func TestRemoveiTermDelete(t *testing.T) { + var tests = []testCase{ + {"NoDelete", "TestingStuff", "TestingStuff"}, + {"SingleDelete", "Testing\x7fStuff", "Testing\x7fStuff"}, + {"DeleteFirst", "\x7fTestingStuff", "\x7fTestingStuff"}, + {"DoubleDelete", "\x7f\x7fTestingStuff", "\x7f\x7fTestingStuff"}, + {"SpaceFirst", "\x20TestingStuff", "\x20TestingStuff"}, + {"iTermDelete", "\x20\x7fTestingStuff", "TestingStuff"}, + } + + for _, test := range tests { + result := removeiTermDelete(test.input) + if result != test.expected { + t.Errorf("Test %s failed, input: '%s', expected: '%s', output: '%s'", test.name, test.input, test.expected, result) + } + } +}