-
Notifications
You must be signed in to change notification settings - Fork 147
/
pause.go
116 lines (98 loc) · 3.78 KB
/
pause.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sync
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/mutagen-io/mutagen/cmd"
"github.com/mutagen-io/mutagen/cmd/mutagen/daemon"
"github.com/mutagen-io/mutagen/pkg/grpcutil"
"github.com/mutagen-io/mutagen/pkg/selection"
synchronizationsvc "github.com/mutagen-io/mutagen/pkg/service/synchronization"
)
// PauseWithLabelSelector is an orchestration convenience method invokes the
// pause command using the specified label selector.
func PauseWithLabelSelector(labelSelector string) error {
pauseConfiguration.labelSelector = labelSelector
return pauseMain(nil, nil)
}
func pauseMain(command *cobra.Command, arguments []string) error {
// Create session selection specification.
selection := &selection.Selection{
All: pauseConfiguration.all,
Specifications: arguments,
LabelSelector: pauseConfiguration.labelSelector,
}
if err := selection.EnsureValid(); err != nil {
return errors.Wrap(err, "invalid session selection specification")
}
// Connect to the daemon and defer closure of the connection.
daemonConnection, err := daemon.CreateClientConnection(true, true)
if err != nil {
return errors.Wrap(err, "unable to connect to daemon")
}
defer daemonConnection.Close()
// Create a session service client.
sessionService := synchronizationsvc.NewSynchronizationClient(daemonConnection)
// Invoke the session pause method. The stream will close when the
// associated context is cancelled.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := sessionService.Pause(ctx)
if err != nil {
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to invoke pause")
}
// Send the initial request.
request := &synchronizationsvc.PauseRequest{
Selection: selection,
}
if err := stream.Send(request); err != nil {
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to send pause request")
}
// Create a status line printer.
statusLinePrinter := &cmd.StatusLinePrinter{}
// Receive and process responses until we're done.
for {
if response, err := stream.Recv(); err != nil {
statusLinePrinter.BreakIfNonEmpty()
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "pause failed")
} else if err = response.EnsureValid(); err != nil {
return errors.Wrap(err, "invalid pause response received")
} else if response.Message == "" {
statusLinePrinter.Clear()
return nil
} else if response.Message != "" {
statusLinePrinter.Print(response.Message)
if err := stream.Send(&synchronizationsvc.PauseRequest{}); err != nil {
statusLinePrinter.BreakIfNonEmpty()
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to send message response")
}
}
}
}
var pauseCommand = &cobra.Command{
Use: "pause [<session>...]",
Short: "Pause a synchronization session",
RunE: pauseMain,
SilenceUsage: true,
}
var pauseConfiguration struct {
// help indicates whether or not to show help information and exit.
help bool
// all indicates whether or not all sessions should be paused.
all bool
// labelSelector encodes a label selector to be used in identifying which
// sessions should be paused.
labelSelector string
}
func init() {
// Grab a handle for the command line flags.
flags := pauseCommand.Flags()
// Disable alphabetical sorting of flags in help output.
flags.SortFlags = false
// Manually add a help flag to override the default message. Cobra will
// still implement its logic automatically.
flags.BoolVarP(&pauseConfiguration.help, "help", "h", false, "Show help information")
// Wire up pause flags.
flags.BoolVarP(&pauseConfiguration.all, "all", "a", false, "Pause all sessions")
flags.StringVar(&pauseConfiguration.labelSelector, "label-selector", "", "Pause sessions matching the specified label selector")
}