CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 
7 #include <algorithm>
8 #include <cstdlib>
9 #include <string>
10 #include <vector>
11 
12 #include "logging.h"
13 #include "publish/cmd_abort.h"
14 #include "publish/cmd_diff.h"
15 #include "publish/cmd_enter.h"
16 #include "publish/cmd_hash.h"
17 #include "publish/cmd_help.h"
18 #include "publish/cmd_info.h"
19 #include "publish/cmd_mkfs.h"
21 #include "publish/cmd_zpipe.h"
22 #include "publish/command.h"
23 #include "publish/except.h"
24 
25 using namespace std; // NOLINT
26 
27 
28 static void PrintVersion() {
29  LogCvmfs(kLogCvmfs, kLogStdout, "CernVM-FS Server Tool %s", VERSION);
30 }
31 
32 static void Usage(const std::string &progname,
33  const publish::CommandList &clist)
34 {
36  "CernVM-FS Server Tool %s\n"
37  "NOTE: This utility is for CernVM-FS internal use only for the time being!"
38  "\n\n"
39  "Usage:\n"
40  "------\n"
41  " %s COMMAND [options] <parameters>\n\n"
42  "Supported Commmands\n"
43  "-------------------\n",
44  VERSION, progname.c_str());
45  const vector<publish::Command *> commands = clist.commands();
46 
47  string::size_type max_len = 0;
48  for (unsigned i = 0; i < commands.size(); ++i) {
49  if (commands[i]->IsHidden()) continue;
50  max_len = std::max(commands[i]->GetName().length(), max_len);
51  }
52 
53  for (unsigned i = 0; i < commands.size(); ++i) {
54  if (commands[i]->IsHidden()) continue;
56  commands[i]->GetName().c_str());
57  for (unsigned p = commands[i]->GetName().length(); p < max_len; ++p)
59  LogCvmfs(kLogCvmfs, kLogStdout, " %s", commands[i]->GetBrief().c_str());
60  }
61 
63 }
64 
65 
66 int main(int argc, char **argv) {
67  publish::CommandList commands;
68  commands.TakeCommand(new publish::CmdMkfs());
69  commands.TakeCommand(new publish::CmdTransaction());
70  commands.TakeCommand(new publish::CmdAbort());
71  commands.TakeCommand(new publish::CmdEnter());
72  commands.TakeCommand(new publish::CmdInfo());
73  commands.TakeCommand(new publish::CmdDiff());
74  commands.TakeCommand(new publish::CmdHelp(&commands));
75  commands.TakeCommand(new publish::CmdZpipe());
76  commands.TakeCommand(new publish::CmdHash());
77 
78  if (argc < 2) {
79  Usage(argv[0], commands);
80  return 1;
81  }
82  if ((string(argv[1]) == "--help") || (string(argv[1]) == "-h")) {
83  Usage(argv[0], commands);
84  return 0;
85  }
86  if ((string(argv[1]) == "--version") || (string(argv[1]) == "-v")) {
87  PrintVersion();
88  return 0;
89  }
90 
91  publish::Command *command = commands.Find(argv[1]);
92  if (command == NULL) {
93  LogCvmfs(kLogCvmfs, kLogStderr, "unknown command: %s", argv[1]);
94  Usage(argv[0], commands);
95  return 1;
96  }
97 
98  try {
99  publish::Command::Options options = command->ParseOptions(argc, argv);
100  return command->Main(options);
101  } catch (const publish::EPublish& e) {
103  LogCvmfs(kLogCvmfs, kLogStderr, "Invocation error: %s", e.msg().c_str());
106  "Missing dependency: %s", e.msg().c_str());
107  } else if (e.failure() == publish::EPublish::kFailPermission) {
109  "Permission error: %s", e.msg().c_str());
110  } else {
111  LogCvmfs(kLogCvmfs, kLogStderr, "(unexpected termination) %s", e.what());
112  }
113  return 1;
114  }
115 }
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
static void Usage(const char *progname)
static void PrintVersion()
Definition: main.cc:28
std::string msg() const
Definition: except.h:46
void TakeCommand(Command *command)
Definition: command.cc:134
Command * Find(const std::string &name)
Definition: command.cc:123
const std::vector< Command * > & commands() const
Definition: command.h:203
int main()
Definition: helper_allow.cc:16
Options ParseOptions(int argc, char **argv)
Definition: command.cc:33
virtual int Main(const Options &options)=0
EFailures failure() const
Definition: except.h:45