srp_load.c 4.6 KB

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