-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathdetach.go
More file actions
79 lines (65 loc) · 2.2 KB
/
Copy pathdetach.go
File metadata and controls
79 lines (65 loc) · 2.2 KB
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
package mpg
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
cmdv1 "github.com/superfly/flyctl/internal/command/mpg/v1"
cmdv2 "github.com/superfly/flyctl/internal/command/mpg/v2"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/uiex/mpg"
"github.com/superfly/flyctl/iostreams"
)
func newDetach() *cobra.Command {
const (
short = "Detach a managed Postgres cluster from an app"
long = short + ". " +
`This command will remove the attachment record linking the app to the cluster.
Note: This does NOT remove any secrets from the app. Use 'fly secrets unset' to remove secrets.`
usage = "detach <CLUSTER ID>"
)
cmd := command.New(usage, short, long, runDetach,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runDetach(ctx context.Context) error {
var (
clusterId = flag.FirstArg(ctx)
appName = appconfig.NameFromContext(ctx)
client = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
)
// Get app details to determine which org it belongs to
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return fmt.Errorf("failed retrieving app %s: %w", appName, err)
}
appOrgSlug := app.Organization.RawSlug
if appOrgSlug != "" && clusterId == "" {
fmt.Fprintf(io.Out, "Listing clusters in organization %s\n", appOrgSlug)
}
// Get cluster details
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterId, appOrgSlug)
if err != nil {
return fmt.Errorf("failed retrieving cluster %s: %w", clusterId, err)
}
clusterOrgSlug := cluster.Organization.Slug
// Verify that the app and cluster are in the same organization
if appOrgSlug != clusterOrgSlug {
return fmt.Errorf("app %s is in organization %s, but cluster %s is in organization %s. They must be in the same organization",
appName, appOrgSlug, cluster.Id, clusterOrgSlug)
}
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunDetach(ctx, cluster.Id, appName)
}
return cmdv2.RunDetach(ctx, cluster.Id, appName)
}