Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

term: add examples for ReadPassword #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

codesoap
Copy link

@codesoap codesoap commented Jun 3, 2021

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
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

@gopherbot
Copy link
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
Copy link
Author

codesoap commented Jun 4, 2021

@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
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
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
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).
@gopherbot
Copy link
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
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
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
Copy link

ghost commented Nov 8, 2021

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
Copy link
Author

codesoap commented Nov 9, 2021

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

@ghost
Copy link

ghost commented Nov 9, 2021

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
Copy link
Author

codesoap commented Nov 9, 2021

@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
Copy link

ghost commented Nov 9, 2021

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
Copy link
Author

codesoap commented Nov 9, 2021

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
Copy link

ghost commented Nov 9, 2021

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