mirror of
https://github.com/gethomepage/homepage.git
synced 2025-12-07 09:35:54 -08:00
Added support for environment variable substitution
* Only environment variables starting with HOMEPAGE_VAR_ and HOMEPAGE_FILE_
are supported
* The value of env var HOMEPAGE_VAR_XXX will replace {{HOMEPAGE_VAR_XXX}}
in any config
* The value of env var HOMEPAGE_FILE_XXX must be a file path, the contents
of which will be used to replace {{HOMEPAGE_FILE_XXX}} in any config
* If a substituted value contains a variable reference it may also be
replaced, but the behavior is non-deterministic
This commit is contained in:
@@ -27,10 +27,28 @@ export default function checkAndCopyConfig(config) {
|
||||
}
|
||||
}
|
||||
|
||||
export function substituteEnvironmentVars(str) {
|
||||
const homepageVarPrefix = "HOMEPAGE_VAR_";
|
||||
const homepageFilePrefix = "HOMEPAGE_FILE_";
|
||||
|
||||
let result = str;
|
||||
Object.keys(process.env).forEach(key => {
|
||||
if (key.startsWith(homepageVarPrefix)) {
|
||||
result = result.replaceAll(`{{${key}}}`, process.env[key]);
|
||||
} else if (key.startsWith(homepageFilePrefix)) {
|
||||
const filename = process.env[key];
|
||||
const fileContents = readFileSync(filename, "utf8");
|
||||
result = result.replaceAll(`{{${key}}}`, fileContents);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getSettings() {
|
||||
checkAndCopyConfig("settings.yaml");
|
||||
|
||||
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
|
||||
const fileContents = readFileSync(settingsYaml, "utf8");
|
||||
const rawFileContents = readFileSync(settingsYaml, "utf8");
|
||||
const fileContents = substituteEnvironmentVars(rawFileContents);
|
||||
return yaml.load(fileContents) ?? {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user