-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcmdproxy.go
87 lines (77 loc) · 1.52 KB
/
cmdproxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
)
// main runs the bc Linux command and proxies stdin input to it... all in one method
func main() {
cmd := exec.Command("pianobar")
in, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
out, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
//
// Capture standard error and print it
//
go func() {
defer stderr.Close()
errReader := bufio.NewReader(stderr)
errScanner := bufio.NewScanner(errReader)
for errScanner.Scan() {
fmt.Println(errScanner.Text())
}
}()
//
// Capture standard input and pass it to the command
//
go func() {
defer in.Close()
consolereader := bufio.NewReader(os.Stdin)
for {
//fmt.Print("> ")
// inputText, err := consolereader.ReadString('\n') // this will wait for user input
b, err := consolereader.ReadByte() // this will wait for user input
if err != nil {
if err != io.EOF {
panic(err)
}
} else {
_, err := in.Write([]byte{b})
if err != nil {
panic(err)
}
//fmt.Fprintln(in, inputText)
}
}
}()
//
// Start the process
//
if err = cmd.Start(); err != nil {
panic(err)
}
//
// Capture standard output and print it
//
go func() {
defer out.Close()
reader := bufio.NewReader(out)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
fmt.Println("Enter bc calculations (like 5*4) and press enter. Enter 'quit' to exit")
cmd.Wait()
}