Skip to content

Commit

Permalink
feat: add support for loading env variable in config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Aug 26, 2024
1 parent 5915011 commit aaba6a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"yaml.customTags": ["!secret"]
"yaml.customTags": ["!secret", "!env"]
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ Possible ideas:
- [x] Local recyclarr templates to include
- [ ] Clone existing templates: Lets say you want the same template but with a different name?

## Configuration

- `config.yml`
- Check the template file [template](./config.yml.template) or check the examples.
- You can provide values with the custom tags:
- `value: !secret secretKey`: Loads the value from the secrets file with the key `secretKey`
- `value: !env ENV_NAME`: Loads the value from the environment variable `ENV_NAME`

## Custom formats

This repository also provide additional custom formats what TrashGuide does not offer.
Expand Down
18 changes: 17 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ const secretsTag = {
},
};

const envTag = {
identify: (value: any) => value instanceof String,
tag: "!env",
resolve(str: string) {
const envValue = process.env[str];

if (!envValue) {
const message = `Environment variables '${str}' is not set.`;
logger.error(message);
throw new Error(message);
}

return envValue;
},
};

// TODO some schema validation. For now only check if something can be imported
export const getConfig = (): YamlConfig => {
if (config) {
Expand All @@ -31,7 +47,7 @@ export const getConfig = (): YamlConfig => {
}

const file = readFileSync(CONFIG_LOCATION, "utf8");
config = yaml.parse(file, { customTags: [secretsTag] }) as YamlConfig;
config = yaml.parse(file, { customTags: [secretsTag, envTag] }) as YamlConfig;
return config;
};

Expand Down

0 comments on commit aaba6a3

Please sign in to comment.