/
users.go
159 lines (125 loc) · 3.16 KB
/
users.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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/agent"
"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/config"
"github.com/superfly/flyctl/internal/flag"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newUsers() *cobra.Command {
const (
short = "Manage users in a postgres cluster"
long = short + "\n"
usage = "users"
)
cmd := command.New(usage, short, long, nil)
cmd.AddCommand(
newListUsers(),
)
return cmd
}
func newListUsers() *cobra.Command {
const (
short = "List users"
long = short + "\n"
usage = "list"
)
cmd := command.New(usage, short, long, runListUsers,
command.RequireSession,
command.RequireAppName,
)
cmd.Aliases = []string{"ls"}
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.JSONOutput(),
)
return cmd
}
func runListUsers(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
}
return runMachineListUsers(ctx, app)
}
func runMachineListUsers(ctx context.Context, app *fly.AppCompact) (err error) {
// Minimum image version requirements
var (
MinPostgresHaVersion = "0.0.19"
MinPostgresFlexVersion = "0.0.3"
MinPostgresStandaloneVersion = "0.0.7"
)
machines, err := mach.ListActive(ctx)
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
if err := hasRequiredVersionOnMachines(machines, MinPostgresHaVersion, MinPostgresFlexVersion, MinPostgresStandaloneVersion); err != nil {
return err
}
leader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
return renderUsers(ctx, leader.PrivateIP)
}
func renderUsers(ctx context.Context, leaderIP string) error {
var (
io = iostreams.FromContext(ctx)
cfg = config.FromContext(ctx)
dialer = agent.DialerFromContext(ctx)
)
pgclient := flypg.NewFromInstance(leaderIP, dialer)
users, err := pgclient.ListUsers(ctx)
if err != nil {
return fmt.Errorf("error fetching users: %w", err)
}
if len(users) == 0 {
fmt.Fprintf(io.Out, "No users found\n")
return nil
}
if cfg.JSONOutput {
return render.JSON(io.Out, users)
}
rows := make([][]string, len(users))
for _, user := range users {
var databases string
for i, database := range user.Databases {
databases += database
if i < len(user.Databases)-1 {
databases += ", "
}
}
superuser := "no"
if user.Superuser {
superuser = "yes"
}
rows = append(rows, []string{
user.Username,
superuser,
databases,
})
}
return render.Table(io.Out, "", rows, "Name", "Superuser", "Databases")
}