-
Notifications
You must be signed in to change notification settings - Fork 147
/
list.go
148 lines (124 loc) · 4.45 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
package forward
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/forwarding"
"github.com/mutagen-io/mutagen/pkg/grpcutil"
"github.com/mutagen-io/mutagen/pkg/selection"
forwardingsvc "github.com/mutagen-io/mutagen/pkg/service/forwarding"
"github.com/mutagen-io/mutagen/pkg/url"
)
func formatConnectionStatus(connected bool) string {
if connected {
return "Connected"
}
return "Disconnected"
}
func printEndpointStatus(name string, url *url.URL, connected bool) {
// 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))
}
func printSessionStatus(state *forwarding.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)
}
}
// 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 := forwardingsvc.NewForwardingClient(daemonConnection)
// Invoke list.
request := &forwardingsvc.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("Source", state.Session.Source, state.SourceConnected)
printEndpointStatus("Destination", state.Session.Destination, state.DestinationConnected)
printSessionStatus(state)
}
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 forwarding 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")
}