Gerrit Bot has uploaded this change for review.
term: add examples for ReadPassword
These examples demonstrate how to use ReadPassword for interactive password input from the terminal. They highlight how to use the function, even if standard input is not a terminal (e.g. when it is a pipe). The example for Windows is especially interesting, because there seems to be a bug where CONIN$ cannot be used unless opened with the write flag.
This contribution was suggested to me at https://github.com/golang/go/issues/46164.
Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
GitHub-Last-Rev: 17125723315e87d2876e5781e956d67a5a21656b
GitHub-Pull-Request: golang/term#4
---
A read_password_test.go
A read_password_unix_test.go
A read_password_windows.go
3 files changed, 82 insertions(+), 0 deletions(-)
diff --git a/read_password_test.go b/read_password_test.go
new file mode 100644
index 0000000..b1eb3cd
--- /dev/null
+++ b/read_password_test.go
@@ -0,0 +1,21 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package term_test
+
+import (
+ "fmt"
+ "os"
+
+ "golang.org/x/term"
+)
+
+func ExampleReadPassword() {
+ fmt.Print("Enter your password: ")
+ p, err := term.ReadPassword(int(os.Stdin.Fd()))
+ if err != nil {
+ panic(err)
+ }
+ fmt.Printf("\nYou entered '%s'\n", string(p))
+}
diff --git a/read_password_unix_test.go b/read_password_unix_test.go
new file mode 100644
index 0000000..82170bf
--- /dev/null
+++ b/read_password_unix_test.go
@@ -0,0 +1,30 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !windows
+
+package term_test
+
+import (
+ "fmt"
+ "os"
+
+ "golang.org/x/term"
+)
+
+func ExampleReadPassword_unix() {
+ // If standard input is not bound to the terminal, a password can
+ // still be read from it.
+ tty, err := os.Open("/dev/tty")
+ if err != nil {
+ panic(err)
+ }
+ defer tty.Close()
+ fmt.Print("Enter your password: ")
+ p, err := term.ReadPassword(int(tty.Fd()))
+ if err != nil {
+ panic(err)
+ }
+ fmt.Printf("\nYou entered '%s'\n", string(p))
+}
diff --git a/read_password_windows.go b/read_password_windows.go
new file mode 100644
index 0000000..a7bca3d
--- /dev/null
+++ b/read_password_windows.go
@@ -0,0 +1,31 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build windows
+
+package term_test
+
+import (
+ "fmt"
+ "os"
+
+ "golang.org/x/term"
+)
+
+func ExampleReadPassword_windows() {
+ // If standard input is not bound to the terminal, a password can
+ // still be read from it. OpenFile must be used with the write flag
+ // for CONIN$.
+ tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
+ if err != nil {
+ panic(err)
+ }
+ defer tty.Close()
+ fmt.Print("Enter your password: ")
+ p, err := term.ReadPassword(int(tty.Fd()))
+ if err != nil {
+ panic(err)
+ }
+ fmt.Printf("\nYou entered '%s'\n", string(p))
+}
To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.
Gerrit Bot uploaded patch set #2 to this change.
term: add examples for ReadPassword
These examples demonstrate how to use ReadPassword for interactive password input from the terminal. They highlight how to use the function, even if standard input is not a terminal (e.g. when it is a pipe). The example for Windows is especially interesting, because there seems to be a bug where CONIN$ cannot be used unless opened with the write flag.
This contribution was suggested to me at https://github.com/golang/go/issues/46164.
Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
GitHub-Last-Rev: b2824fc2bbc1d05a1bd8396cbbb3fc74aebfb4d9
GitHub-Pull-Request: golang/term#4
---
A read_password_test.go
A read_password_unix_test.go
A read_password_windows_test.go
3 files changed, 82 insertions(+), 0 deletions(-)
To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.
1 comment:
Patchset:
Alex Brainman said in https://github.com/golang/go/issues/46164#issuecomment-854482868 that he would rather use the Windows API for opening CONIN$, than to use os.OpenFile with the os.O_RDWR flag. I have no preference, but am open to changing the Windows example, if using the Windows API directly is preferred.
To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.
Patch set 2:Code-Review +1
Attention is currently required from: Kadir Selçuk , Richard Ulmer.
1 comment:
File read_password_windows_test.go:
Patch Set #2, Line 20: tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
I do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.
If you disagree, then you you should submit proposal to document this behavior
To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Kadir Selçuk , Richard Ulmer.
Gerrit Bot uploaded patch set #3 to this change.
term: add examples for ReadPassword
These examples demonstrate how to use ReadPassword for interactive password input from the terminal. They highlight how to use the function, even if standard input is not a terminal (e.g. when it is a pipe). The example for Windows is especially interesting, because there seems to be a bug where CONIN$ cannot be used unless opened with the write flag.
This contribution was suggested to me at https://github.com/golang/go/issues/46164.
Change-Id: Ib9bc595f57025f4df258d6724ea512f03894889f
GitHub-Last-Rev: 136cee71c27ed96f94f9b04834e593a537af210a
GitHub-Pull-Request: golang/term#4
---
A read_password_test.go
A read_password_unix_test.go
A read_password_windows_test.go
3 files changed, 91 insertions(+), 0 deletions(-)
To view, visit change 324829. To unsubscribe, or for help writing mail filters, visit settings.
would be nice to merge this is still accurate
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
would be nice to merge this is still accurate
Fair enough.
I rebased this CL and run tests and they pass.
If it looks good to you (since you are the user of this package), I will give +2 and hopefully we can merge this CL.
Thank you.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
fmt.Printf("\nYou entered '%s'\n", string(p))
it's just a test/example but maybe %q instead of '%s' would be better as control characters are accepted (for better or worse) by ReadPassword
I do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.
If you disagree, then you you should submit proposal to document this behavior
I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such
my bad for missing your comment earlier, I was going through the open PRs on github and it seems innocous
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
fmt.Printf("\nYou entered '%s'\n", string(p))
it's just a test/example but maybe %q instead of '%s' would be better as control characters are accepted (for better or worse) by ReadPassword
@ldem...@gmail.com this CL is more than 4 years old. I doubt the CL authors would be there to address your comments.
tty, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
Laurent DemaillyI do not want to add this example, because this code was not designed to work. Therefore it can break any time in the future. And it would be impossible to write a test for it.
If you disagree, then you you should submit proposal to document this behavior
I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such
my bad for missing your comment earlier, I was going through the open PRs on github and it seems innocous
I actually agree this one is quite hairy/ugly - probably better as a stackoverflow "trick" than a godoc ? or we would need to hide away this as "windows.OpenConsoleIn()" or some such
Like I said earlier it is your call, if we should submit this. I am unfamiliar with this code and have no time to investigate it properly.
my bad for missing your comment earlier, ...
No problem.