-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.go
81 lines (73 loc) · 1.38 KB
/
client.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
"yunion.io/x/executor/client"
"yunion.io/x/pkg/utils"
)
func Client() {
if len(socketPath) == 0 {
panic("socket path not provide")
}
client.Init(socketPath)
start()
}
func start() {
fmt.Print("# ")
reader := bufio.NewReader(os.Stdin)
var cmdRunning bool
var Sinput io.Writer
for {
content, _ := reader.ReadString('\n')
if cmdRunning {
io.WriteString(Sinput, content)
continue
}
input := strings.TrimSpace(strings.Trim(content, "\n"))
if utils.IsInStringArray(input, []string{"exit", "quit"}) {
return
} else if input == "" {
fmt.Println(time.Now().UTC().String())
fmt.Print("# ")
continue
}
is := utils.ArgsStringToArray(input)
c := client.Command(is[0], is[1:]...)
si, err := c.StdinPipe()
if err != nil {
panic(err)
}
Sinput = si
stdout, err := c.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := c.StderrPipe()
if err != nil {
panic(err)
}
if err := c.Start(); err != nil {
fmt.Printf("cmd %s start failed: %s\n", input, err)
fmt.Print("# ")
continue
}
go func() {
io.Copy(os.Stdout, stdout)
}()
go func() {
io.Copy(os.Stderr, stderr)
}()
cmdRunning = true
go func() {
if err := c.Wait(); err != nil {
fmt.Printf("cmd %s wait failed: %s\n", input, err)
}
cmdRunning = false
fmt.Print("# ")
}()
}
}