srp_load.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <getopt.h>
  5. #include <faux/faux.h>
  6. #include <faux/str.h>
  7. #include <faux/file.h>
  8. #include <faux/argv.h>
  9. #include <libyang/log.h>
  10. #include <sysrepo.h>
  11. #include <sysrepo/xpath.h>
  12. #include "klish_plugin_sysrepo.h"
  13. #ifndef VERSION
  14. #define VERSION "1.0.0"
  15. #endif
  16. #define DEFAULT_USER "root"
  17. #define DEFAULT_DATASTORE SR_DS_CANDIDATE
  18. #define DEFAULT_OP 's'
  19. typedef struct cmd_opts_s {
  20. char *cfg; // Configuration options
  21. char *file; // File to load
  22. char *user; // NACM user
  23. char *current_path; // Current sysrepo path
  24. char op; // Operation to execute 's'(set)/'d'(del)
  25. bool_t verbose;
  26. bool_t stop_on_error;
  27. sr_datastore_t datastore;
  28. } cmd_opts_t;
  29. // Command line options
  30. static cmd_opts_t *cmd_opts_init(void);
  31. static void cmd_opts_free(cmd_opts_t *opts);
  32. static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts);
  33. static void help(int status, const char *argv0);
  34. int main(int argc, char **argv)
  35. {
  36. int ret = -1;
  37. pline_opts_t opts;
  38. cmd_opts_t *cmd_opts = NULL;
  39. int fd = STDIN_FILENO;
  40. faux_argv_t *cur_path = NULL;
  41. // Command line options parsing
  42. cmd_opts = cmd_opts_init();
  43. if (cmd_opts_parse(argc, argv, cmd_opts) < 0) {
  44. fprintf(stderr, "Error: Illegal command line options\n");
  45. goto out;
  46. }
  47. // Open input file
  48. if (cmd_opts->file) {
  49. fd = open(cmd_opts->file, O_RDONLY, 0);
  50. if (fd < 0) {
  51. fprintf(stderr, "Error: Can't open \"%s\"\n", cmd_opts->file);
  52. goto out;
  53. }
  54. }
  55. // Get pline options
  56. pline_opts_init(&opts);
  57. if (cmd_opts->cfg)
  58. pline_opts_parse_file(cmd_opts->cfg, &opts);
  59. // Turn off libyang warnings
  60. ly_log_level(LY_LLERR);
  61. ly_log_options(LY_LOSTORE);
  62. // Prepare argv structure for current sysrepo path
  63. if (cmd_opts->current_path) {
  64. cur_path = faux_argv_new();
  65. faux_argv_parse(cur_path, cmd_opts->current_path);
  66. }
  67. ret = srp_mass_op(cmd_opts->op, fd, cmd_opts->datastore, cur_path,
  68. &opts, cmd_opts->user, cmd_opts->stop_on_error);
  69. out:
  70. if (cur_path)
  71. faux_argv_free(cur_path);
  72. if (cmd_opts->file)
  73. close(fd);
  74. cmd_opts_free(cmd_opts);
  75. return ret;
  76. }
  77. static cmd_opts_t *cmd_opts_init(void)
  78. {
  79. cmd_opts_t *opts = NULL;
  80. opts = faux_zmalloc(sizeof(*opts));
  81. assert(opts);
  82. // Initialize
  83. opts->verbose = BOOL_FALSE;
  84. opts->stop_on_error = BOOL_FALSE;
  85. opts->cfg = NULL;
  86. opts->file = NULL;
  87. opts->user = faux_str_dup(DEFAULT_USER);
  88. opts->op = DEFAULT_OP;
  89. opts->datastore = DEFAULT_DATASTORE;
  90. opts->current_path = NULL;
  91. return opts;
  92. }
  93. static void cmd_opts_free(cmd_opts_t *opts)
  94. {
  95. if (!opts)
  96. return;
  97. faux_str_free(opts->cfg);
  98. faux_str_free(opts->file);
  99. faux_str_free(opts->user);
  100. faux_str_free(opts->current_path);
  101. faux_free(opts);
  102. }
  103. static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
  104. {
  105. static const char *shortopts = "hf:veu:d:p:o:";
  106. static const struct option longopts[] = {
  107. {"conf", 1, NULL, 'f'},
  108. {"help", 0, NULL, 'h'},
  109. {"verbose", 0, NULL, 'v'},
  110. {"user", 1, NULL, 'u'},
  111. {"stop-on-error", 0, NULL, 'e'},
  112. {"datastore", 1, NULL, 'd'},
  113. {"current-path", 1, NULL, 'p'},
  114. {"operation", 1, NULL, 'o'},
  115. {NULL, 0, NULL, 0}
  116. };
  117. optind = 1;
  118. while(1) {
  119. int opt = 0;
  120. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  121. if (-1 == opt)
  122. break;
  123. switch (opt) {
  124. case 'v':
  125. opts->verbose = BOOL_TRUE;
  126. break;
  127. case 'e':
  128. opts->stop_on_error = BOOL_TRUE;
  129. break;
  130. case 'h':
  131. help(0, argv[0]);
  132. _exit(0);
  133. break;
  134. case 'u':
  135. faux_str_free(opts->user);
  136. opts->user = faux_str_dup(optarg);
  137. break;
  138. case 'f':
  139. faux_str_free(opts->cfg);
  140. opts->cfg = faux_str_dup(optarg);
  141. break;
  142. case 'o':
  143. opts->op = optarg[0];
  144. if ((opts->op != 's') && (opts->op != 'd')) {
  145. fprintf(stderr, "Error: Illegal operation '%c'\n",
  146. opts->op);
  147. help(-1, argv[0]);
  148. _exit(-1);
  149. }
  150. break;
  151. case 'd':
  152. if (!kly_str2ds(optarg, strlen(optarg), &opts->datastore))
  153. return BOOL_FALSE;
  154. break;
  155. case 'p':
  156. faux_str_free(opts->current_path);
  157. opts->current_path = faux_str_dup(optarg);
  158. break;
  159. default:
  160. help(-1, argv[0]);
  161. _exit(-1);
  162. break;
  163. }
  164. }
  165. // Input file
  166. if(optind < argc) {
  167. faux_str_free(opts->file);
  168. opts->file = faux_str_dup(argv[optind]);
  169. }
  170. return 0;
  171. }
  172. static void help(int status, const char *argv0)
  173. {
  174. const char *name = NULL;
  175. if (!argv0)
  176. return;
  177. // Find the basename
  178. name = strrchr(argv0, '/');
  179. if (name)
  180. name++;
  181. else
  182. name = argv0;
  183. if (status != 0) {
  184. fprintf(stderr, "Try `%s -h' for more information.\n",
  185. name);
  186. } else {
  187. printf("Version : %s\n", VERSION);
  188. printf("Usage : %s [options] [filename]\n", name);
  189. printf("Load mass of config strings to Sysrepo repository\n");
  190. printf("Options :\n");
  191. printf("\t-h, --help Print this help\n");
  192. printf("\t-v, --verbose Be verbose\n");
  193. printf("\t-e, --stop-on-error Stop script execution on error\n");
  194. printf("\t-u <name>, --user=<name> NACM user name\n");
  195. printf("\t-f <path>, --conf=<path> Config file with parsing settings\n");
  196. printf("\t-o <op>, --operation=<op> Operation to perform\n");
  197. printf("\t\t's' Set (default)\n");
  198. printf("\t\t'd' Delete\n");
  199. printf("\t-d <ds>, --datastore=<ds> Datastore (Default is 'candidate')\n");
  200. printf("\t-p <sr-path>, --current-path=<sr-path> Current sysrepo path\n");
  201. }
  202. }