dcdbconfig.cpp 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * dcdbconfig.cpp
 *
 *  Created on: Jan 06, 2015
 *      Author: Axel Auweter
 */

#include <iostream>
#include <cstring>

#include <unistd.h>

#include "cassandra.h"

#include "useraction.h"



void usage(int argc, char* argv[])
{
  std::cout << "Usage: " << argv[0] << " [-h host] <command> [<arguments> ... ]" << std::endl << std::endl;
  std::cout << "Valid commands are: " << std::endl;
Axel Auweter's avatar
Axel Auweter committed
23
  std::cout << "    HELP <command name> - print help for given command" << std::endl;
24
  std::cout << "    DB                  - perform low-level database functions" << std::endl;
Axel Auweter's avatar
Axel Auweter committed
25
  std::cout << "    SENSOR              - list and configure sensors" << std::endl;
26
27
28
29
30
31
32
33
34
35
}

int main(int argc, char* argv[])
{
  /* Check command line parameters */
  if (argc < 2) {
      usage(argc, argv);
      exit(EXIT_FAILURE);
  }

36
  int ret;
37
  const char *host = "localhost";
38
  while ((ret=getopt(argc, argv, "+h:"))!=-1) {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
      switch(ret) {
          case 'h':
              host = optarg;
              break;
          default:
              usage(argc, argv);
              exit(EXIT_FAILURE);
      }
  }

  if (optind >= argc) {
      std::cout << "Missing command!" << std::endl;
      usage(argc, argv);
      exit(EXIT_FAILURE);
  }

  /* Process user command */
  if (strcasecmp(argv[optind], "help") == 0) {
      /* Help is special: either we do general usage or we trigger the class factory and run the printHelp() function */
      if (optind + 1 >= argc) {
          usage (argc, argv);
      }
      else {
Axel Auweter's avatar
Axel Auweter committed
62
          auto action = UserActionFactory::getAction(argv[optind+1]);
63
64
65
66
67
68
69
70
71
72
73
          if (action) {
              action->printHelp(argc, argv);
          }
          else {
              std::cout << "Cannot provide help for unknown command: " << argv[optind+1] << std::endl;
              exit(EXIT_FAILURE);
          }
      }
  }
  else {
      /* If the command is not help, we try to instantiate the respective class through the factory and process the command */
Axel Auweter's avatar
Axel Auweter committed
74
      auto action = UserActionFactory::getAction(argv[optind]);
75
76
77
78
79
80
81
82
83
84
85
86
87
      if (action) {
          return action->executeCommand(argc, argv, optind, host);
      }
      else {
          std::cout << "Unknwon command: " << argv[1] << std::endl;
          usage(argc, argv);
          exit(EXIT_FAILURE);
      }
  }

  /* Shouldn't fall through here */
  return 0;
}