Added examples to the cli

The function has been renamed and has become more universal
Added error checking (incorrect input)
This commit is contained in:
Хроменок Роман Владимирович 2024-05-23 21:01:19 +02:00 committed by Olof Hagsand
parent b81e18141d
commit 20750cb616
3 changed files with 27 additions and 16 deletions

View file

@ -789,34 +789,42 @@ cli_set_mode(clixon_handle h,
* The function returns the exit code of the Python script, or a value of (-1) on error.
*/
int
cli_start_python3(clixon_handle h,
cli_start_program(clixon_handle h,
cvec *cvv,
cvec *argv)
{
int pid = 0;
int retval = -1;
char *script_path = NULL;
char *runner = "python3";
char *runner = NULL;
struct passwd *pw = NULL;
if (cvec_len(argv) > 1){
/* Check parameters */
if (cvec_len(argv) == 0){
clixon_err(OE_PLUGIN, EINVAL, "Can not found argument in a function");
goto done;
}
if ((cvec_len(argv) >= 2) && (cvec_len(cvv) >= 2)){
clixon_err(OE_PLUGIN, EINVAL, "A lot of arguments");
goto done;
}
if (cvec_len(argv) == 1){
script_path = cv_string_get(cvec_i(argv, 0));
if ((cvec_len(argv) == 2) && (cvec_len(cvv) == 2)){
clixon_err(OE_PLUGIN, EINVAL, "You cannot use 2 arguments in a function and 1 argument in a vector");
goto done;
}
/* get data */
if (cvec_len(argv) == 1){
runner = cv_string_get(cvec_i(argv, 0));
}
if (cvec_len(argv) == 2){
runner = cv_string_get(cvec_i(argv, 0));
script_path = cv_string_get(cvec_i(argv, 1));
}
if (cvec_len(cvv) == 2){
script_path = cv_string_get(cvec_i(cvv, 1));
}
if ((cvec_len(cvv) == 2) && (cvec_len(argv) == 1)){
clixon_err(OE_PLUGIN, EINVAL, "Both the arguments of the function and the vector of values are specified\n");
goto done;
}
if ((pw = getpwuid(getuid())) == NULL){
clixon_err(OE_PLUGIN, errno, "getpwuid");
goto done;
@ -826,6 +834,7 @@ cli_start_python3(clixon_handle h,
goto done;
}
/* main run */
if ((pid = fork()) == 0) {
execlp(runner, runner, script_path, NULL);
clixon_err(OE_PLUGIN, errno, "Error run script");

View file

@ -96,7 +96,7 @@ int cli_debug_cli(clixon_handle h, cvec *vars, cvec *argv);
int cli_debug_backend(clixon_handle h, cvec *vars, cvec *argv);
int cli_debug_restconf(clixon_handle h, cvec *vars, cvec *argv);
int cli_set_mode(clixon_handle h, cvec *vars, cvec *argv);
int cli_start_python3(clixon_handle h, cvec *vars, cvec *argv);
int cli_start_program(clixon_handle h, cvec *vars, cvec *argv);
int cli_start_shell(clixon_handle h, cvec *vars, cvec *argv);
int cli_quit(clixon_handle h, cvec *vars, cvec *argv);
int cli_commit(clixon_handle h, cvec *vars, cvec *argv);

View file

@ -72,10 +72,12 @@ shell("System command"), cli_start_shell("bash");{
<source:rest>("Single shell command"), cli_start_shell("bash");
}
run_python3("Run python"), cli_start_python3();
run_python3_script("Run python script"), cli_start_python3("/tmp/test.py");
run_python3_script_manual("Run python script manual") <source:rest>("Path to script"), cli_start_python3();
run_python3_script_manual_err("Run python script") <source:rest>("Path to script"), cli_start_python3("/tmp/test2.py");
run_program_err("Run program"), cli_start_program();
run_program_python3("Run program"), cli_start_program("python3");
run_program_python3_source_arg("Run program"), cli_start_program("python3", "/tmp/test.py");
run_program_python3_source_arg_vector("Run program") <source:rest>("Path program"), cli_start_program("python3");
run_program_python3_source_arg_vector_err("Run program") <source:rest>("Path program"), cli_start_program("python3", "/tmp/test2.py");
run_program_bash("Run program"), cli_start_program("bash");
copy("Copy and create a new object") {
running("Copy from running db") startup("Copy to startup config"), db_copy("running", "startup");