-
Notifications
You must be signed in to change notification settings - Fork 147
/
list.go
217 lines (186 loc) · 6.06 KB
/
list.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package sync
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/fatih/color"
"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"
"github.com/mutagen-io/mutagen/pkg/synchronization"
"github.com/mutagen-io/mutagen/pkg/synchronization/core"
"github.com/mutagen-io/mutagen/pkg/url"
)
func formatPath(path string) string {
if path == "" {
return "<root>"
}
return path
}
func formatConnectionStatus(connected bool) string {
if connected {
return "Connected"
}
return "Disconnected"
}
func printEndpointStatus(name string, url *url.URL, connected bool, problems []*core.Problem) {
// Print header.
fmt.Printf("%s:\n", name)
// Print URL if we're not in long-listing mode (otherwise it will be
// printed elsewhere).
if !listConfiguration.long {
fmt.Println("\tURL:", url.Format("\n\t\t"))
}
// Print connection status.
fmt.Printf("\tConnection state: %s\n", formatConnectionStatus(connected))
// Print problems, if any.
if len(problems) > 0 {
color.Red("\tProblems:\n")
for _, p := range problems {
color.Red("\t\t%s: %v\n", formatPath(p.Path), p.Error)
}
}
}
func printSessionStatus(state *synchronization.State) {
// Print status.
statusString := state.Status.Description()
if state.Session.Paused {
statusString = color.YellowString("[Paused]")
}
fmt.Fprintln(color.Output, "Status:", statusString)
// Print the last error, if any.
if state.LastError != "" {
color.Red("Last error: %s\n", state.LastError)
}
}
func formatEntryKind(entry *core.Entry) string {
if entry == nil {
return "<non-existent>"
} else if entry.Kind == core.EntryKind_Directory {
return "Directory"
} else if entry.Kind == core.EntryKind_File {
if entry.Executable {
return fmt.Sprintf("Executable File (%x)", entry.Digest)
}
return fmt.Sprintf("File (%x)", entry.Digest)
} else if entry.Kind == core.EntryKind_Symlink {
return fmt.Sprintf("Symbolic Link (%s)", entry.Target)
} else {
return "<unknown>"
}
}
func printConflicts(conflicts []*core.Conflict) {
// Print the header.
color.Red("Conflicts:\n")
// Print conflicts.
for i, c := range conflicts {
// Print the alpha changes.
for _, a := range c.AlphaChanges {
color.Red(
"\t(alpha) %s (%s -> %s)\n",
formatPath(a.Path),
formatEntryKind(a.Old),
formatEntryKind(a.New),
)
}
// Print the beta changes.
for _, b := range c.BetaChanges {
color.Red(
"\t(beta) %s (%s -> %s)\n",
formatPath(b.Path),
formatEntryKind(b.Old),
formatEntryKind(b.New),
)
}
// If we're not on the last conflict, print a newline.
if i < len(conflicts)-1 {
fmt.Println()
}
}
}
// ListWithLabelSelector is an orchestration convenience method that invokes the
// list command using the specified label selector.
func ListWithLabelSelector(labelSelector string, long bool) error {
listConfiguration.long = long
listConfiguration.labelSelector = labelSelector
return listMain(nil, nil)
}
func listMain(command *cobra.Command, arguments []string) error {
// Create session selection specification.
selection := &selection.Selection{
All: len(arguments) == 0 && listConfiguration.labelSelector == "",
Specifications: arguments,
LabelSelector: listConfiguration.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 list.
request := &synchronizationsvc.ListRequest{
Selection: selection,
}
response, err := sessionService.List(context.Background(), request)
if err != nil {
return errors.Wrap(grpcutil.PeelAwayRPCErrorLayer(err), "list failed")
} else if err = response.EnsureValid(); err != nil {
return errors.Wrap(err, "invalid list response received")
}
// Handle output based on whether or not any sessions were returned.
if len(response.SessionStates) > 0 {
for _, state := range response.SessionStates {
fmt.Println(cmd.DelimiterLine)
printSession(state, listConfiguration.long)
printEndpointStatus("Alpha", state.Session.Alpha, state.AlphaConnected, state.AlphaProblems)
printEndpointStatus("Beta", state.Session.Beta, state.BetaConnected, state.BetaProblems)
printSessionStatus(state)
if len(state.Conflicts) > 0 {
printConflicts(state.Conflicts)
}
}
fmt.Println(cmd.DelimiterLine)
} else {
fmt.Println(cmd.DelimiterLine)
fmt.Println("No sessions found")
fmt.Println(cmd.DelimiterLine)
}
// Success.
return nil
}
var listCommand = &cobra.Command{
Use: "list [<session>...]",
Short: "List existing synchronization sessions and their statuses",
RunE: listMain,
SilenceUsage: true,
}
var listConfiguration struct {
// help indicates whether or not to show help information and exit.
help bool
// long indicates whether or not to use long-format listing.
long 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 := listCommand.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(&listConfiguration.help, "help", "h", false, "Show help information")
// Wire up list flags.
flags.BoolVarP(&listConfiguration.long, "long", "l", false, "Show detailed session information")
flags.StringVar(&listConfiguration.labelSelector, "label-selector", "", "List sessions matching the specified label selector")
}