-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.js
77 lines (73 loc) · 2.68 KB
/
execute.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const exec = require('@actions/exec'); // EXEC IS NOT A BASH INTERPRETER: https://github.com/actions/toolkit/issues/461
const helpers = require('./helpers')
const fs = require('fs');
let STD = ''
async function hostCommands(commands,hostCommandOptions,existingSTD) {
if (typeof(existingSTD) !== "undefined") { // Reset
if (existingSTD.length !== 0) {
STD = existingSTD
}
} else {
STD = ''
}
var options = await helpers.mergeOptions(hostCommandOptions,{
listeners: { // Populate STDOUT AND STDERR
stdout: (data) => {
STD += data.toString();
},
stderr: (data) => {
STD += data.toString();
}
}
})
// Ensure the options.cwd exists
if (options.cwd) {
try {
await exec.exec('bash', ['-c', `cd -P ${options.cwd} || exit 10`],{silent: true});
} catch(error) {
throw new Error(`exec.exec failed: cannot find ${options.cwd}`)
}
}
// Execute
try {
await exec.exec('bash', ['-c', commands], options);
} catch(error) {
throw new Error(`exec.exec failed:\n${helpers.lastFourLines(STD)}\n${error.stack}`)
}
// Return STD outputs
exports.STD = STD;
exports.finalHostCommandOptions = options; // used in tests
return true
}
module.exports.hostCommands = hostCommands;
async function ankaRun(ankaVMLabel,ankaRunOptions,ankaVmCommands,hostCommandOptions) {
// So we can use bash -s + HEREDOC, we need to add proper newlines to commands
await hostCommands(`anka run ${ankaRunOptions} ${ankaVMLabel} bash -c \"${ankaVmCommands}\"`,hostCommandOptions,STD)
}
module.exports.ankaRun = ankaRun;
async function ankaCp(direction,location,ankaVmTemplateName,destinationDirectory,hostCommandOptions) {
try {
if (direction === "in") {
if (fs.existsSync(location)) {
if (fs.lstatSync(location).isSymbolicLink()) {
await hostCommands(`anka cp -fRH ${location}/ ${ankaVmTemplateName}:${destinationDirectory}${helpers.obtainLastPathSection(`${location}`)}`,hostCommandOptions,STD)
} else if (
(fs.lstatSync(location).isDirectory()) ||
(fs.lstatSync(location).isFile())
) {
await hostCommands(`anka cp -fa ${location} ${ankaVmTemplateName}:${destinationDirectory}`,hostCommandOptions,STD)
} else {
throw new Error(`could not determine if "${location}" is a file, folder, symlink`)
}
} else {
throw new Error(`"${location}" does not exist`)
}
} else { // out
await hostCommands(`anka cp -fa ${ankaVmTemplateName}:${location} ${destinationDirectory}`,hostCommandOptions,STD)
}
} catch (error) {
throw new Error(`ankaCp failed:\n${error.stack}`);
}
return true
}
module.exports.ankaCp = ankaCp;