SHOW:
|
|
- or go back to the newest paste.
| 1 | - | package main |
| 1 | + | package main |
| 2 | - | |
| 2 | + | |
| 3 | - | import ( |
| 3 | + | import ( |
| 4 | - | "os" |
| 4 | + | "os" |
| 5 | - | "os/exec" |
| 5 | + | "os/exec" |
| 6 | - | "io" |
| 6 | + | "io" |
| 7 | - | "strings" |
| 7 | + | "strings" |
| 8 | - | "github.com/kr/pty" |
| 8 | + | "github.com/kr/pty" |
| 9 | - | ) |
| 9 | + | ) |
| 10 | - | |
| 10 | + | |
| 11 | - | // This will be the equivalent of (echo id; cat) | /bin/sh but nicer |
| 11 | + | // This will be the equivalent of (echo id; cat) | /bin/sh but nicer |
| 12 | - | // because it realocates a tty after. |
| 12 | + | // because it realocates a tty after. |
| 13 | - | |
| 13 | + | |
| 14 | - | func main() { |
| 14 | + | func main() {
|
| 15 | - | buffer := strings.NewReader("id\n") |
| 15 | + | buffer := strings.NewReader("id\n")
|
| 16 | - | reader := io.MultiReader(buffer, os.Stdin) |
| 16 | + | reader := io.MultiReader(buffer, os.Stdin) |
| 17 | - | |
| 17 | + | |
| 18 | - | cmd := exec.Command("/bin/sh") |
| 18 | + | cmd := exec.Command("/bin/sh")
|
| 19 | - | tty, _ := pty.Start(cmd) |
| 19 | + | tty, _ := pty.Start(cmd) |
| 20 | - | defer tty.Close() |
| 20 | + | defer tty.Close() |
| 21 | - | |
| 21 | + | |
| 22 | - | go func() { |
| 22 | + | go func() {
|
| 23 | - | io.Copy(tty, reader) |
| 23 | + | io.Copy(tty, reader) |
| 24 | - | }() |
| 24 | + | }() |
| 25 | - | go func() { |
| 25 | + | go func() {
|
| 26 | - | io.Copy(os.Stdout, tty) |
| 26 | + | io.Copy(os.Stdout, tty) |
| 27 | - | }() |
| 27 | + | }() |
| 28 | - | |
| 28 | + | |
| 29 | - | cmd.Wait() |
| 29 | + | cmd.Wait() |
| 30 | } |