-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
59 lines (53 loc) · 1.76 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
package databaseconnection
import (
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
)
// ListCommand creates the "connection list" command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New("list", "List current connections to specified databases", "upctl database connection list"),
}
}
type listCommand struct {
*commands.BaseCommand
resolver.CachingDatabase
completion.Database
}
// Execute implements commands.MultipleArgumentCommand
func (s *listCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
connections, err := svc.GetManagedDatabaseConnections(exec.Context(), &request.GetManagedDatabaseConnectionsRequest{UUID: uuid}) //nolint:staticcheck // Deprecated, replace in a feature PR
if err != nil {
return nil, err
}
rows := []output.TableRow{}
for _, conn := range connections {
rows = append(rows, output.TableRow{
conn.Pid,
conn.State,
conn.ApplicationName,
conn.DatName,
conn.Username,
conn.ClientAddr,
})
}
return output.MarshaledWithHumanOutput{
Value: connections,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "pid", Header: "Process ID"},
{Key: "state", Header: "State"},
{Key: "application_name", Header: "Application name"},
{Key: "dataname", Header: "Database"},
{Key: "username", Header: "Username"},
{Key: "client_addr", Header: "Client IP", Colour: ui.DefaultAddressColours},
},
Rows: rows,
},
}, nil
}