-
Notifications
You must be signed in to change notification settings - Fork 630
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
feat(cli): promptSecret()
#3777
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @lambdalisue, thanks for the contribution! This is related to #3526. Can you please move the implementation to a new std/cli
top-level folder? We haven't confirmed the new sub-module, but I feel like it's likely to happen.
Just a little update - we're going ahead with the |
I'll apply that change on this weekend 👍 |
1b06e72
to
c82cad9
Compare
@iuioiua Done. Please re-review. Note that I rebased and force pushed. |
c82cad9
to
266a4a1
Compare
promptSecret
functionpromptSecret
function
promptSecret
functionpromptSecret()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work so far. Few things:
- I'm getting the following artefact on my macOS machine:
Password **********%
- Backspace isn't being correctly processed.
- Perhaps, we can open a new process using and simulate typing with
Deno.Command()
.
Ah, sounds nice. I'll try. |
3ab987a
to
205935c
Compare
I thought that the following implementation would handle the Ctrl sequence on the side of the process invoked by test.ts const cmd = new Deno.Command(Deno.execPath(), {
args: ["run", "./cli/prompt_secret_script.ts"],
stdin: "inherit",
stdout: "piped",
});
try {
Deno.stdin.setRaw(true, { cbreak: true });
const proc = cmd.spawn();
const { stdout } = await proc.output();
const result = (new TextDecoder()).decode(stdout);
console.log(`OUTPUT: ${result}`);
} finally {
Deno.stdin.setRaw(false);
} prompt_secret_script.ts const input = Deno.stdin;
const output = Deno.stdout;
const LF = "\n".charCodeAt(0);
const CR = "\r".charCodeAt(0);
const c = new Uint8Array(1);
const buf = [];
while (true) {
const n = input.readSync(c);
if (n === null || n === 0) {
break;
}
if (c[0] === CR || c[0] === LF) {
break;
}
buf.push(c[0]);
}
output.writeSync(new Uint8Array(buf)); |
I tried playing around with this and I couldn't get a test work. I would've thought that this could be accomplished by piping both input and output. Using |
I see. We want a stdin that doesn't display the input string, not a stdin that doesn't process it... I think it would make more sense to add a |
The function is similar to `prompt` but it print user's input as `*` to prevent password from being shown.
91d5fd1
to
55bf007
Compare
@iuioiua, I'm not certain whether my implementation works on Windows, but I've added control sequence handling to properly manage the backspace. I'm unsure if this approach is a good idea compared to my previous comment, but in case you find it preferable. CleanShot.2023-11-20.at.04.33.26.mp4Above video, I use Backspace (DEL in macOS) and Ctrl-H. |
Nice. Backspaces are working now. Frankly, I'm not sure which direction to go with this. @kt3k, do you have any advice on the implementation of this? Another issue I found. Once I hit enter upon entering my secret, the |
Actually, that's on purpose. I can make the behavior similar to |
I think it's ok to have the |
I made some adjustments to the interface to enable developers to customize the behavior. CleanShot.2023-11-21.at.21.25.02.mp4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good! Just one final nit. Also, @kt3k, any idea how we can test this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM for first pass! I think we can improve in following PRs by adding tests (however that's done) and perhaps simplifying logic. Thank you, @lambdalisue!
@ayame113, are you able to confirm whether this works correctly on Windows? IIRC, you've got a Windows machine. |
Hi @iuioiua ! I tried running the snippet below on windows. import { promptSecret } from "./cli/prompt_secret.ts"
const password = promptSecret("please input password")
console.log(password) Then the following error message was output.
When I looked at the
|
@ayame113 I see. I'll remove that option on Windows later but could you remove it from code and try if that works properly? |
@lambdalisue |
@ayame113 Thanks a lot. I removed the option on Windows 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM again. Nice. @kt3k, are you happy to have this even though there's no test method yet?
Not an ideal situation, but let's land this one for now as this seems manually tested enough. Another note: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The function is similar to
prompt
, but it prints the user's input as*
to prevent the password from being shown.Sometimes, I find the need for this type of function when creating CLI applications, so I decided to implement it. I'm not certain whether it should be included in the standard library, as it's somewhat challenging to test since it hooks into stdin. Therefore, I couldn't provide tests (or please let me know if there is a way).
Please inform me if this function shouldn't be included in the standard library, and I'll develop it as a third-party module. 👍