-
Notifications
You must be signed in to change notification settings - Fork 147
/
flush.go
122 lines (104 loc) · 4.16 KB
/
flush.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
117
118
119
120
121
122
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"
)
// FlushWithLabelSelector is an orchestration convenience method invokes the
// flush command using the specified label selector.
func FlushWithLabelSelector(labelSelector string, skipWait bool) error {
flushConfiguration.labelSelector = labelSelector
flushConfiguration.skipWait = skipWait
return flushMain(nil, nil)
}
func flushMain(command *cobra.Command, arguments []string) error {
// Create session selection specification.
selection := &selection.Selection{
All: flushConfiguration.all,
Specifications: arguments,
LabelSelector: flushConfiguration.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 flush method. The stream will close when the
// associated context is cancelled.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := sessionService.Flush(ctx)
if err != nil {
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to invoke flush")
}
// Send the initial request.
request := &synchronizationsvc.FlushRequest{
Selection: selection,
SkipWait: flushConfiguration.skipWait,
}
if err := stream.Send(request); err != nil {
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to send flush 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), "flush failed")
} else if err = response.EnsureValid(); err != nil {
return errors.Wrap(err, "invalid flush response received")
} else if response.Message == "" {
statusLinePrinter.Clear()
return nil
} else if response.Message != "" {
statusLinePrinter.Print(response.Message)
if err := stream.Send(&synchronizationsvc.FlushRequest{}); err != nil {
statusLinePrinter.BreakIfNonEmpty()
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "unable to send message response")
}
}
}
}
var flushCommand = &cobra.Command{
Use: "flush [<session>...]",
Short: "Force a synchronization cycle",
RunE: flushMain,
SilenceUsage: true,
}
var flushConfiguration struct {
// help indicates whether or not to show help information and exit.
help bool
// all indicates whether or not all sessions should be flushed.
all bool
// labelSelector encodes a label selector to be used in identifying which
// sessions should be paused.
labelSelector string
// skipWait indicates whether or not the flush operation should block until
// a synchronization cycle completes for each sesion requested.
skipWait bool
}
func init() {
// Grab a handle for the command line flags.
flags := flushCommand.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(&flushConfiguration.help, "help", "h", false, "Show help information")
// Wire up flush flags.
flags.BoolVarP(&flushConfiguration.all, "all", "a", false, "Flush all sessions")
flags.StringVar(&flushConfiguration.labelSelector, "label-selector", "", "Flush sessions matching the specified label selector")
flags.BoolVar(&flushConfiguration.skipWait, "skip-wait", false, "Avoid waiting for the resulting synchronization cycle(s) to complete")
}