forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request twpayne#1322 from twpayne/fix-1311
Use terminal to read password on Windows
- Loading branch information
Showing
8 changed files
with
116 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//+build !windows | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func (c *Config) readPassword(prompt string) (string, error) { | ||
if c.noTTY { | ||
return c.readLine(prompt) | ||
} | ||
|
||
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer func() { | ||
_ = tty.Close() | ||
}() | ||
if _, err := tty.Write([]byte(prompt)); err != nil { | ||
return "", err | ||
} | ||
password, err := term.ReadPassword(int(tty.Fd())) | ||
if err != nil && !errors.Is(err, term.ErrPasteIndicator) { | ||
return "", err | ||
} | ||
if _, err := tty.Write([]byte{'\n'}); err != nil { | ||
return "", err | ||
} | ||
return string(password), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/sys/windows" | ||
"golang.org/x/term" | ||
) | ||
|
||
func (c *Config) readPassword(prompt string) (string, error) { | ||
if c.noTTY { | ||
return c.readLine(prompt) | ||
} | ||
|
||
name, err := windows.UTF16PtrFromString("CONIN$") | ||
if err != nil { | ||
return "", err | ||
} | ||
handle, err := windows.CreateFile(name, windows.GENERIC_READ|windows.GENERIC_WRITE, windows.FILE_SHARE_READ, nil, windows.OPEN_EXISTING, 0, 0) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer func() { | ||
_ = windows.CloseHandle(handle) | ||
}() | ||
//nolint:forbidigo | ||
fmt.Print(prompt) | ||
password, err := term.ReadPassword(int(handle)) | ||
if err != nil { | ||
return "", err | ||
} | ||
//nolint:forbidigo | ||
fmt.Println("") | ||
return string(password), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *Config) newInternalTestCmd() *cobra.Command { | ||
internalTestCmd := &cobra.Command{ | ||
Use: "internal-test", | ||
Short: "Expose functionality for testing", | ||
Hidden: true, | ||
} | ||
|
||
internalTestReadPasswordCmd := &cobra.Command{ | ||
Use: "read-password", | ||
Short: "Read a password", | ||
RunE: c.runInternalTestReadPasswordCmd, | ||
} | ||
internalTestCmd.AddCommand(internalTestReadPasswordCmd) | ||
|
||
return internalTestCmd | ||
} | ||
|
||
func (c *Config) runInternalTestReadPasswordCmd(cmd *cobra.Command, args []string) error { | ||
password, err := c.readPassword("Password? ") | ||
if err != nil { | ||
return err | ||
} | ||
return c.writeOutputString(password + "\n") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters