Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/command/mpg/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"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"
)

Expand Down Expand Up @@ -85,5 +87,9 @@ func runAttach(ctx context.Context) error {
appName, appOrgSlug, cluster.Id, clusterOrgSlug)
}

return cmdv1.RunAttach(ctx, cluster.Id)
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunAttach(ctx, cluster.Id)
}

return cmdv2.RunAttach(ctx, cluster.Id)
}
31 changes: 17 additions & 14 deletions internal/command/mpg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/spf13/cobra"
"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/uiex/mpg"
)

func newBackup() *cobra.Command {
Expand Down Expand Up @@ -54,16 +56,16 @@ func newBackupList() *cobra.Command {

func runBackupList(ctx context.Context) error {
clusterID := flag.FirstArg(ctx)
if clusterID == "" {
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

clusterID = cluster.Id
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunBackupList(ctx, cluster.Id)
}

return cmdv1.RunBackupList(ctx, clusterID)
return cmdv2.RunBackupList(ctx, cluster.Id)
}

func newBackupCreate() *cobra.Command {
Expand Down Expand Up @@ -92,14 +94,15 @@ func newBackupCreate() *cobra.Command {

func runBackupCreate(ctx context.Context) error {
clusterID := flag.FirstArg(ctx)
if clusterID == "" {
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

clusterID = cluster.Id
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

if cluster.Version == mpg.VersionV1 {
return cmdv1.RunBackupCreate(ctx, cluster.Id)
}

return cmdv1.RunBackupCreate(ctx, clusterID)
return cmdv2.RunBackupCreate(ctx, clusterID)
}
33 changes: 15 additions & 18 deletions internal/command/mpg/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"github.com/spf13/cobra"
"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"
mpgv1 "github.com/superfly/flyctl/internal/uiex/mpg/v1"
"github.com/superfly/flyctl/internal/uiex/mpg"
)

const (
localProxyPort = "16380"
)

func newConnect() (cmd *cobra.Command) {
Expand Down Expand Up @@ -40,24 +45,12 @@ func newConnect() (cmd *cobra.Command) {

func runConnect(ctx context.Context) (err error) {
clusterID := flag.FirstArg(ctx)

var orgSlug string

if clusterID != "" {
// If cluster ID is provided, fetch directly without prompting for org
mpgClient := mpgv1.ClientFromContext(ctx)
response, err := mpgClient.GetManagedClusterById(ctx, clusterID)
if err != nil {
return fmt.Errorf("failed retrieving cluster %s: %w", clusterID, err)
}
orgSlug = response.Data.Organization.Slug
} else {
// Otherwise, prompt for org/cluster selection
cluster, resolvedOrgSlug, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}
clusterID = cluster.Id
orgSlug = resolvedOrgSlug
cluster, orgSlug, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

// Resolve org slug alias for wireguard tunnel
Expand All @@ -66,5 +59,9 @@ func runConnect(ctx context.Context) (err error) {
return fmt.Errorf("failed to resolve organization slug: %w", err)
}

return cmdv1.RunConnect(ctx, clusterID, resolvedOrgSlug)
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunConnect(ctx, cluster.Id, resolvedOrgSlug)
}

return cmdv2.RunConnect(ctx, cluster.Id, resolvedOrgSlug, localProxyPort)
}
30 changes: 16 additions & 14 deletions internal/command/mpg/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/spf13/cobra"
"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/uiex/mpg"
)

func newDatabases() *cobra.Command {
Expand Down Expand Up @@ -47,16 +49,16 @@ func newDatabasesList() *cobra.Command {

func runDatabasesList(ctx context.Context) error {
clusterID := flag.FirstArg(ctx)
if clusterID == "" {
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

clusterID = cluster.Id
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunDatabasesList(ctx, clusterID)
}

return cmdv1.RunDatabasesList(ctx, clusterID)
return cmdv2.RunDatabasesList(ctx, clusterID)
}

func newDatabasesCreate() *cobra.Command {
Expand Down Expand Up @@ -85,14 +87,14 @@ func newDatabasesCreate() *cobra.Command {

func runDatabasesCreate(ctx context.Context) error {
clusterID := flag.FirstArg(ctx)
if clusterID == "" {
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

clusterID = cluster.Id
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunDatabasesCreate(ctx, cluster.Id)
}

return cmdv1.RunDatabasesCreate(ctx, clusterID)
return cmdv2.RunDatabasesCreate(ctx, cluster.Id)
}
14 changes: 12 additions & 2 deletions internal/command/mpg/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/spf13/cobra"
"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/uiex/mpg"
)

func newDestroy() *cobra.Command {
Expand All @@ -32,7 +34,15 @@ This action is not reversible.`
}

func runDestroy(ctx context.Context) error {
clusterId := flag.FirstArg(ctx)
clusterID := flag.FirstArg(ctx)
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, "")
if err != nil {
return err
}

return cmdv1.RunDestroy(ctx, clusterId)
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunDestroy(ctx, cluster.Id)
}

return cmdv2.RunDestroy(ctx, cluster.Id)
}
8 changes: 7 additions & 1 deletion internal/command/mpg/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"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"
)

Expand Down Expand Up @@ -69,5 +71,9 @@ func runDetach(ctx context.Context) error {
appName, appOrgSlug, cluster.Id, clusterOrgSlug)
}

return cmdv1.RunDetach(ctx, cluster.Id, appName)
if cluster.Version == mpg.VersionV1 {
return cmdv1.RunDetach(ctx, cluster.Id, appName)
}

return cmdv2.RunDetach(ctx, cluster.Id, appName)
}
5 changes: 3 additions & 2 deletions internal/command/mpg/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
cmdv1 "github.com/superfly/flyctl/internal/command/mpg/v1"
"github.com/superfly/flyctl/internal/command/orgs"
"github.com/superfly/flyctl/internal/flag"
mpgv1 "github.com/superfly/flyctl/internal/uiex/mpg/v1"
mpg "github.com/superfly/flyctl/internal/uiex/mpg"
)

func newList() *cobra.Command {
Expand Down Expand Up @@ -42,11 +42,12 @@ func runList(ctx context.Context) error {
return err
}

// TODO: Move out of cmdv1 and list all across v1 and v2
return cmdv1.RunList(ctx, org.Slug)
}

// formatAttachedApps formats the list of attached apps for display.
// Delegates to the v1 implementation.
func formatAttachedApps(apps []mpgv1.AttachedApp) string {
func formatAttachedApps(apps []mpg.AttachedApp) string {
return cmdv1.FormatAttachedApps(apps)
}
75 changes: 52 additions & 23 deletions internal/command/mpg/mpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/uiex/mpg"
mpgv1 "github.com/superfly/flyctl/internal/uiex/mpg/v1"
)

Expand Down Expand Up @@ -104,9 +105,10 @@
// otherwise it prompts the user to select a cluster from the available ones for
// the given organization.
// It prompts for the org if the org slug is not provided.
func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mpgv1.ManagedCluster, string, error) {
mpgClient := mpgv1.ClientFromContext(ctx)

func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mpg.Cluster, string, error) {
// If a cluster ID is provided, fetch the cluster directly
// TODO

Check failure on line 111 in internal/command/mpg/mpg.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofmt)
if orgSlug == "" {
org, err := prompt.Org(ctx)
if err != nil {
Expand All @@ -116,44 +118,71 @@
orgSlug = org.RawSlug
}

clustersResponse, err := mpgClient.ListManagedClusters(ctx, orgSlug, false)
// Fetch V1 clusters
mpgv1Client := mpgv1.ClientFromContext(ctx)
clustersResponse, err := mpgv1Client.ListManagedClusters(ctx, orgSlug, false)
if err != nil {
return nil, orgSlug, fmt.Errorf("failed retrieving postgres clusters: %w", err)
}

if len(clustersResponse.Data) == 0 {
return nil, orgSlug, fmt.Errorf("no managed postgres clusters found in organization %s", orgSlug)
// // Fetch V2 clusters
// v2Client := mpgv2.Client{
// Client: uiexClient,
// }
// clustersV2, err := v2Client.ListManagedClusters(ctx, orgSlug, false)
// if err != nil {
// return nil, orgSlug, fmt.Errorf("failed retrieving postgres clusters: %w", err)
// }

// if len(clustersV1.Data) == 0 && len(clustersV2.Data) == 0 {
// return nil, orgSlug, fmt.Errorf("no managed postgres clusters found in organization %s", orgSlug)
// }

// clusters := slices.Concat(clustersV1.Data, clustersV2.Data)
clusters := make([]*mpg.Cluster, 0, len(clustersResponse.Data))
for _, cluster := range clustersResponse.Data {
clusters = append(clusters, &mpg.Cluster{
Id: cluster.Id,
Name: cluster.Name,
Region: cluster.Region,
Status: cluster.Status,
Plan: cluster.Plan,
Disk: cluster.Disk,
Replicas: cluster.Replicas,
Organization: cluster.Organization,
Version: mpg.VersionV1,
})
}

// If a cluster ID is provided via flag, find it
if clusterID != "" {
// If a cluster ID is provided via flag, find it
for i := range clustersResponse.Data {
if clustersResponse.Data[i].Id == clusterID {
return &clustersResponse.Data[i], orgSlug, nil
for _, cluster := range clusters {
if cluster.Id == clusterID {
return cluster, orgSlug, nil
}
}

return nil, orgSlug, fmt.Errorf("managed postgres cluster %q not found in organization %s", clusterID, orgSlug)
} else {
// Otherwise, prompt the user to select a cluster
var options []string
for _, cluster := range clustersResponse.Data {
options = append(options, fmt.Sprintf("%s [%s] (%s)", cluster.Name, cluster.Id, cluster.Region))
}
}

var index int
selectErr := prompt.Select(ctx, &index, "Select a Postgres cluster", "", options...)
if selectErr != nil {
return nil, orgSlug, selectErr
}
// Otherwise, prompt the user to select a cluster
var options []string
for _, cluster := range clusters {
options = append(options, fmt.Sprintf("%s [%s] (%s)", cluster.Name, cluster.Id, cluster.Region))
}

return &clustersResponse.Data[index], orgSlug, nil
var index int
selectErr := prompt.Select(ctx, &index, "Select a Postgres cluster", "", options...)
if selectErr != nil {
return nil, orgSlug, selectErr
}

return clusters[index], orgSlug, nil
}

// ClusterFromFlagOrSelect retrieves the cluster ID from the --cluster flag.
// If the flag is not set, it prompts the user to select a cluster from the available ones for the given organization.
func ClusterFromFlagOrSelect(ctx context.Context, orgSlug string) (*mpgv1.ManagedCluster, error) {
func ClusterFromFlagOrSelect(ctx context.Context, orgSlug string) (*mpg.Cluster, error) {
clusterID := flag.GetMPGClusterID(ctx)
cluster, _, err := ClusterFromArgOrSelect(ctx, clusterID, orgSlug)

Expand Down
Loading
Loading