Skip to content

Commit

Permalink
Add additional field additionalPaths and updated dependency version
Browse files Browse the repository at this point in the history
  • Loading branch information
prakattuladhar committed Apr 11, 2022
1 parent 23b9c8c commit ceda68d
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 107 deletions.
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ inputs:
description: 'Passwort to login'
required: true
default: 'password'
additionalPaths:
description: "Map of the path to upload multiple files. '{"localPath":"remotePath"}'"
localPath:
description: 'Local file or directory'
required: true
Expand Down
52 changes: 36 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (privateKeyIsFile == "true") {

const localPath = core.getInput('localPath');
const remotePath = core.getInput('remotePath');
const additionalPaths = core.getInput('additionalPaths')

sftp.connect({
host: host,
Expand All @@ -39,26 +40,22 @@ sftp.connect({
}).then(async () => {
console.log("Connection established.");
console.log("Current working directory: " + await sftp.cwd())
let promisesList = [processPath(localPath, remotePath)] //TODO: Instead of localPath, remotePath use key/value to uplaod multiple files at once.

if (fs.lstatSync(localPath).isDirectory()) {
return sftp.uploadDir(localPath, remotePath);
} else {

var directory = await sftp.realPath(path.dirname(remotePath));
if (!(await sftp.exists(directory))) {
await sftp.mkdir(directory, true);
console.log("Created directories.");
const parsedAdditionalPaths = (() => {
try {
const parsedAdditionalPaths = JSON.parse(additionalPaths)
return Object.entries(parsedAdditionalPaths)
}

var modifiedPath = remotePath;
if (await sftp.exists(remotePath)) {
if ((await sftp.stat(remotePath)).isDirectory) {
var modifiedPath = modifiedPath + path.basename(localPath);
}
catch (e) {
throw "Error parsing addtionalPaths. Make sure it is a valid JSON object (key/ value pairs)."
}
})()

return sftp.put(fs.createReadStream(localPath), modifiedPath);
}
parsedAdditionalPaths.forEach(([local, remote]) => {
promisesList.push(processPath(local, remote))
})
return Promise.all(promisesList)

}).then(() => {
console.log("Upload finished.");
Expand All @@ -67,3 +64,26 @@ sftp.connect({
core.setFailed(`Action failed with error ${err}`);
process.exit(1);
});

function processPath(local, remote) {
console.log("Uploading: " + local + " to " + remote)
if (fs.lstatSync(local).isDirectory()) {
return sftp.uploadDir(local, remote);
} else {

var directory = await sftp.realPath(path.dirname(remote));
if (!(await sftp.exists(directory))) {
await sftp.mkdir(directory, true);
console.log("Created directories.");
}

var modifiedPath = remote;
if (await sftp.exists(remote)) {
if ((await sftp.stat(remote)).isDirectory) {
var modifiedPath = modifiedPath + path.basename(local);
}
}

return sftp.put(fs.createReadStream(local), modifiedPath);
}
}
Loading

0 comments on commit ceda68d

Please sign in to comment.