Skip to content

Commit 4bef3a5

Browse files
author
Matt Farmer
committed
bin: Allow configuration via ENV variables
Check for environment variable of config options, and if present override any config file or command-line option. Environment variable names will be the SNAKE_CASE version of an option's name, prefixed with `SOLID_`. For example `--db-path` would be overridden by `SOLID_DB_PATH` and `--serverUri` would be overridden by `SOLID_SERVER_URI`
1 parent c0329fb commit 4bef3a5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ $ solid start --help
206206
-h, --help output usage information
207207
```
208208

209+
Instead of using flags, these same options can also be configured via environment variables taking the form of `SOLID_` followed by the `SNAKE_CASE` of the flag. For example `--api-apps` can be set via the `SOLID_API_APPS`environment variable, and `--serverUri` can be set with `SOLID_SERVER_URI`.
210+
211+
CLI flags take precedence over Environment variables, which take precedence over entries in the config file.
212+
213+
Configuring Solid via the config file can be a concise and convenient method and is the generally recommended approach. CLI flags can be useful when you would like to override a single configuration parameter, and using environment variables can be helpful in situations where you wish to deploy a single generic Docker image to multiple environments.
214+
209215
## Use Docker
210216

211217
Build with:

bin/lib/start.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ module.exports = function (program, server) {
1313
options
1414
.filter((option) => !option.hide)
1515
.forEach((option) => {
16+
const configName = option.name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
17+
const snakeCaseName = configName.replace(/([A-Z])/g, '_$1')
18+
const envName = `SOLID_${snakeCaseName.toUpperCase()}`
19+
1620
let name = '--' + option.name
1721
if (!option.flag) {
1822
name += ' [value]'
1923
}
20-
start.option(name, option.help)
24+
25+
if (process.env[envName]) {
26+
const raw = process.env[envName]
27+
const envValue = /^(true|false)$/.test(raw) ? raw === 'true' : raw
28+
29+
start.option(name, option.help, envValue)
30+
} else {
31+
start.option(name, option.help)
32+
}
2133
})
2234

2335
start.option('-v, --verbose', 'Print the logs to console')

0 commit comments

Comments
 (0)