-
Notifications
You must be signed in to change notification settings - Fork 221
/
events.go
139 lines (114 loc) · 2.75 KB
/
events.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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flag/flagnames"
mach "github.com/superfly/flyctl/internal/machine"
)
func newEvents() *cobra.Command {
const (
short = "Track major cluster events"
long = short + "\n"
usage = "events"
)
cmd := command.New(usage, short, long, nil)
cmd.AddCommand(
newListEvents(),
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func newListEvents() *cobra.Command {
const (
short = "Outputs a formatted list of cluster events"
long = short + "\n"
usage = "list"
)
cmd := command.New(usage, short, long, runListEvents,
command.RequireSession,
command.RequireAppName,
)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.String{
Name: "event",
Shorthand: "e",
Description: "Event type in a postgres cluster",
},
flag.String{
Name: "limit",
Shorthand: "l",
Description: "Set the maximum number of entries to output (default: 20)",
},
flag.String{
Name: "node-id",
Shorthand: "i",
Description: "Restrict entries to node with this ID",
},
flag.String{
Name: "node-name",
Shorthand: "n",
Description: "Restrict entries to node with this name",
},
flag.Bool{
Name: "all",
Shorthand: "o",
Description: "Outputs all entries",
},
flag.Bool{
Name: "compact",
Shorthand: "d",
Description: "Omit the 'Details' column",
},
)
return cmd
}
func runListEvents(ctx context.Context) error {
var (
client = fly.ClientFromContext(ctx)
appName = appconfig.NameFromContext(ctx)
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("failed retrieving app %s: %w", appName, err)
}
if !app.IsPostgresApp() {
return fmt.Errorf("app %s is not a postgres app", appName)
}
ctx, err = apps.BuildContext(ctx, app)
if err != nil {
return err
}
machines, err := mach.ListActive(ctx)
if err != nil {
return err
}
leader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
if !IsFlex(leader) {
return fmt.Errorf("this feature is not compatible with this postgres service ")
}
ignoreFlags := []string{flagnames.AccessToken, flagnames.App, flagnames.AppConfigFilePath,
flagnames.Verbose, "help"}
flagsName := flag.GetFlagsName(ctx, ignoreFlags)
cmd, err := flypg.NewCommand(ctx, app)
if err != nil {
return err
}
err = cmd.ListEvents(ctx, leader.PrivateIP, flagsName)
if err != nil {
return err
}
return nil
}