mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: Add --raw-url to coder server postgres-builtin-* commands (#5478)
This commit is contained in:
committed by
GitHub
parent
50dfc2082b
commit
c7ce3e70da
@ -921,7 +921,8 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
root.AddCommand(&cobra.Command{
|
var pgRawURL bool
|
||||||
|
postgresBuiltinURLCmd := &cobra.Command{
|
||||||
Use: "postgres-builtin-url",
|
Use: "postgres-builtin-url",
|
||||||
Short: "Output the connection URL for the built-in PostgreSQL deployment.",
|
Short: "Output the connection URL for the built-in PostgreSQL deployment.",
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
@ -930,37 +931,49 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "psql %q\n", url)
|
if pgRawURL {
|
||||||
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", url)
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", cliui.Styles.Code.Render(fmt.Sprintf("psql %q", url)))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
postgresBuiltinServeCmd := &cobra.Command{
|
||||||
root.AddCommand(&cobra.Command{
|
|
||||||
Use: "postgres-builtin-serve",
|
Use: "postgres-builtin-serve",
|
||||||
Short: "Run the built-in PostgreSQL deployment.",
|
Short: "Run the built-in PostgreSQL deployment.",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
ctx := cmd.Context()
|
||||||
|
|
||||||
cfg := createConfig(cmd)
|
cfg := createConfig(cmd)
|
||||||
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
|
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
|
||||||
if ok, _ := cmd.Flags().GetBool(varVerbose); ok {
|
if ok, _ := cmd.Flags().GetBool(varVerbose); ok {
|
||||||
logger = logger.Leveled(slog.LevelDebug)
|
logger = logger.Leveled(slog.LevelDebug)
|
||||||
}
|
}
|
||||||
|
|
||||||
url, closePg, err := startBuiltinPostgres(cmd.Context(), cfg, logger)
|
ctx, cancel := signal.NotifyContext(ctx, InterruptSignals...)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
url, closePg, err := startBuiltinPostgres(ctx, cfg, logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() { _ = closePg() }()
|
defer func() { _ = closePg() }()
|
||||||
|
|
||||||
cmd.Println(cliui.Styles.Code.Render("psql \"" + url + "\""))
|
if pgRawURL {
|
||||||
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", url)
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", cliui.Styles.Code.Render(fmt.Sprintf("psql %q", url)))
|
||||||
|
}
|
||||||
|
|
||||||
stopChan := make(chan os.Signal, 1)
|
<-ctx.Done()
|
||||||
defer signal.Stop(stopChan)
|
|
||||||
signal.Notify(stopChan, os.Interrupt)
|
|
||||||
|
|
||||||
<-stopChan
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
postgresBuiltinURLCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
|
||||||
|
postgresBuiltinServeCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
|
||||||
|
|
||||||
|
root.AddCommand(postgresBuiltinURLCmd, postgresBuiltinServeCmd)
|
||||||
|
|
||||||
deployment.AttachFlags(root.Flags(), vip, false)
|
deployment.AttachFlags(root.Flags(), vip, false)
|
||||||
|
|
||||||
|
@ -118,6 +118,19 @@ func TestServer(t *testing.T) {
|
|||||||
|
|
||||||
pty.ExpectMatch("psql")
|
pty.ExpectMatch("psql")
|
||||||
})
|
})
|
||||||
|
t.Run("BuiltinPostgresURLRaw", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
root, _ := clitest.New(t, "server", "postgres-builtin-url", "--raw-url")
|
||||||
|
pty := ptytest.New(t)
|
||||||
|
root.SetOutput(pty.Output())
|
||||||
|
err := root.Execute()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
got := pty.ReadLine()
|
||||||
|
if !strings.HasPrefix(got, "postgres://") {
|
||||||
|
t.Fatalf("expected postgres URL to start with \"postgres://\", got %q", got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Validate that a warning is printed that it may not be externally
|
// Validate that a warning is printed that it may not be externally
|
||||||
// reachable.
|
// reachable.
|
||||||
|
Reference in New Issue
Block a user