From 0fe8088a6507e63d1c54416056ad3c37bde979ac Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 25 Dec 2024 23:32:18 +0530 Subject: [PATCH 01/22] fix:2964/obtain Postgres DB URL from ENV using branches get command Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index ee5bc332c..31041a547 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -9,7 +9,7 @@ import ( "github.com/supabase/cli/internal/utils" ) -func Run(ctx context.Context, branchId string) error { +func Run(ctx context.Context, branchId string, output string) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) @@ -29,10 +29,20 @@ func Run(ctx context.Context, branchId string) error { resp.JSON200.JwtSecret = &masked } - table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS| -|-|-|-|-|-|-|-| -` + fmt.Sprintf( - "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", + table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" + if output == "env" { + table += "|POSTGRES_USER_ENV|" + } + + table += "\n|-|-|-|-|-|-|-|" + if output == "env" { + table += "-|" + } + + table += "\n" + + row := fmt.Sprintf( + "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|", resp.JSON200.DbHost, resp.JSON200.DbPort, *resp.JSON200.DbUser, @@ -41,5 +51,10 @@ func Run(ctx context.Context, branchId string) error { resp.JSON200.PostgresVersion, resp.JSON200.Status, ) + if output == "env" { + row += fmt.Sprintf("`%s`|", "POSTGRES_ENV_URL goes here") + } + table += row + "\n" + return list.RenderTable(table) } From 1342a93f0c602234a493e617f5ad396f63261285 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Tue, 31 Dec 2024 23:44:01 +0530 Subject: [PATCH 02/22] added --output-env flag in branches get command Signed-off-by: Yaten Dhingra --- cmd/branches.go | 9 ++++++++- internal/bootstrap/bootstrap.go | 31 +++++++++++++++++++++++++++++++ internal/branches/get/get.go | 12 +++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index 0248e7f31..2ab42d251 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -6,6 +6,7 @@ import ( "os" "github.com/go-errors/errors" + "github.com/jackc/pgconn" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/supabase/cli/internal/branches/create" @@ -67,6 +68,7 @@ var ( } branchId string + postgres_url bool branchGetCmd = &cobra.Command{ Use: "get [branch-id]", @@ -75,14 +77,19 @@ var ( Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() + cmdFlags := cmd.Flags() + var output bool if len(args) == 0 { if err := promptBranchId(ctx, flags.ProjectRef); err != nil { return err } } else { branchId = args[0] + if cmdFlags.Changed("output-env") { + output = true + } } - return get.Run(ctx, branchId) + return get.Run(ctx, branchId, output, pgconn.Config{}, afero.NewOsFs()) }, } diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index 677ef1530..c456651f2 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -272,6 +272,37 @@ func writeDotEnv(keys []api.ApiKeyResponse, config pgconn.Config, fsys afero.Fs) return utils.WriteFile(".env", []byte(out), fsys) } +func GetPostgresURLNonPooling(config pgconn.Config, fsys afero.Fs) string { + // Initialize default envs + initial := map[string]string{ + POSTGRES_URL: utils.ToPostgresURL(config), + } + + // Populate from .env.example if exists + envs, err := parseExampleEnv(fsys) + if err != nil { + errors.Errorf("failed to parse .env.example: %w", err) + return "" + } + for k := range envs { + switch k { + case POSTGRES_URL_NON_POOLING: + initial[k] = utils.ToPostgresURL(config) + default: + // Skip other keys + continue + } + } + + // Check if POSTGRES_URL_NON_POOLING is present + if value, exists := initial[POSTGRES_URL_NON_POOLING]; exists { + return value + } + + errors.New("error fetching POSTGRES_URL_NON_POOLING") + return "" +} + func parseExampleEnv(fsys afero.Fs) (map[string]string, error) { path := ".env.example" f, err := fsys.Open(path) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 31041a547..7ed56d511 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -5,11 +5,13 @@ import ( "fmt" "github.com/go-errors/errors" + "github.com/spf13/afero" + "github.com/jackc/pgconn" "github.com/supabase/cli/internal/migration/list" "github.com/supabase/cli/internal/utils" ) -func Run(ctx context.Context, branchId string, output string) error { +func Run(ctx context.Context, branchId string, env bool, config pgconn.Config, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) @@ -30,12 +32,12 @@ func Run(ctx context.Context, branchId string, output string) error { } table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" - if output == "env" { + if env { table += "|POSTGRES_USER_ENV|" } table += "\n|-|-|-|-|-|-|-|" - if output == "env" { + if env { table += "-|" } @@ -51,8 +53,8 @@ func Run(ctx context.Context, branchId string, output string) error { resp.JSON200.PostgresVersion, resp.JSON200.Status, ) - if output == "env" { - row += fmt.Sprintf("`%s`|", "POSTGRES_ENV_URL goes here") + if env { + row += fmt.Sprintf("`%s`|", "") } table += row + "\n" From 5f7573b7492673113f2dbc0001553e5f27598a2f Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 11:42:36 +0530 Subject: [PATCH 03/22] fixed typo in internal/branches/get/get.go Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 7ed56d511..fe4669cbc 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -5,8 +5,9 @@ import ( "fmt" "github.com/go-errors/errors" - "github.com/spf13/afero" "github.com/jackc/pgconn" + "github.com/spf13/afero" + "github.com/supabase/cli/internal/bootstrap" "github.com/supabase/cli/internal/migration/list" "github.com/supabase/cli/internal/utils" ) @@ -54,7 +55,7 @@ func Run(ctx context.Context, branchId string, env bool, config pgconn.Config, f resp.JSON200.Status, ) if env { - row += fmt.Sprintf("`%s`|", "") + row += fmt.Sprintf("`%s`|", bootstrap.GetPostgresURLNonPooling(config, fsys)) } table += row + "\n" From 7898aca0a052650d0152bb4f7ce63b06095bb606 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 11:53:08 +0530 Subject: [PATCH 04/22] updated the output-env flag in branches.go Signed-off-by: Yaten Dhingra --- cmd/branches.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/branches.go b/cmd/branches.go index 2ab42d251..0024cb170 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -172,6 +172,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.BoolVar(&postgres_url, "output-env", false, "Output the Postgres URL in the table.") branchesCmd.AddCommand(branchCreateCmd) branchesCmd.AddCommand(branchListCmd) branchesCmd.AddCommand(branchGetCmd) From 031d8c20a9319908e02603f950984957ad2edab8 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 11:56:13 +0530 Subject: [PATCH 05/22] changed output var to postgres_url var in branches.go Signed-off-by: Yaten Dhingra --- cmd/branches.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index 0024cb170..ffc1ea500 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -78,7 +78,6 @@ var ( RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() cmdFlags := cmd.Flags() - var output bool if len(args) == 0 { if err := promptBranchId(ctx, flags.ProjectRef); err != nil { return err @@ -86,10 +85,10 @@ var ( } else { branchId = args[0] if cmdFlags.Changed("output-env") { - output = true + postgres_url = true } } - return get.Run(ctx, branchId, output, pgconn.Config{}, afero.NewOsFs()) + return get.Run(ctx, branchId, postgres_url, pgconn.Config{}, afero.NewOsFs()) }, } From 1b7a0d8d4f092a0547f454ec00b84c380404a405 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 12:49:51 +0530 Subject: [PATCH 06/22] changed postgres_url to output Signed-off-by: Yaten Dhingra --- cmd/branches.go | 7 +++---- internal/branches/get/get.go | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index ffc1ea500..9bf43d034 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -68,7 +68,6 @@ var ( } branchId string - postgres_url bool branchGetCmd = &cobra.Command{ Use: "get [branch-id]", @@ -85,10 +84,10 @@ var ( } else { branchId = args[0] if cmdFlags.Changed("output-env") { - postgres_url = true + output.Value = "env" } } - return get.Run(ctx, branchId, postgres_url, pgconn.Config{}, afero.NewOsFs()) + return get.Run(ctx, branchId, output.Value, pgconn.Config{}, afero.NewOsFs()) }, } @@ -172,7 +171,7 @@ func init() { 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.BoolVar(&postgres_url, "output-env", false, "Output the Postgres URL in the table.") + getFlags.VarP(&output, "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 fe4669cbc..8c784ff24 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -12,7 +12,7 @@ import ( "github.com/supabase/cli/internal/utils" ) -func Run(ctx context.Context, branchId string, env bool, config pgconn.Config, fsys afero.Fs) error { +func Run(ctx context.Context, branchId string, env string, config pgconn.Config, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) @@ -33,12 +33,12 @@ func Run(ctx context.Context, branchId string, env bool, config pgconn.Config, f } table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" - if env { + if env == "env" { table += "|POSTGRES_USER_ENV|" } table += "\n|-|-|-|-|-|-|-|" - if env { + if env == "env" { table += "-|" } @@ -54,7 +54,7 @@ func Run(ctx context.Context, branchId string, env bool, config pgconn.Config, f resp.JSON200.PostgresVersion, resp.JSON200.Status, ) - if env { + if env == "env" { row += fmt.Sprintf("`%s`|", bootstrap.GetPostgresURLNonPooling(config, fsys)) } table += row + "\n" From 81657b00a5f0afd0333b941f39e0719c17668d1e Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 13:05:06 +0530 Subject: [PATCH 07/22] changed checks condition from env to utils.OutputEnv Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 8c784ff24..e34a5c287 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -33,12 +33,12 @@ func Run(ctx context.Context, branchId string, env string, config pgconn.Config, } table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" - if env == "env" { + if env == utils.OutputEnv { table += "|POSTGRES_USER_ENV|" } table += "\n|-|-|-|-|-|-|-|" - if env == "env" { + if env == utils.OutputEnv { table += "-|" } @@ -54,7 +54,7 @@ func Run(ctx context.Context, branchId string, env string, config pgconn.Config, resp.JSON200.PostgresVersion, resp.JSON200.Status, ) - if env == "env" { + if env == utils.OutputEnv { row += fmt.Sprintf("`%s`|", bootstrap.GetPostgresURLNonPooling(config, fsys)) } table += row + "\n" From 1d7773f8adf49c6dfde4ee4bae011a38c4af6810 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra <129659514+yaten2302@users.noreply.github.com> Date: Wed, 1 Jan 2025 13:19:55 +0530 Subject: [PATCH 08/22] remove cmdFlags condition in cmd/branches.go Co-authored-by: Han Qiao --- cmd/branches.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index 9bf43d034..1c7083364 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -83,9 +83,6 @@ var ( } } else { branchId = args[0] - if cmdFlags.Changed("output-env") { - output.Value = "env" - } } return get.Run(ctx, branchId, output.Value, pgconn.Config{}, afero.NewOsFs()) }, From e275bb4a3e696a13059a9461d64a6334de8b592b Mon Sep 17 00:00:00 2001 From: Yaten Dhingra <129659514+yaten2302@users.noreply.github.com> Date: Wed, 1 Jan 2025 13:20:21 +0530 Subject: [PATCH 09/22] remove cmdFlags in cmd/branches.go Co-authored-by: Han Qiao --- cmd/branches.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/branches.go b/cmd/branches.go index 1c7083364..9925d172b 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -76,7 +76,6 @@ var ( Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - cmdFlags := cmd.Flags() if len(args) == 0 { if err := promptBranchId(ctx, flags.ProjectRef); err != nil { return err From eca4f21ac23d47f4837f7aaf3008169cdb65b781 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra <129659514+yaten2302@users.noreply.github.com> Date: Wed, 1 Jan 2025 13:28:08 +0530 Subject: [PATCH 10/22] remove pgconn.Config{} param in get.Run() Co-authored-by: Han Qiao --- cmd/branches.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/branches.go b/cmd/branches.go index 9925d172b..ea52d4738 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -83,7 +83,7 @@ var ( } else { branchId = args[0] } - return get.Run(ctx, branchId, output.Value, pgconn.Config{}, afero.NewOsFs()) + return get.Run(ctx, branchId, output.Value, afero.NewOsFs()) }, } From 69b6c8e499ed8b45411b73967f0aaf600016bd88 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra <129659514+yaten2302@users.noreply.github.com> Date: Wed, 1 Jan 2025 13:41:21 +0530 Subject: [PATCH 11/22] remove unused import in cmd/branches.go Co-authored-by: Han Qiao --- cmd/branches.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/branches.go b/cmd/branches.go index ea52d4738..af5a7d6ba 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -6,7 +6,6 @@ import ( "os" "github.com/go-errors/errors" - "github.com/jackc/pgconn" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/supabase/cli/internal/branches/create" From b3d6e5f6d222d7d5ffa8b1ef87cbfb8c40225fe7 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 14:02:43 +0530 Subject: [PATCH 12/22] get DB URL from utils.GetSupabaseDbHost(...) Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index e34a5c287..db5b40bfd 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -12,7 +12,7 @@ import ( "github.com/supabase/cli/internal/utils" ) -func Run(ctx context.Context, branchId string, env string, config pgconn.Config, fsys afero.Fs) error { +func Run(ctx context.Context, branchId string, env 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) @@ -32,6 +32,8 @@ func Run(ctx context.Context, branchId string, env string, config pgconn.Config, resp.JSON200.JwtSecret = &masked } + config := pgconn.Config{Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost)} + table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" if env == utils.OutputEnv { table += "|POSTGRES_USER_ENV|" From 76407f2af00b7469de668f0862c396490c5bfb36 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 16:30:14 +0530 Subject: [PATCH 13/22] revert to prev table render structure Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index db5b40bfd..41137357c 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -7,7 +7,6 @@ import ( "github.com/go-errors/errors" "github.com/jackc/pgconn" "github.com/spf13/afero" - "github.com/supabase/cli/internal/bootstrap" "github.com/supabase/cli/internal/migration/list" "github.com/supabase/cli/internal/utils" ) @@ -34,20 +33,10 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error config := pgconn.Config{Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost)} - table := "|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|" - if env == utils.OutputEnv { - table += "|POSTGRES_USER_ENV|" - } - - table += "\n|-|-|-|-|-|-|-|" - if env == utils.OutputEnv { - table += "-|" - } - - table += "\n" - - row := fmt.Sprintf( - "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|", + table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS| +|-|-|-|-|-|-|-| +` + fmt.Sprintf( + "|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", resp.JSON200.DbHost, resp.JSON200.DbPort, *resp.JSON200.DbUser, @@ -56,10 +45,6 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error resp.JSON200.PostgresVersion, resp.JSON200.Status, ) - if env == utils.OutputEnv { - row += fmt.Sprintf("`%s`|", bootstrap.GetPostgresURLNonPooling(config, fsys)) - } - table += row + "\n" return list.RenderTable(table) } From 73a60fd9dd220c4847ed19d29553c08378b50a58 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Wed, 1 Jan 2025 22:42:00 +0530 Subject: [PATCH 14/22] update get.go Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 41137357c..ae092fd9c 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -31,12 +31,17 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error resp.JSON200.JwtSecret = &masked } - config := pgconn.Config{Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost)} + config := pgconn.Config{ + Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), + Port: uint16(resp.JSON200.DbPort), + User: *resp.JSON200.DbUser, + Password: *resp.JSON200.DbPass, + } - table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS| -|-|-|-|-|-|-|-| + 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, @@ -44,6 +49,7 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error *resp.JSON200.JwtSecret, resp.JSON200.PostgresVersion, resp.JSON200.Status, + "", ) return list.RenderTable(table) From 139e739d9e202ba2b70b2705eb66d1bb7f0b43a3 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Fri, 3 Jan 2025 13:17:07 +0530 Subject: [PATCH 15/22] added postgres connection string Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index ae092fd9c..a472af7e8 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -38,6 +38,7 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error Password: *resp.JSON200.DbPass, } + postgresConnectionString := utils.ToPostgresURL(config) table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|POSTGRES URL| |-|-|-|-|-|-|-|-| ` + fmt.Sprintf( @@ -49,7 +50,7 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error *resp.JSON200.JwtSecret, resp.JSON200.PostgresVersion, resp.JSON200.Status, - "", + postgresConnectionString, ) return list.RenderTable(table) From 188ecc6d35502035ef0a6cf534e75b7f99baf1a7 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Fri, 3 Jan 2025 15:09:28 +0530 Subject: [PATCH 16/22] removed GetPostgresURLNonPooling func from bootstrap.go Signed-off-by: Yaten Dhingra --- internal/bootstrap/bootstrap.go | 31 ------------------------------- internal/branches/get/get.go | 9 +++++++++ 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index c456651f2..677ef1530 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -272,37 +272,6 @@ func writeDotEnv(keys []api.ApiKeyResponse, config pgconn.Config, fsys afero.Fs) return utils.WriteFile(".env", []byte(out), fsys) } -func GetPostgresURLNonPooling(config pgconn.Config, fsys afero.Fs) string { - // Initialize default envs - initial := map[string]string{ - POSTGRES_URL: utils.ToPostgresURL(config), - } - - // Populate from .env.example if exists - envs, err := parseExampleEnv(fsys) - if err != nil { - errors.Errorf("failed to parse .env.example: %w", err) - return "" - } - for k := range envs { - switch k { - case POSTGRES_URL_NON_POOLING: - initial[k] = utils.ToPostgresURL(config) - default: - // Skip other keys - continue - } - } - - // Check if POSTGRES_URL_NON_POOLING is present - if value, exists := initial[POSTGRES_URL_NON_POOLING]; exists { - return value - } - - errors.New("error fetching POSTGRES_URL_NON_POOLING") - return "" -} - func parseExampleEnv(fsys afero.Fs) (map[string]string, error) { path := ".env.example" f, err := fsys.Open(path) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index a472af7e8..229e38c3a 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -3,6 +3,7 @@ package get import ( "context" "fmt" + "os" "github.com/go-errors/errors" "github.com/jackc/pgconn" @@ -13,6 +14,7 @@ import ( func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) + envs := map[string]string{} if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) } @@ -39,6 +41,13 @@ func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error } 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( From 44257ab5e59cce9485c3c8f1929b55c6e850a017 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Fri, 3 Jan 2025 15:11:51 +0530 Subject: [PATCH 17/22] remove vars which are not required Signed-off-by: Yaten Dhingra --- cmd/branches.go | 2 +- internal/branches/get/get.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index af5a7d6ba..6428ddf88 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -82,7 +82,7 @@ var ( } else { branchId = args[0] } - return get.Run(ctx, branchId, output.Value, afero.NewOsFs()) + return get.Run(ctx, branchId, afero.NewOsFs()) }, } diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 229e38c3a..858269b2c 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -12,7 +12,7 @@ import ( "github.com/supabase/cli/internal/utils" ) -func Run(ctx context.Context, branchId string, env string, fsys afero.Fs) error { +func Run(ctx context.Context, branchId string, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) envs := map[string]string{} if err != nil { From edf5b8b34208aced44c5b4ac86382340778e4fd2 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Fri, 3 Jan 2025 15:58:48 +0530 Subject: [PATCH 18/22] minor changes in get.go & branches.go Signed-off-by: Yaten Dhingra --- cmd/branches.go | 2 +- internal/branches/get/get.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/branches.go b/cmd/branches.go index 6428ddf88..b541704bf 100644 --- a/cmd/branches.go +++ b/cmd/branches.go @@ -166,7 +166,7 @@ func init() { 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(&output, "output", "o", "Output format of branch details.") + 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 858269b2c..7a8d89109 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -14,7 +14,6 @@ import ( func Run(ctx context.Context, branchId string, fsys afero.Fs) error { resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId) - envs := map[string]string{} if err != nil { return errors.Errorf("failed to retrieve preview branch: %w", err) } From 77d76a228be335e211b62809e6367796a1b4d8c4 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Sat, 4 Jan 2025 11:25:33 +0530 Subject: [PATCH 19/22] convert port from int to uint Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 7a8d89109..d4ec49460 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -10,6 +10,7 @@ import ( "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, fsys afero.Fs) error { @@ -34,7 +35,7 @@ func Run(ctx context.Context, branchId string, fsys afero.Fs) error { config := pgconn.Config{ Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), - Port: uint16(resp.JSON200.DbPort), + Port: uint16(cast.IntToUint(resp.JSON200.DbPort)), User: *resp.JSON200.DbUser, Password: *resp.JSON200.DbPass, } From 1b61d705df1a2433cd2ae67c4b0ec1337fbdbecb Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Sat, 4 Jan 2025 16:30:14 +0530 Subject: [PATCH 20/22] attempt to fix linter errors Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index d4ec49460..d4d5135f6 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -34,9 +34,9 @@ func Run(ctx context.Context, branchId string, fsys afero.Fs) error { } config := pgconn.Config{ - Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), - Port: uint16(cast.IntToUint(resp.JSON200.DbPort)), - User: *resp.JSON200.DbUser, + Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), + Port: uint16(cast.IntToUint(resp.JSON200.DbPort)), + User: *resp.JSON200.DbUser, Password: *resp.JSON200.DbPass, } From 0114638fdddc0ddcc924495c77a3d7e472b26d6c Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Sun, 5 Jan 2025 11:24:16 +0530 Subject: [PATCH 21/22] attempt to fix uint->uint16 linter error Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index d4d5135f6..113aa6be1 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -10,7 +10,6 @@ import ( "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, fsys afero.Fs) error { @@ -33,9 +32,14 @@ func Run(ctx context.Context, branchId string, fsys afero.Fs) error { resp.JSON200.JwtSecret = &masked } + if resp.JSON200.DbPort > 65535 { + return errors.Errorf("Port value %d exceeds uint16 range", resp.JSON200.DbPort) + } + port := uint16(resp.JSON200.DbPort) + config := pgconn.Config{ Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), - Port: uint16(cast.IntToUint(resp.JSON200.DbPort)), + Port: port, User: *resp.JSON200.DbUser, Password: *resp.JSON200.DbPass, } From bc371bf2f165d9c9c060a2aab4ddf4d983beb6c2 Mon Sep 17 00:00:00 2001 From: Yaten Dhingra Date: Sun, 5 Jan 2025 15:53:20 +0530 Subject: [PATCH 22/22] fixed uint->uint16 overflow error Signed-off-by: Yaten Dhingra --- internal/branches/get/get.go | 8 ++------ pkg/cast/cast.go | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/branches/get/get.go b/internal/branches/get/get.go index 113aa6be1..22f3274e7 100644 --- a/internal/branches/get/get.go +++ b/internal/branches/get/get.go @@ -10,6 +10,7 @@ import ( "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, fsys afero.Fs) error { @@ -32,14 +33,9 @@ func Run(ctx context.Context, branchId string, fsys afero.Fs) error { resp.JSON200.JwtSecret = &masked } - if resp.JSON200.DbPort > 65535 { - return errors.Errorf("Port value %d exceeds uint16 range", resp.JSON200.DbPort) - } - port := uint16(resp.JSON200.DbPort) - config := pgconn.Config{ Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost), - Port: port, + Port: cast.UIntToUInt16(cast.IntToUint(resp.JSON200.DbPort)), User: *resp.JSON200.DbUser, Password: *resp.JSON200.DbPass, } 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 {