-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up util package into pkg/password, pkg/detach, pkg/version
Fixes: #1654 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
- Loading branch information
Showing
12 changed files
with
220 additions
and
197 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
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,57 @@ | ||
//go:build linux || darwin || freebsd | ||
// +build linux darwin freebsd | ||
|
||
package password | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
terminal "golang.org/x/term" | ||
) | ||
|
||
var ErrInterrupt = errors.New("interrupted") | ||
|
||
// Read reads a password from the terminal without echo. | ||
func Read(fd int) ([]byte, error) { | ||
// Store and restore the terminal status on interruptions to | ||
// avoid that the terminal remains in the password state | ||
// This is necessary as for https://github.com/golang/go/issues/31180 | ||
|
||
oldState, err := terminal.GetState(fd) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
|
||
type Buffer struct { | ||
Buffer []byte | ||
Error error | ||
} | ||
errorChannel := make(chan Buffer, 1) | ||
|
||
// SIGINT and SIGTERM restore the terminal, otherwise the no-echo mode would remain intact | ||
interruptChannel := make(chan os.Signal, 1) | ||
signal.Notify(interruptChannel, syscall.SIGINT, syscall.SIGTERM) | ||
defer func() { | ||
signal.Stop(interruptChannel) | ||
close(interruptChannel) | ||
}() | ||
go func() { | ||
for range interruptChannel { | ||
if oldState != nil { | ||
_ = terminal.Restore(fd, oldState) | ||
} | ||
errorChannel <- Buffer{Buffer: make([]byte, 0), Error: ErrInterrupt} | ||
} | ||
}() | ||
|
||
go func() { | ||
buf, err := terminal.ReadPassword(fd) | ||
errorChannel <- Buffer{Buffer: buf, Error: err} | ||
}() | ||
|
||
buf := <-errorChannel | ||
return buf.Buffer, buf.Error | ||
} |
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,21 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package password | ||
|
||
import ( | ||
terminal "golang.org/x/term" | ||
) | ||
|
||
// Read reads a password from the terminal. | ||
func Read(fd int) ([]byte, error) { | ||
oldState, err := terminal.GetState(fd) | ||
if err != nil { | ||
return make([]byte, 0), err | ||
} | ||
buf, err := terminal.ReadPassword(fd) | ||
if oldState != nil { | ||
_ = terminal.Restore(fd, oldState) | ||
} | ||
return buf, err | ||
} |
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
Oops, something went wrong.