Skip to content

Commit c7ce3e7

Browse files
authored
feat: Add --raw-url to coder server postgres-builtin-* commands (#5478)
1 parent 50dfc20 commit c7ce3e7

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

cli/server.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
921921
},
922922
}
923923

924-
root.AddCommand(&cobra.Command{
924+
var pgRawURL bool
925+
postgresBuiltinURLCmd := &cobra.Command{
925926
Use: "postgres-builtin-url",
926927
Short: "Output the connection URL for the built-in PostgreSQL deployment.",
927928
RunE: func(cmd *cobra.Command, _ []string) error {
@@ -930,37 +931,49 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
930931
if err != nil {
931932
return err
932933
}
933-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "psql %q\n", url)
934+
if pgRawURL {
935+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", url)
936+
} else {
937+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", cliui.Styles.Code.Render(fmt.Sprintf("psql %q", url)))
938+
}
934939
return nil
935940
},
936-
})
937-
938-
root.AddCommand(&cobra.Command{
941+
}
942+
postgresBuiltinServeCmd := &cobra.Command{
939943
Use: "postgres-builtin-serve",
940944
Short: "Run the built-in PostgreSQL deployment.",
941945
RunE: func(cmd *cobra.Command, args []string) error {
946+
ctx := cmd.Context()
947+
942948
cfg := createConfig(cmd)
943949
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
944950
if ok, _ := cmd.Flags().GetBool(varVerbose); ok {
945951
logger = logger.Leveled(slog.LevelDebug)
946952
}
947953

948-
url, closePg, err := startBuiltinPostgres(cmd.Context(), cfg, logger)
954+
ctx, cancel := signal.NotifyContext(ctx, InterruptSignals...)
955+
defer cancel()
956+
957+
url, closePg, err := startBuiltinPostgres(ctx, cfg, logger)
949958
if err != nil {
950959
return err
951960
}
952961
defer func() { _ = closePg() }()
953962

954-
cmd.Println(cliui.Styles.Code.Render("psql \"" + url + "\""))
955-
956-
stopChan := make(chan os.Signal, 1)
957-
defer signal.Stop(stopChan)
958-
signal.Notify(stopChan, os.Interrupt)
963+
if pgRawURL {
964+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", url)
965+
} else {
966+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", cliui.Styles.Code.Render(fmt.Sprintf("psql %q", url)))
967+
}
959968

960-
<-stopChan
969+
<-ctx.Done()
961970
return nil
962971
},
963-
})
972+
}
973+
postgresBuiltinURLCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
974+
postgresBuiltinServeCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
975+
976+
root.AddCommand(postgresBuiltinURLCmd, postgresBuiltinServeCmd)
964977

965978
deployment.AttachFlags(root.Flags(), vip, false)
966979

cli/server_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ func TestServer(t *testing.T) {
118118

119119
pty.ExpectMatch("psql")
120120
})
121+
t.Run("BuiltinPostgresURLRaw", func(t *testing.T) {
122+
t.Parallel()
123+
root, _ := clitest.New(t, "server", "postgres-builtin-url", "--raw-url")
124+
pty := ptytest.New(t)
125+
root.SetOutput(pty.Output())
126+
err := root.Execute()
127+
require.NoError(t, err)
128+
129+
got := pty.ReadLine()
130+
if !strings.HasPrefix(got, "postgres://") {
131+
t.Fatalf("expected postgres URL to start with \"postgres://\", got %q", got)
132+
}
133+
})
121134

122135
// Validate that a warning is printed that it may not be externally
123136
// reachable.

0 commit comments

Comments
 (0)