forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
52 lines (45 loc) · 1.4 KB
/
wrapper.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtctlclient
import (
"errors"
"fmt"
"io"
"time"
"golang.org/x/net/context"
logutilpb "github.com/youtube/vitess/go/vt/proto/logutil"
)
// RunCommandAndWait executes a single command on a given vtctld and blocks until the command did return or timed out.
// Output from vtctld is streamed as logutilpb.Event messages which
// have to be consumed by the caller who has to specify a "recv" function.
func RunCommandAndWait(ctx context.Context, server string, args []string, dialTimeout, actionTimeout time.Duration, recv func(*logutilpb.Event)) error {
if recv == nil {
return errors.New("No function closure for Event stream specified")
}
// create the client
client, err := New(server, dialTimeout)
if err != nil {
return fmt.Errorf("Cannot dial to server %v: %v", server, err)
}
defer client.Close()
// run the command
ctx, cancel := context.WithTimeout(context.Background(), actionTimeout)
defer cancel()
stream, err := client.ExecuteVtctlCommand(ctx, args, actionTimeout)
if err != nil {
return fmt.Errorf("Cannot execute remote command: %v", err)
}
// stream the result
for {
e, err := stream.Recv()
switch err {
case nil:
recv(e)
case io.EOF:
return nil
default:
return fmt.Errorf("Remote error: %v", err)
}
}
}