MCPcopy Index your code
hub / github.com/coder/coder / embeddedPostgresURL

Function embeddedPostgresURL

cli/server.go:2319–2352  ·  view source on GitHub ↗

embeddedPostgresURL returns the URL for the embedded PostgreSQL deployment.

(cfg config.Root)

Source from the content-addressed store, hash-verified

2317
2318// embeddedPostgresURL returns the URL for the embedded PostgreSQL deployment.
2319func embeddedPostgresURL(cfg config.Root) (string, error) {
2320 pgPassword, err := cfg.PostgresPassword().Read()
2321 if errors.Is(err, os.ErrNotExist) {
2322 pgPassword, err = cryptorand.String(16)
2323 if err != nil {
2324 return "", xerrors.Errorf("generate password: %w", err)
2325 }
2326 err = cfg.PostgresPassword().Write(pgPassword)
2327 if err != nil {
2328 return "", xerrors.Errorf("write password: %w", err)
2329 }
2330 }
2331 if err != nil && !errors.Is(err, os.ErrNotExist) {
2332 return "", err
2333 }
2334 pgPort, err := cfg.PostgresPort().Read()
2335 if errors.Is(err, os.ErrNotExist) {
2336 listener, err := net.Listen("tcp4", "127.0.0.1:0")
2337 if err != nil {
2338 return "", xerrors.Errorf("listen for random port: %w", err)
2339 }
2340 _ = listener.Close()
2341 tcpAddr, valid := listener.Addr().(*net.TCPAddr)
2342 if !valid {
2343 return "", xerrors.Errorf("listener returned non TCP addr: %T", tcpAddr)
2344 }
2345 pgPort = strconv.Itoa(tcpAddr.Port)
2346 err = cfg.PostgresPort().Write(pgPort)
2347 if err != nil {
2348 return "", xerrors.Errorf("write postgres port: %w", err)
2349 }
2350 }
2351 return fmt.Sprintf("postgres://coder@localhost:%s/coder?sslmode=disable&password=%s", pgPort, pgPassword), nil
2352}
2353
2354func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logger, customCacheDir string) (string, func() error, error) {
2355 usr, err := user.Current()

Callers 2

ServerMethod · 0.85
startBuiltinPostgresFunction · 0.85

Calls 10

StringFunction · 0.92
PostgresPasswordMethod · 0.80
PostgresPortMethod · 0.80
ReadMethod · 0.65
WriteMethod · 0.65
ListenMethod · 0.65
CloseMethod · 0.65
IsMethod · 0.45
ErrorfMethod · 0.45
AddrMethod · 0.45

Tested by

no test coverage detected