srp_load.c 4.4 KB

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