Skip to content

Commit

Permalink
cleanup for v1
Browse files Browse the repository at this point in the history
  • Loading branch information
rvolo committed Jan 29, 2021
1 parent fb3247b commit 7e4c709
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
XML-Replace-Action

Simple github action to replace values in a xml file using xpath
Simple github action to replace a value in a xml config file

Sample replace action
```yaml
- name: Set logback env values
uses: rvolo/[email protected]
with:
filepath: "config-file.xml"
xpath: "//configuration/server/port/text()"
replace: "5000"
```
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,38 @@ const fs = require('fs');
try {
let filePath = core.getInput('filepath', {required: true});
let xpathString = core.getInput('xpath', {required: true});
let replaceString = core.getInput('replace', {required: true});
let replaceString = core.getInput('replace');

writeFile(filePath, xpathString, replaceString);
} catch (error) {
console.log(error)
core.setFailed(error.message);
}

function writeFile(filePath, xpathString, replaceString) {
const content = fs.readFileSync(filePath, 'utf8');
const document = new DOMParser().parseFromString(content);

const nodes = xpath.select(xpathString, document);
for (const node of nodes) {
node.firstChild.textContent = replaceString;
if (nodes.length === 0) {
core.setFailed("No matching xml nodes found");
} else {
for (const node of nodes) {
console.log("Setting xml value at " + getNodePath(node));
if (replaceString === null) {
node.parentNode.removeChild(node);
} else {
node.textContent = replaceString;
}
}
fs.writeFileSync(filePath, new XMLSerializer().serializeToString(document));
}

fs.writeFileSync(filePath, new XMLSerializer().serializeToString(document));
} catch (error) {
console.log(error)
core.setFailed(error.message);
}

function getNodePath(node) {
let parentNode = node.parentNode;
if (parentNode == null) {
return node.nodeName;
}
return (getNodePath(parentNode)) + "/" + node.nodeName;
}

0 comments on commit 7e4c709

Please sign in to comment.