srp_load.c 4.7 KB

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