RunCommand bootstraps and runs a River CLI subcommand.
(ctx context.Context, bundle *RunCommandBundle, command Command[TOpts], opts TOpts)
| 57 | |
| 58 | // RunCommand bootstraps and runs a River CLI subcommand. |
| 59 | func RunCommand[TOpts CommandOpts](ctx context.Context, bundle *RunCommandBundle, command Command[TOpts], opts TOpts) error { |
| 60 | procureAndRun := func() (bool, error) { |
| 61 | if err := opts.Validate(); err != nil { |
| 62 | return false, err |
| 63 | } |
| 64 | |
| 65 | var ( |
| 66 | databaseURL *string |
| 67 | protocol string |
| 68 | urlWithoutProtocol string |
| 69 | ) |
| 70 | if pgEnvConfigured() { |
| 71 | databaseURL = ptrutil.Ptr("") |
| 72 | protocol = "postgres" |
| 73 | } else if bundle.DatabaseURL != nil { |
| 74 | databaseURL = bundle.DatabaseURL |
| 75 | var ok bool |
| 76 | protocol, urlWithoutProtocol, ok = strings.Cut(*databaseURL, "://") |
| 77 | if !ok { |
| 78 | return false, fmt.Errorf("expected database URL (`%s`) to be formatted like `postgres://...`", *bundle.DatabaseURL) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | driverProcurer := bundle.DriverProcurer |
| 83 | if databaseURL != nil { |
| 84 | switch protocol { |
| 85 | case "postgres", "postgresql": |
| 86 | dbPool, err := openPgxV5DBPool(ctx, *databaseURL, bundle.StatementTimeout) |
| 87 | if err != nil { |
| 88 | return false, err |
| 89 | } |
| 90 | defer dbPool.Close() |
| 91 | |
| 92 | driverProcurerPgxV5, isPgxV5Procurer := driverProcurer.(DriverProcurerPgxV5) |
| 93 | if driverProcurer != nil && isPgxV5Procurer { |
| 94 | driverProcurerPgxV5.InitPgxV5(dbPool) |
| 95 | } else { |
| 96 | driverProcurer = &pgxV5DriverProcurer{dbPool: dbPool} |
| 97 | } |
| 98 | |
| 99 | case "sqlite": |
| 100 | dbPool, err := openSQLitePool(protocol, urlWithoutProtocol) |
| 101 | if err != nil { |
| 102 | return false, err |
| 103 | } |
| 104 | defer dbPool.Close() |
| 105 | |
| 106 | driverProcurer = &sqliteDriverProcurer{dbPool: dbPool} |
| 107 | |
| 108 | default: |
| 109 | return false, fmt.Errorf("unsupported database URL (`%s`); try one with a `postgres://`, `postgresql://`, or `sqlite://` scheme/prefix", *bundle.DatabaseURL) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | command.SetCommandBase(&CommandBase{ |
| 114 | DriverProcurer: driverProcurer, |
| 115 | Logger: bundle.Logger, |
| 116 | Out: bundle.OutStd, |
no test coverage detected
searching dependent graphs…