~emersion/public-inbox

chathistorysync: askpass: accept passwords from stdin v4 APPLIED

Umar Getagazov: 1
 askpass: accept passwords from stdin

 3 files changed, 28 insertions(+), 24 deletions(-)
#819237 .build.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/34537/mbox | git am -3
Learn more about email & git

[PATCH chathistorysync v4] askpass: accept passwords from stdin Export this patch

Also remove the dependency on golang.org/x/crypto.
---
Changes since v4:
- Copy the soju readPassword routine instead of acting smart
- Actually use the passed prompt string

 askpass.go | 43 ++++++++++++++++++++++++++++---------------
 go.mod     |  1 -
 go.sum     |  8 --------
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/askpass.go b/askpass.go
index 40f0b3a..bb70d9e 100644
--- a/askpass.go
+++ b/askpass.go
@@ -1,29 +1,42 @@
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"

	"golang.org/x/crypto/ssh/terminal"
	"golang.org/x/term"
)

func askPass(prompt string) (string, error) {
	f := os.Stdin
	fd := int(f.Fd())
	if !term.IsTerminal(fd) {
		// This can happen if stdin is used for piping data
		// TODO: the following assumes Unix
		var err error
		if f, err = os.Open("/dev/tty"); err != nil {
	var password []byte
	var err error
	fd := int(os.Stdin.Fd())

	if term.IsTerminal(fd) {
		fmt.Printf("%s: ", prompt)
		password, err = term.ReadPassword(int(os.Stdin.Fd()))
		if err != nil {
			return "", err
		}
		defer f.Close()
	}
	fmt.Fprintf(os.Stderr, "%v: ", prompt)
	b, err := terminal.ReadPassword(fd)
	if err == nil {
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Println()
	} else {
		fmt.Fprintf(os.Stderr, "Warning: Reading password from stdin.\n")
		// TODO: the buffering messes up repeated calls to readPassword
		scanner := bufio.NewScanner(os.Stdin)
		if !scanner.Scan() {
			if err := scanner.Err(); err != nil {
				return "", err
			}
			return "", io.ErrUnexpectedEOF
		}
		password = scanner.Bytes()

		if len(password) == 0 {
			return "", fmt.Errorf("zero length password")
		}
	}
	return string(b), err

	return string(password), nil
}
diff --git a/go.mod b/go.mod
index d34b4db..51eb8b7 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,6 @@ module git.sr.ht/~emersion/chathistorysync
go 1.17

require (
	golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce
	golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
	gopkg.in/irc.v3 v3.1.4
)
diff --git a/go.sum b/go.sum
index 278637b..04d1031 100644
--- a/go.sum
+++ b/go.sum
@@ -5,19 +5,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce h1:Roh6XWxHFKrPgC/EQhVubSAGQ6Ozk6IdxHSzt1mR0EI=
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/irc.v3 v3.1.4 h1:DYGMRFbtseXEh+NadmMUFzMraqyuUj4I3iWYFEzDZPc=
gopkg.in/irc.v3 v3.1.4/go.mod h1:shO2gz8+PVeS+4E6GAny88Z0YVVQSxQghdrMVGQsR9s=
-- 
2.32.1 (Apple Git-133)
chathistorysync/patches/.build.yml: SUCCESS in 1m35s

[askpass: accept passwords from stdin][0] v4 from [Umar Getagazov][1]

[0]: https://lists.sr.ht/~emersion/public-inbox/patches/34537
[1]: mailto:umar@handlerug.me

✓ #819237 SUCCESS chathistorysync/patches/.build.yml https://builds.sr.ht/~emersion/job/819237
Pushed, thanks!