-
Notifications
You must be signed in to change notification settings - Fork 47
/
password.go
51 lines (43 loc) · 1.35 KB
/
password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package sh
import (
"fmt"
"os"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
// MustPassword gives a prompt and reads input until newlines without printing the input to screen.
// The prompt is written to stdout with `fmt.Print` unchanged.
// It panics on error.
func MustPassword(prompt string) string {
output, err := Password(prompt)
if err != nil {
panic(err)
}
return output
}
// Password prints a prompt and reads input until newlines without printing the input to screen.
// The prompt is written to stdout with `fmt.Print` unchanged.
func Password(prompt string) (string, error) {
fmt.Fprint(os.Stdout, prompt)
results, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
fmt.Fprintln(os.Stdout)
return string(results), nil
}
// Passwordf gives a prompt and reads input until newlines without printing the input to screen.
// The prompt is written to stdout with `fmt.Printf` unchanged.
func Passwordf(format string, args ...interface{}) (string, error) {
fmt.Fprintf(os.Stdout, format, args...)
results, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
fmt.Fprintln(os.Stdout)
return string(results), nil
}