-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/oasis-node/cmd/common: Add
Isatty
and use it
Having to use a command line flag when not running in an interactive terminal is somewhat silly, and recursively calling the Fisher Price Baby's First Interactive Prompt routine till the command exhauses the heap and crashes when not run in a tty is even sillier.
- Loading branch information
Showing
3 changed files
with
58 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package common | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
// Isatty returns true iff the provided file descriptor is a terminal. | ||
func Isatty(fd uintptr) bool { | ||
var attrs syscall.Termios | ||
|
||
// This could examine the error more specifically to see if | ||
// something really strange is going on since we expect it | ||
// to return 0 or ENOTTY all the time, but, "the messed up | ||
// thing the user passed in that makes it complain" also | ||
// is not a tty. | ||
// | ||
// And yes, this is the standard way of doing this, see your | ||
// libc implementation of choice. | ||
_, _, errno := syscall.Syscall6( | ||
syscall.SYS_IOCTL, | ||
fd, | ||
syscall.TCGETS, | ||
uintptr(unsafe.Pointer(&attrs)), | ||
0, | ||
0, | ||
0, | ||
) | ||
|
||
return errno == 0 | ||
} |