Skip to content

Commit

Permalink
feat: adding the ability to not fail if the file is not found (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjohnson00 authored Jul 25, 2023
1 parent a3ff367 commit 4950620
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ jobs:
echo "${{ steps.test-nodes.outputs.var_1 }}"
echo "${{ steps.test-nodes.outputs.var_2 }}"
echo "${{ steps.test-nodes.outputs.var_3 }}"
echo "${{ steps.test-nodes.outputs.var_4__var_1__var_2 }}"
echo "${{ steps.test-nodes.outputs.var_4__var_1__var_2 }}"
- uses: ./
name: Test File Not Found, expect no failure
id: test-file-not-found # By default the action fails if the file is not found, test that it does not fail when instructed
with:
file-path: '.github/not-found-test-file.yaml'
fail-on-file-not-found: false
- uses: ./
name: Test File Not Found, expect failure
id: test-file-not-found-fail
with:
file-path: '.github/not-found-test-file.yaml'
continue-on-error: true
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Default: all the nodes in the file will be used.

**Optional** It controls whether outputs are exported as environment variables or not. Default: true.

### `fail-on-file-not-found`

**Optional** It controls whether the action should fail if the `file-path` is found or not. Default: true.

## Outputs 📜
This action generates one output parameter for each key in the flattened YAML file. The name of each output parameter is the concatenation of the flattened key and the separator.

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ inputs:
description: 'Export outputs as environment variables'
default: true

fail-on-file-not-found:
required: false
description: 'Set to false if you do not want the action to fail if the file does not exist'
default: true

runs:
using: 'node16'
main: 'dist/index.js'
19 changes: 17 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 2 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ try {
const separator = core.getInput('separator')
const node = core.getInput('node')
const exportEnvVariables = core.getBooleanInput('export-env-variables')
const failOnFileNotFoundInput = core.getInput('fail-on-file-not-found', {
required: false
})
const failOnFileNotFound =
failOnFileNotFoundInput.trim().toLowerCase() === 'true'

// Read file content and parse it as YAML
const fileContent = fs.readFileSync(filePath, 'utf8')
let fileContent
try {
// Read file content and parse it as YAML
fileContent = fs.readFileSync(filePath, 'utf8')
} catch (error) {
if (failOnFileNotFound) throw error
else {
core.notice('file-path was not found')
process.exit(0)
}
}
const yamlData: any = getYamlData(fileContent, node)

// Flatten the object recursively
Expand Down

0 comments on commit 4950620

Please sign in to comment.