forked from bhowell2/github-substring-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstring-action.js
46 lines (42 loc) · 1.74 KB
/
substring-action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const core = require('@actions/core');
function run() {
const value = core.getInput('value', {required: true});
const failIfNotFound = core.getInput('fail_if_not_found', {required: true}).toLowerCase() === 'true';
const defaultReturnValue = core.getInput('default_return_value', {required: false})
const outputName = core.getInput('output_name', {required: true});
const indexOfStr = core.getInput('index_of_str', {required: false});
const lengthFromStart = core.getInput('length_from_start', {required: false});
const lengthFromEnd = core.getInput('length_from_end', {required: false})
try {
let outputStr = null;
if (indexOfStr !== undefined) {
const pos = value.indexOf(indexOfStr);
if (pos < 0) { // no match was found
if (failIfNotFound) {
throw `Could not find '${indexOfStr}' in '${value}' with indexOf() check.`;
} else {
outputStr = defaultReturnValue;
}
} else {
// this is the start position of the indexOfStr + the length (to find the remaining bit)
outputStr = value.substr(pos + indexOfStr.length);
}
} else if (lengthFromStart !== undefined) {
outputStr = value.substr(0, Number.parseInt(lengthFromStart))
} else if (lengthFromEnd !== undefined) {
let fromEndInt = Number.parseInt(lengthFromEnd);
if (fromEndInt > value.length) {
outputStr = value;
} else {
outputStr = value.substr(value.length - fromEndInt);
}
} else {
// throw error
throw "Inputs 'index_of_str', 'length_from_start', or 'length_from_end' were not provided."
}
core.setOutput(outputName, outputStr);
} catch (err) {
core.setFailed("Action failed with error: '" + err + "'");
}
}
module.exports = run;