Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sandbox cli #494

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions source/cli/metacallcli/include/metacallcli/application.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ class application
private:
/* -- Private Member Data -- */

void *plugin_cli_handle; /**< Handle containing all loaded plugins for CLI */
void *plugin_repl_handle; /**< Handle containing all loaded plugins for REPL */
void *plugin_cmd_handle; /**< Handle containing all loaded plugins for CMD */
void *plugin_cli_handle; /**< Handle containing all loaded plugins for CLI */
void *plugin_repl_handle; /**< Handle containing all loaded plugins for REPL */
void *plugin_cmd_handle; /**< Handle containing all loaded plugins for CMD */
void *plugin_sandbox_handle; /**< Handle containing all loaded plugins for sandbox */
};

} /* namespace metacallcli */
Expand Down
23 changes: 22 additions & 1 deletion source/cli/metacallcli/source/application.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ void application::repl()

bool application::cmd(std::vector<std::string> &arguments)
{
/* Get the command initializing function for sandbox plugin */
void *cli_sandbox_plugin_cmd_initialize_func = metacall_handle_function(plugin_sandbox_handle, "cli_sandbox_plugin_cmd_initialize");

if (cli_sandbox_plugin_cmd_initialize_func == NULL)
{
return false;
}

/* Initialize CLI sandbox plugin */
void *cli_sandbox_plugin_cmd_initialize_ret = metacallfv_s(cli_sandbox_plugin_cmd_initialize_func, metacall_null_args, 0);

check_for_exception(cli_sandbox_plugin_cmd_initialize_ret);

/* Get the command parsing function */
void *command_parse_func = metacall_handle_function(plugin_cli_handle, "command_parse");

Expand Down Expand Up @@ -198,7 +211,7 @@ bool application::cmd(std::vector<std::string> &arguments)
}

application::application(int argc, char *argv[]) :
plugin_cli_handle(NULL), plugin_repl_handle(NULL), plugin_cmd_handle(NULL)
plugin_cli_handle(NULL), plugin_repl_handle(NULL), plugin_cmd_handle(NULL), plugin_sandbox_handle(NULL)
{
/* Initialize MetaCall */
if (metacall_initialize() != 0)
Expand All @@ -221,6 +234,14 @@ application::application(int argc, char *argv[]) :
return;
}

/* Initialize CLI sandbox plugin */
if (!load_path("sandbox", &plugin_sandbox_handle))
{
/* Do not enter into the main loop */
exit_condition = true;
return;
}

if (argc == 1)
{
/* Launch the REPL */
Expand Down
124 changes: 62 additions & 62 deletions source/cli/plugins/cli_cmd_plugin/source/cli_cmd_plugin.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
const util = require('util');

const options = {};

function command_initialize(plugin_path) {
/* Initialize all CMD descriptors
* This will load all plugin descriptors like:
* plugins/cli/cmd/${plugin_name}/${plugin_name}_cmd.js
*/
const cmd_path = path.join(plugin_path, 'cli', 'cmd');
const files = fs.readdirSync(cmd_path);

for (const file of files) {
const file_path = path.join(cmd_path, file);
const file_stat = fs.statSync(file_path);

if (file_stat.isDirectory()) {
const descriptor_path = path.join(file_path, `${file}_cmd.js`);
const descriptor_stat = fs.statSync(descriptor_path);

if (descriptor_stat.isFile()) {
const descriptor = require(descriptor_path);
options = { ...options, ...descriptor };
}
}
}
}

function command_register(cmd, type, short, multiple) {
const type_map = {
METACALL_STRING: 'string',
METACALL_BOOL: 'boolean'
};

options[cmd] = {
type: type_map[type || 'METACALL_BOOL']
};

if (short) {
options[cmd]['short'] = short;
}

if (multiple) {
options[cmd]['multiple'] = multiple;
}
}

function command_parse(args) {
const { values, positionals } = util.parseArgs({
args,
options,
allowPositionals: true
});

return [ { ...values }, positionals ];
}

module.exports = {
command_initialize,
command_register,
command_parse
};
const util = require('util');
const options = {};
function command_initialize(plugin_path) {
/* Initialize all CMD descriptors
* This will load all plugin descriptors like:
* plugins/cli/cmd/${plugin_name}/${plugin_name}_cmd.js
*/
const cmd_path = path.join(plugin_path, 'cli', 'cmd', 'sandbox');
const files = fs.readdirSync(cmd_path);
for (const file of files) {
const file_path = path.join(cmd_path, file);
const file_stat = fs.statSync(file_path);
if (file_stat.isDirectory()) {
const descriptor_path = path.join(file_path, `${file}_cmd.js`);
const descriptor_stat = fs.statSync(descriptor_path);
if (descriptor_stat.isFile()) {
const descriptor = require(descriptor_path);
options = { ...options, ...descriptor };
}
}
}
}
function command_register(cmd, type, short, multiple) {
const type_map = {
METACALL_STRING: 'string',
METACALL_BOOL: 'boolean'
};
options[cmd] = {
type: type_map[type || 'METACALL_BOOL']
};
if (short) {
options[cmd]['short'] = short;
}
if (multiple) {
options[cmd]['multiple'] = multiple;
}
}
function command_parse(args) {
const { values, positionals } = util.parseArgs({
args,
options,
allowPositionals: true
});
return [ { ...values }, positionals ];
}
module.exports = {
command_initialize,
command_register,
command_parse
};
109 changes: 55 additions & 54 deletions source/cli/plugins/cli_sandbox_plugin/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
# Check if this loader is enabled
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_EXT OR NOT OPTION_BUILD_EXTENSIONS OR NOT OPTION_BUILD_LOADERS_NODE OR NOT TARGET sandbox_plugin)
return()
endif()

#
# Plugin name and options
#

# Target name
set(target cli_sandbox_plugin)

# Exit here if required dependencies are not met
message(STATUS "Plugin ${target}")

#
# Source
#

set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

#
# Destination
#

set(PLUGIN_OUTPUT_DIRECTORY "${PROJECT_OUTPUT_DIR}/plugins/cli/cmd/${target}")

#
# Project Target
#

add_custom_target(${target} ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${source_path}/cli_sandbox_plugin_cmd.js ${PLUGIN_OUTPUT_DIRECTORY}/cli_sandbox_plugin_cmd.js
)

#
# Target Properties
#

set_target_properties(${target}
PROPERTIES
FOLDER "${IDE_FOLDER}"
)

#
# Dependencies
#

add_dependencies(${target}
plugin_extension
node_loader
sandbox_plugin
)
# Check if this loader is enabled
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_EXT OR NOT OPTION_BUILD_EXTENSIONS OR NOT OPTION_BUILD_LOADERS_NODE OR NOT TARGET sandbox_plugin)
return()
endif()

#
# Plugin name and options
#

# Target name
set(target cli_sandbox_plugin)

# Exit here if required dependencies are not met
message(STATUS "Plugin ${target}")

#
# Source
#

set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

#
# Destination
#

set(PLUGIN_OUTPUT_DIRECTORY "${PROJECT_OUTPUT_DIR}/plugins/cli/cmd/${target}")

#
# Project Target
#

add_custom_target(${target} ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${source_path}/metacall.json ${PLUGIN_OUTPUT_DIRECTORY}/metacall.json
COMMAND ${CMAKE_COMMAND} -E copy ${source_path}/cli_sandbox_plugin_cmd.js ${PLUGIN_OUTPUT_DIRECTORY}/cli_sandbox_plugin_cmd.js
)

#
# Target Properties
#

set_target_properties(${target}
PROPERTIES
FOLDER "${IDE_FOLDER}"
)

#
# Dependencies
#

add_dependencies(${target}
plugin_extension
node_loader
sandbox_plugin
)
21 changes: 17 additions & 4 deletions source/cli/plugins/cli_sandbox_plugin/source/cli_sandbox_plugin_cmd.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
/* TODO */
module.exports = {

};
const { command_register } = require('../../cli_cmd_plugin/source/cli_cmd_plugin');

function cli_sandbox_plugin_cmd_initialize() {
command_register('sandbox', 'METACALL_BOOL', 's'); // s -> sandbox
command_register('allow-io', 'METACALL_BOOL', 'sio'); // sio -> sandbox-io
command_register('allow-sockets', 'METACALL_BOOL', 'sn'); // sn -> sandbox-network
command_register('allow-ipc', 'METACALL_BOOL', 'sipc'); // sipc -> sandbox-ipc
command_register('allow-process', 'METACALL_BOOL', 'sp'); // sp -> sandbox-process
command_register('allow-filesystems', 'METACALL_BOOL', 'sf'); // sf -> sandbox-filesystem
command_register('allow-time', 'METACALL_BOOL', 'st'); // st -> sandbox-time
command_register('allow-memory', 'METACALL_BOOL', 'sm'); // sm -> sandbox-memory
command_register('allow-signals', 'METACALL_BOOL', 'ss'); // ss -> sandbox-signals
}

module.exports = {
cli_sandbox_plugin_cmd_initialize
};
7 changes: 7 additions & 0 deletions source/cli/plugins/cli_sandbox_plugin/source/metacall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"language_id": "node",
"path": ".",
"scripts": [
"cli_sandbox_plugin_cmd.js"
]
}
Loading