Skip to content

Commit 4e7659c

Browse files
authored
Merge pull request #107 from ClickHouse/issue-90-server-dotenv
Add `local server dotenv` command
2 parents 6f6f2ed + 37b3fdb commit 4e7659c

4 files changed

Lines changed: 403 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ clickhousectl local server stop-all --global # Stop all servers sys
126126

127127
# Remove a stopped server and its data
128128
clickhousectl local server remove test
129+
130+
# Write connection env vars to .env file
131+
clickhousectl local server dotenv # From "default" server → .env
132+
clickhousectl local server dotenv --name dev # From "dev" server → .env
133+
clickhousectl local server dotenv --local # Write to .env.local instead
134+
clickhousectl local server dotenv --user default --password secret --database mydb # Include credentials
129135
```
130136

131137
**Server naming:** Without `--name`, the first server is called "default". If "default" is already running, a random name is generated (e.g. "bold-crane"). Use `--name` for stable identities you can start/stop repeatedly.

src/local/cli.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,36 @@ CONTEXT FOR AGENTS:
230230
/// Name of the server to remove
231231
name: String,
232232
},
233+
234+
/// Write ClickHouse connection env vars to a .env file
235+
#[command(after_help = "\
236+
CONTEXT FOR AGENTS:
237+
Writes CLICKHOUSE_HOST, CLICKHOUSE_PORT, and CLICKHOUSE_HTTP_PORT into a .env file
238+
(or .env.local with --local) based on a running server's actual connection details.
239+
Optionally includes CLICKHOUSE_USER, CLICKHOUSE_PASSWORD, and CLICKHOUSE_DATABASE when
240+
the corresponding flags are provided.
241+
If the file already exists, existing CLICKHOUSE_* vars are replaced in-place. Otherwise the file is created.
242+
Useful for configuring apps that read from dotenv files.
243+
Related: `clickhousectl local server start` to start a server, `clickhousectl local server list` to see servers.")]
244+
Dotenv {
245+
/// Server name (default: "default")
246+
#[arg(long)]
247+
name: Option<String>,
248+
249+
/// Write to .env.local instead of .env
250+
#[arg(long)]
251+
local: bool,
252+
253+
/// Include CLICKHOUSE_USER with this value
254+
#[arg(long)]
255+
user: Option<String>,
256+
257+
/// Include CLICKHOUSE_PASSWORD with this value
258+
#[arg(long)]
259+
password: Option<String>,
260+
261+
/// Include CLICKHOUSE_DATABASE with this value
262+
#[arg(long)]
263+
database: Option<String>,
264+
},
233265
}

0 commit comments

Comments
 (0)