Last active
January 26, 2018 18:05
-
-
Save ericchiang/4f8a99e7ad072f932a40d45ad499c347 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
isatty "github.com/mattn/go-isatty" | |
) | |
func reExec(args ...string) error { | |
cmd := exec.Command("/proc/self/exe", args...) | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
cmd.Stdin = os.Stdin | |
if err := cmd.Run(); err != nil { | |
return fmt.Errorf("run command: %v", err) | |
} | |
return nil | |
} | |
// This program prints "Is Terminal" | |
func main() { | |
if len(os.Args) > 1 && os.Args[1] == "reexec" { | |
if isatty.IsTerminal(os.Stdout.Fd()) { | |
fmt.Fprintln(os.Stderr, "Is Terminal") | |
} else { | |
fmt.Fprintln(os.Stderr, "Is Not Terminal") | |
} | |
return | |
} | |
if err := reExec("reexec"); err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment