Skip to content

Commit

Permalink
Merge pull request #1 from v3io/exec_command
Browse files Browse the repository at this point in the history
Add ability to execute script externally
  • Loading branch information
yanburman authored May 31, 2020
2 parents a55d39e + 5fc294b commit d7a6a81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
23 changes: 16 additions & 7 deletions icli.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Iguazio.io Systems Ltd.
* Copyright 2019-2020 Iguazio.io Systems Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License") with
* an addition restriction as set forth herein. You may not use this
Expand Down Expand Up @@ -636,26 +636,26 @@ static enum icli_ret icli_quit(char *argv[] UNUSED, int argc UNUSED, void *conte
return ICLI_OK;
}

static enum icli_ret icli_execute(char *argv[], int argc UNUSED, void *context UNUSED)
int icli_exec_script(const char *fname)
{
FILE *input = fopen(argv[0], "r");
FILE *input = fopen(fname, "r");
if (!input) {
icli_err_printf("Unable to open file %s:%m\n", argv[0]);
return ICLI_ERR;
icli_err_printf("Unable to open file %s:%m\n", fname);
return -1;
}

ssize_t read;
char *line = NULL;
char *stripped_line;
size_t len = 0;
enum icli_ret ret = ICLI_OK;
int ret = 0;

while ((read = getline(&line, &len, input)) != -1) {
stripped_line = stripwhite(line);
if (*stripped_line && *stripped_line != '#') {
icli_printf("Executing: \"%s\"\n", stripped_line);
ret = icli_execute_line(stripped_line);
if (ret != ICLI_OK)
if (ret)
goto out;
}
}
Expand All @@ -667,6 +667,15 @@ static enum icli_ret icli_execute(char *argv[], int argc UNUSED, void *context U
return ret;
}

static enum icli_ret icli_execute(char *argv[], int argc UNUSED, void *context UNUSED)
{
int ret = icli_exec_script(argv[0]);
if (ret)
return ICLI_ERR;

return ICLI_OK;
}

static int icli_init_default_cmds(struct icli_command *parent)
{
struct icli_command_params params[] =
Expand Down
8 changes: 7 additions & 1 deletion icli.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Iguazio.io Systems Ltd.
* Copyright 2019-2020 Iguazio.io Systems Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License") with
* an addition restriction as set forth herein. You may not use this
Expand Down Expand Up @@ -192,3 +192,9 @@ int icli_execute_line(char *line);
* @return 0 on success, -1 on error
*/
int icli_reset_arguments(struct icli_command *cmd, struct icli_arg *argv);

/**
* Execute a script
* @param fname the path to the script
*/
int icli_exec_script(const char *fname);
1 change: 1 addition & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ def test_icli(icli):

assert not icli.isalive()


if __name__ == '__main__':
sys.exit(pytest.main(sys.argv[0] + " -s " + ' '.join(sys.argv[3:])))

0 comments on commit d7a6a81

Please sign in to comment.