is running interactive or not

424 views
Skip to first unread message

Rich

unread,
Jun 8, 2023, 2:19:26 PM6/8/23
to golang-nuts
Hi,

I have a program I am writing that stops and asks the user for input, in this case it's a passphrase used to encrypt output.

 I want the program to also be able to be used in a script, and if in a script use a predefined value as the passphrase. What I'd like to know is how to detect if running in a script or not.  I've tried something like this:
runPid := os.Getpid()
parPid := os.Getppid()
val := runPid - parPid //normally I check if runPid is > parPid in my code.
if val < 20 {
fmt.Println("Not running in a script")
}
This works, but only if the script ran quickly. Wondering if there is a better way to do this?

Kurtis Rader

unread,
Jun 8, 2023, 2:48:31 PM6/8/23
to Rich, golang-nuts
The easiest way to detect if your program is running interactively is to use https://github.com/mattn/go-isatty and test if stdin (fd 0) is connected to a terminal. The Elvish shell uses this function to handle both Unix and Windows:

// IsATTY determines whether the given file is a terminal.
func IsATTY(fd uintptr) bool {
    return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c8ae1be5-5a6b-45af-9249-ccdb02283d97n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Chris Burkert

unread,
Jun 8, 2023, 3:22:29 PM6/8/23
to Rich, golang-nuts
Hi, there are cases when this does not work. I tend to use a flag like -batch or -noninteractive to trigger the correct behavior from within scripts. Less magic, more control.

Rich

unread,
Jun 8, 2023, 7:52:58 PM6/8/23
to golang-nuts
Thank you Cris and Kurtis -- For this project I am going with the switch option -- but I have other programs that I am going to replace the os.Getpid and os.Getppid trick with go-isatty.

quin...@gmail.com

unread,
Jun 10, 2023, 6:35:17 AM6/10/23
to golang-nuts
there has been much discussion in the past about programs that modify their behaviour depending on what stdout is; http://harmful.cat-v.org/cat-v/unix_prog_design.pdf

i do to want to start a war, just suggest a different approach is available.

my suggestion would be to always expect a password from standard input and to do document this, suggesting, for non-interactive use people could do something like:

         echo password | application

using environment variables or passing passwords on the command line are rather leaky.

-Steve

Robert Engels

unread,
Jun 10, 2023, 8:30:27 AM6/10/23
to quin...@gmail.com, golang-nuts
No need to even think about this. Look at ssh and mimic their practices. It has been thoroughly audited. 

On Jun 10, 2023, at 5:35 AM, quin...@gmail.com <quin...@gmail.com> wrote:

there has been much discussion in the past about programs that modify their behaviour depending on what stdout is; http://harmful.cat-v.org/cat-v/unix_prog_design.pdf
Reply all
Reply to author
Forward
0 new messages