|
@@ -4,6 +4,7 @@ import (
|
|
|
"bytes"
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
byte2 "git.nspix.com/golang/micro/helper/pool/byte"
|
|
|
"git.nspix.com/golang/micro/helper/unsafestr"
|
|
@@ -177,6 +178,40 @@ func (client *Client) Run() (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func ExecuteContext(ctx context.Context, conn net.Conn, cmd string) (s string, err error) {
|
|
|
+ if ctx == nil {
|
|
|
+ ctx = context.Background()
|
|
|
+ }
|
|
|
+ cli := &Client{
|
|
|
+ conn: conn,
|
|
|
+ context: ctx,
|
|
|
+ readyChan: make(chan struct{}),
|
|
|
+ completerChan: make(chan *Frame),
|
|
|
+ responseChan: make(chan *Frame, 1),
|
|
|
+ }
|
|
|
+ if err = cli.writePack(&Frame{
|
|
|
+ Type: PacketTypeEcho,
|
|
|
+ }); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ frame := &Frame{}
|
|
|
+ if frame, err = readFrame(conn); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err = cli.writePack(&Frame{Type: PacketTypeData, Data: []byte(cmd)}); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if frame, err = readFrame(conn); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if frame.Error != "" {
|
|
|
+ err = errors.New(frame.Error)
|
|
|
+ } else {
|
|
|
+ s = string(frame.Data)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func OpenInteractive(ctx context.Context, conn net.Conn) (err error) {
|
|
|
if ctx == nil {
|
|
|
ctx = context.Background()
|