srp_load.c 3.8 KB

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