Skip to content

term: add examples for ReadPassword#4

Open
codesoap wants to merge 1 commit into
golang:masterfrom
codesoap:readpassword_examples
Open

term: add examples for ReadPassword#4
codesoap wants to merge 1 commit into
golang:masterfrom
codesoap:readpassword_examples

Conversation

@codesoap

@codesoap codesoap commented Jun 3, 2021

Copy link
Copy Markdown

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 golang/go#46164.

@google-cla google-cla Bot added the cla: yes label Jun 3, 2021
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 1712572) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/term/+/324829 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@codesoap codesoap force-pushed the readpassword_examples branch from 1712572 to b2824fc Compare June 3, 2021 19:58
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: b2824fc) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/term/+/324829 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@codesoap

codesoap commented Jun 4, 2021

Copy link
Copy Markdown
Author

@alexbrainman said in golang/go#46164 (comment) that he would rather use the Windows API for opening CONIN$, than to use 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.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Richard Ulmer:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/324829.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from TurkDevOps DevTurks-Team:

Patch Set 2: Code-Review+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/324829.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alex Brainman:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/324829.
After addressing review feedback, remember to publish your drafts!

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).
@codesoap codesoap force-pushed the readpassword_examples branch from b2824fc to 136cee7 Compare June 11, 2021 16:00
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 136cee7) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/term/+/324829 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Kadir Selçuk :

Patch Set 2: Code-Review+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/324829.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alex Brainman:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/324829.
After addressing review feedback, remember to publish your drafts!

@ghost

ghost commented Nov 8, 2021

Copy link
Copy Markdown

Thank you for contributing this example! Unfortunately when I try to use your technique in my program I get:

panic: open /dev/tty: no such device or address

@codesoap

codesoap commented Nov 9, 2021

Copy link
Copy Markdown
Author

@acnodal-tc What operating system are you using and which of the three examples did you try?

@ghost

ghost commented Nov 9, 2021

Copy link
Copy Markdown

I'm on Debian 11:

$ \cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

I integrated the code from ExampleReadPassword_unix() into my program, and it works great when I run it locally but if I run it using ssh it crashes:

~/work/Code/epic/epicctl$ ssh localhost "~/work/Code/epic/epicctl/epicctl create api-user user3 root" <<PASSWORD
password1
password1
PASSWORD
panic: open /dev/tty: no such device or address

@codesoap

codesoap commented Nov 9, 2021

Copy link
Copy Markdown
Author

@acnodal-tc The ReadPassword function is not designed for what you are trying to do. If you pass "command argument" to ssh ("~/work/Code/epic/epicctl/epicctl create api-user user3 root"), ssh will not open a terminal. ReadPassword is meant to read passwords from a terminal, not from a pipe.

@ghost

ghost commented Nov 9, 2021

Copy link
Copy Markdown

Ah, thank you. Perhaps this comment isn't accurate, then: "If standard input is not bound to the terminal, a password can still be read from it."

@codesoap

codesoap commented Nov 9, 2021

Copy link
Copy Markdown
Author

The "it" in "a password can still be read from it" refers to the terminal, not the standard input. To read from standard input, you don't need the golang.org/x/term package at all.

@ghost

ghost commented Nov 9, 2021

Copy link
Copy Markdown

Gotcha. Thank you!

ldemailly added a commit to ldemailly/term that referenced this pull request Aug 9, 2024
…ally add to the History (golang#4)

* Add AutoHistory(false) option (default remains true) to not automatically add to the History and let the caller of ReadLine() decide, for instance to only add validated commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants