~emersion/public-inbox

chathistorysync: askpass: accept passwords from stdin v2 SUPERSEDED

Umar Getagazov: 1
 askpass: accept passwords from stdin

 3 files changed, 25 insertions(+), 23 deletions(-)
#819164 .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/34531/mbox | git am -3
Learn more about email & git

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

Also remove the dependency on golang.org/x/crypto.
---
Changes from v1:
- Fixed the bug with passwords that contain whitespace

 askpass.go | 39 +++++++++++++++++++++++++--------------
 go.mod     |  1 -
 go.sum     |  8 --------
 3 files changed, 25 insertions(+), 23 deletions(-)

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

import (
	"errors"
	"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 {
	if term.IsTerminal(int(os.Stdin.Fd())) {
		fmt.Printf("Password: ")
		password, err := term.ReadPassword(int(os.Stdin.Fd()))
		if err != nil {
			return "", err
		}
		defer f.Close()
		fmt.Printf("\n")
		return string(password), nil
	}
	fmt.Fprintf(os.Stderr, "%v: ", prompt)
	b, err := terminal.ReadPassword(fd)
	if err == nil {
		fmt.Fprintf(os.Stderr, "\n")

	var password []byte
	ch := [1]byte{0}
	fmt.Fprintln(os.Stderr, "Warning: Reading password from stdin.")
	for {
		n, err := os.Stdin.Read(ch[:])
		if err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			return "", err
		}
		if n > 0 && (ch[0] == '\n' || ch[0] == '\r') {
			break
		}
		password = append(password, ch[:n]...)
	}
	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 33s

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

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

✓ #819164 SUCCESS chathistorysync/patches/.build.yml https://builds.sr.ht/~emersion/job/819164
Instead of reading bytes one by one, I'd prefer to rely on bufio.Scanner,
just like the soju version does.