on main help sign in
Add env var alternatives for all connection options
8129fdbfabianlindfors
README.md+10−10
@@ -559,16 +559,16 @@ The query should look something like `SET search_path TO migration_1_initial_mig
559
559
560### Connection options
560### Connection options
561
561
562−The options below can be used with all commands that communicate with Postgres. Use either a connection URL or specify each connection option individually.
562+The options below can be used with all commands that communicate with Postgres. Use either a [connection URL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) or specify each connection option individually. All options can also be set using environment variables instead of flags.
563−
563+
564−| Option | Default | Description |
564+| Option | Default | Environment variable | Description |
565−| ------ | ------- | ----------- |
565+| ------ | ------- | -------------------- | ----------- |
566−| `--url` | | URI to connect to your Postgres database.<br>Can also be provided with the environment variable `DB_URL`. |
566+| `--url` | | `DB_URL` | URL to your Postgres database |
567−| `--host` | `localhost` | Hostname to use when connecting to Postgres |
567+| `--host` | `localhost` | `DB_HOST` | Hostname to use when connecting to Postgres |
568−| `--port` | `5432` | Port which Postgres is listening on |
568+| `--port` | `5432` | `DB_PORT` | Port which Postgres is listening on |
569−| `--database` | `postgres` | Database name |
569+| `--database` | `postgres` | `DB_NAME` | Database name |
570−| `--username` | `postgres` | Postgres username |
570+| `--username` | `postgres` | `DB_USERNAME` | Postgres username |
571−| `--password` | `postgres` | Postgres password |
571+| `--password` | `postgres` | `DB_PASSWORD` | Postgres password |
572
572
573## License
573## License
574
574
src/lib.rs+2
@@ -30,6 +30,7 @@ impl Reshape {
30 pub fn new_with_options(
30 pub fn new_with_options(
31 host: &str,
31 host: &str,
32 port: u16,
32 port: u16,
33+ database: &str,
33 username: &str,
34 username: &str,
34 password: &str,
35 password: &str,
35 ) -> anyhow::Result<Reshape> {
36 ) -> anyhow::Result<Reshape> {
@@ -38,6 +39,7 @@ impl Reshape {
38 .host(host)
39 .host(host)
39 .port(port)
40 .port(port)
40 .user(username)
41 .user(username)
42+ .dbname(database)
41 .password(password);
43 .password(password);
42
44
43 Self::new_with_config(&config)
45 Self::new_with_config(&config)
src/main.rs+24−5
@@ -105,13 +105,32 @@ fn run(opts: Opts) -> anyhow::Result<()> {
105}
105}
106
106
107fn reshape_from_connection_options(opts: &ConnectionOptions) -> anyhow::Result<Reshape> {
107fn reshape_from_connection_options(opts: &ConnectionOptions) -> anyhow::Result<Reshape> {
108− let env_url = std::env::var("DB_URL").ok();
108+ let url_env = std::env::var("DB_URL").ok();
109− let url = env_url.as_ref().or_else(|| opts.url.as_ref());
109+ let url = url_env.as_ref().or_else(|| opts.url.as_ref());
110
110
111− match url {
111+ // Use the connection URL if it has been set
112+ if let Some(url) = url {
112− Some(url) => Reshape::new(url),
113+ return Reshape::new(url);
113− None => Reshape::new_with_options(&opts.host, opts.port, &opts.username, &opts.password),
114 }
114 }
115+
116+ let host_env = std::env::var("DB_HOST").ok();
117+ let host = host_env.as_ref().unwrap_or_else(|| &opts.host);
118+
119+ let port = std::env::var("DB_PORT")
120+ .ok()
121+ .and_then(|port| port.parse::<u16>().ok())
122+ .unwrap_or(opts.port);
123+
124+ let username_env = std::env::var("DB_USERNAME").ok();
125+ let username = username_env.as_ref().unwrap_or_else(|| &opts.username);
126+
127+ let password_env = std::env::var("DB_PASSWORD").ok();
128+ let password = password_env.as_ref().unwrap_or_else(|| &opts.password);
129+
130+ let database_env = std::env::var("DB_NAME").ok();
131+ let database = database_env.as_ref().unwrap_or_else(|| &opts.database);
132+
133+ Reshape::new_with_options(host, port, database, username, password)
115}
134}
116
135
117fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration>> {
136fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration>> {