diff --git a/cmd/branches.go b/cmd/branches.go index 0248e7f31..b541704bf 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -82,7 +82,7 @@ var ( } else { branchId = args[0] } - return get.Run(ctx, branchId) + return get.Run(ctx, branchId, afero.NewOsFs()) }, } @@ -165,6 +165,8 @@ func init() { createFlags.Var(&branchRegion, "region", "Select a region to deploy the branch database.") createFlags.Var(&size, "size", "Select a desired instance size for the branch database.") createFlags.BoolVar(&persistent, "persistent", false, "Whether to create a persistent branch.") + getFlags := branchGetCmd.Flags() + getFlags.VarP(&utils.OutputFormat, "output", "o", "Output format of branch details.") branchesCmd.AddCommand(branchCreateCmd) branchesCmd.AddCommand(branchListCmd) branchesCmd.AddCommand(branchGetCmd) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index ee5bc332c..22f3274e7 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -3,13 +3,17 @@ package get import ( "context" "fmt" + "os" "github.com/go-errors/errors" + "github.com/jackc/pgconn" + "github.com/spf13/afero" "github.com/supabase/cli/internal/migration/list" "github.com/supabase/cli/internal/utils" + "github.com/supabase/cli/pkg/cast" ) -func Run(ctx context.Context, branchId string) error { +func Run(ctx context.Context, branchId string, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) @@ -29,10 +33,25 @@ func Run(ctx context.Context, branchId string) error { resp.JSON200.JwtSecret = &masked } - table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS| -|-|-|-|-|-|-|-| + config := pgconn.Config{ + Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), + Port: cast.UIntToUInt16(cast.IntToUint(resp.JSON200.DbPort)), + User: *resp.JSON200.DbUser, + Password: *resp.JSON200.DbPass, + } + + postgresConnectionString := utils.ToPostgresURL(config) + if utils.OutputFormat.Value != utils.OutputPretty { + envs := map[string]string{ + "POSTGRES_URL": postgresConnectionString, + } + return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, envs) + } + + table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|POSTGRES URL| +|-|-|-|-|-|-|-|-| ` + fmt.Sprintf( - "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", + "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", resp.JSON200.DbHost, resp.JSON200.DbPort, *resp.JSON200.DbUser, @@ -40,6 +59,8 @@ func Run(ctx context.Context, branchId string) error { *resp.JSON200.JwtSecret, resp.JSON200.PostgresVersion, resp.JSON200.Status, + postgresConnectionString, ) + return list.RenderTable(table) } diff --git a/pkg/cast/cast.go b/pkg/cast/cast.go index 621ae9a61..89840c895 100644 --- a/pkg/cast/cast.go +++ b/pkg/cast/cast.go @@ -10,6 +10,14 @@ func UintToInt(value uint) int { return math.MaxInt } +// UIntToUInt16 converts a uint to an uint16, handling potential overflow +func UIntToUInt16(value uint) uint16 { + if value <= math.MaxUint16 { + return uint16(value) + } + return math.MaxUint16 +} + // IntToUint converts an int to a uint, handling negative values func IntToUint(value int) uint { if value < 0 {