-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRunScript.php
More file actions
124 lines (108 loc) · 3.87 KB
/
RunScript.php
File metadata and controls
124 lines (108 loc) · 3.87 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace OCA\FilesScripts\Command;
use Error;
use OC\Core\Command\Base;
use OCA\FilesScripts\Db\ScriptInputMapper;
use OCA\FilesScripts\Db\ScriptMapper;
use OCA\FilesScripts\Interpreter\Context;
use OCA\FilesScripts\Interpreter\Lua\LuaProvider;
use OCA\FilesScripts\Service\ScriptService;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class RunScript extends Base {
private ScriptService $scriptService;
private ScriptMapper $scriptMapper;
private ScriptInputMapper $scriptInputMapper;
private LuaProvider $luaProvider;
private IRootFolder $rootFolder;
public function __construct(
ScriptService $scriptService,
ScriptMapper $scriptMapper,
ScriptInputMapper $scriptInputMapper,
IRootFolder $rootFolder,
LuaProvider $luaProvider
) {
parent::__construct('files_scripts:run');
$this->scriptService = $scriptService;
$this->scriptMapper = $scriptMapper;
$this->scriptInputMapper = $scriptInputMapper;
$this->rootFolder = $rootFolder;
$this->luaProvider = $luaProvider;
}
protected function configure(): void {
$this->setDescription('Runs a file action')
->addArgument('id', InputArgument::REQUIRED, 'ID of the action to be run')
->addOption('user', 'u', InputOption::VALUE_OPTIONAL, 'User as which the action should be run')
->addOption('inputs', 'i', InputOption::VALUE_OPTIONAL, 'The user inputs to be set before running the action as a JSON string')
->addOption('file', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File path or id of a file given to the action as input file');
parent::configure();
}
public static function getFilesForCommand(InputInterface $input, OutputInterface $output, Folder $rootFolder) {
$fileInputs = $input->getOption('file') ?? [];
$files = [];
$n = 1;
foreach ($fileInputs as $fileInput) {
if (isset($fileInput))
{
try {
if (ctype_digit(strval($fileInput))) {
$nodes = $rootFolder->getById(intval($fileInput));
if (!isset($nodes[0])) {
throw new Error('Could not find input file ' . $fileInput . ' belonging in root folder ' . $rootFolder->getPath() . ' for file action');
}
$file = $nodes[0];
unset($nodes);
} else {
$file = $rootFolder->get($fileInput);
}
} catch (\Throwable $e) {
throw $e;
}
$files[$n++] = $file;
}
}
return $files;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$scriptId = $input->getArgument('id');
$userId = $input->getOption('user');
$scriptInputsJson = $input->getOption('inputs') ?? '{}';
try {
$scriptInputsData = json_decode($scriptInputsJson, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $err) {
$output->writeln('<error>Could not parse the inputs JSON</error>');
return 1;
}
$script = $this->scriptMapper->find($scriptId);
$output->writeln('<info>Executing file action: ' . $script->getTitle() . '</info>');
$scriptInputs = $this->scriptInputMapper->findAllByScriptId($scriptId) ?? [];
foreach ($scriptInputs as $scriptInput) {
$value = $scriptInputsData[$scriptInput->getName()] ?? null;
$scriptInput->setValue($value);
}
$rootFolder = $this->rootFolder;
try {
if ($userId) {
$rootFolder = $this->rootFolder->getUserFolder($userId);
}
$files = self::getFilesForCommand($input, $output, $rootFolder);
} catch (\Throwable $e) {
$output->writeln('<error>' . $e->getMessage() .'</error>');
return 1;
}
print(gettype($scriptInputs));
$context = new Context(
$this->luaProvider->createLua(),
$rootFolder,
$scriptInputs,
$files
);
$this->scriptService->runScript($script, $context);
$output->writeln('<info>File action executed successfully.</info>');
return 0;
}
}