generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from stuartleeks/sl/env-var-substitution
Add env var substitution
- Loading branch information
Showing
7 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
substituteValues | ||
} from '../src/envvars' | ||
|
||
describe('substituteValues', () => { | ||
test('returns original string with no substitution placeholders', async () => { | ||
const input = 'This is a test' | ||
const result = await substituteValues(input) | ||
expect(result).toBe(input) | ||
}) | ||
|
||
test('Handles \'env\' substitution placeholders', async () => { | ||
process.env.TEST_ENV1='TestEnvValue1' | ||
process.env.TEST_ENV2='TestEnvValue2' | ||
const input = 'TEST_ENV1: ${env:TEST_ENV1}, TEST_ENV2: ${env:TEST_ENV2}' | ||
const result = await substituteValues(input) | ||
expect(result).toBe('TEST_ENV1: TestEnvValue1, TEST_ENV2: TestEnvValue2') | ||
}) | ||
|
||
test('ignores substitution placeholders that it doesn\'t understand', async () => { | ||
const input = 'TEST_ENV: ${nothingToSee:TEST_ENV}' | ||
const result = await substituteValues(input) | ||
expect(result).toBe(input) | ||
}) | ||
|
||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import parse from 'csv-parse' | ||
import { | ||
parseInputAsList, | ||
parseInputAsRecord | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function substituteValues(input: string): string { | ||
// Find all `${...}` entries and substitute | ||
// Note the non-greedy `.+?` match to avoid matching the start of | ||
// one placeholder up to the end of another when multiple placeholders are present | ||
return input.replace(/\$\{(.+?)\}/g, getSubstitutionValue) | ||
} | ||
|
||
function getSubstitutionValue(regexMatch: string, placeholder: string): string { | ||
// Substitution values are in TYPE:KEY form | ||
// e.g. env:MY_ENV | ||
|
||
const parts = placeholder.split(':') | ||
if (parts.length === 2) { | ||
const type = parts[0] | ||
const key = parts[1] | ||
switch (type) { | ||
case 'env': | ||
case 'localenv': | ||
return process.env[key] ?? '' | ||
} | ||
} | ||
|
||
// if we can't process the format then return the original string | ||
// as having it present in any output will likely make issues more obvious | ||
return regexMatch | ||
} |