Commit c6df35d5 authored by Steven Murray's avatar Steven Murray
Browse files

Removed duplicate code from comand-line tools

parent 885c37fc
......@@ -17,6 +17,7 @@
*/
#include "catalogue/CmdLineTool.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <unistd.h>
......@@ -44,7 +45,7 @@ CmdLineTool::~CmdLineTool() noexcept {
//------------------------------------------------------------------------------
// getUsername
//------------------------------------------------------------------------------
std::string CmdLineTool::getUsername() const {
std::string CmdLineTool::getUsername() {
char buf[256];
if(getlogin_r(buf, sizeof(buf))) {
......@@ -57,7 +58,7 @@ std::string CmdLineTool::getUsername() const {
//------------------------------------------------------------------------------
// getHostname
//------------------------------------------------------------------------------
std::string CmdLineTool::getHostname() const {
std::string CmdLineTool::getHostname() {
char buf[256];
if(gethostname(buf, sizeof(buf))) {
......@@ -68,5 +69,36 @@ std::string CmdLineTool::getHostname() const {
}
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int CmdLineTool::main(const int argc, char *const *const argv) {
bool cmdLineNotParsed = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::CommandLineNotParsed &ue) {
errorMessage = ue.getMessage().str();
cmdLineNotParsed = true;
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
m_err << "Aborting: " << errorMessage << std::endl;
if(cmdLineNotParsed) {
m_err << std::endl;
printUsage(m_err);
}
return 1;
}
} // namespace catalogue
} // namespace cta
......@@ -44,6 +44,18 @@ public:
*/
virtual ~CmdLineTool() noexcept = 0;
/**
* The object's implementation of main() that should be called from the main()
* of the program.
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
int main(const int argc, char *const *const argv);
protected:
/**
* An exception throwing version of main().
*
......@@ -53,7 +65,12 @@ public:
*/
virtual int exceptionThrowingMain(const int argc, char *const *const argv) = 0;
protected:
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
virtual void printUsage(std::ostream &os) = 0;
/**
* Standard input stream.
......@@ -75,14 +92,14 @@ protected:
*
* @return The name of the user running the command-line tool.
*/
std::string getUsername() const;
static std::string getUsername();
/**
* Returns the name of the host on which the command-line tool is running.
*
* @return The name of the host on which the command-line tool is running.
*/
std::string getHostname() const;
static std::string getHostname();
}; // class CmdLineTool
......
......@@ -48,7 +48,7 @@ int CreateAdminHostCmd::exceptionThrowingMain(const int argc, char *const *const
const CreateAdminHostCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
CreateAdminHostCmdLineArgs::printUsage(m_out);
printUsage(m_out);
return 0;
}
......@@ -61,5 +61,12 @@ int CreateAdminHostCmd::exceptionThrowingMain(const int argc, char *const *const
return 0;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void CreateAdminHostCmd::printUsage(std::ostream &os) {
CreateAdminHostCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
......@@ -44,6 +44,8 @@ public:
*/
~CreateAdminHostCmd() noexcept;
private:
/**
* An exception throwing version of main().
*
......@@ -51,7 +53,14 @@ public:
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
int exceptionThrowingMain(const int argc, char *const *const argv);
int exceptionThrowingMain(const int argc, char *const *const argv) override;
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
}; // class CreateAdminHostCmd
......
......@@ -17,61 +17,13 @@
*/
#include "catalogue/CreateAdminHostCmd.hpp"
#include "catalogue/CreateAdminHostCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
bool cmdLineNotParsed = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::CommandLineNotParsed &ue) {
errorMessage = ue.getMessage().str();
cmdLineNotParsed = true;
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
if(cmdLineNotParsed) {
std::cerr << std::endl;
catalogue::CreateAdminHostCmdLineArgs::printUsage(std::cerr);
}
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::CreateAdminHostCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
cta::catalogue::CreateAdminHostCmd cmd(std::cin, std::cout, std::cerr);
return cmd.main(argc, argv);
}
......@@ -48,7 +48,7 @@ int CreateAdminUserCmd::exceptionThrowingMain(const int argc, char *const *const
const CreateAdminUserCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
CreateAdminUserCmdLineArgs::printUsage(m_out);
printUsage(m_out);
return 0;
}
......@@ -61,5 +61,12 @@ int CreateAdminUserCmd::exceptionThrowingMain(const int argc, char *const *const
return 0;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void CreateAdminUserCmd::printUsage(std::ostream &os) {
CreateAdminUserCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
......@@ -44,6 +44,8 @@ public:
*/
~CreateAdminUserCmd() noexcept;
private:
/**
* An exception throwing version of main().
*
......@@ -51,7 +53,14 @@ public:
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
int exceptionThrowingMain(const int argc, char *const *const argv);
int exceptionThrowingMain(const int argc, char *const *const argv) override;
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
}; // class CreateAdminUserCmd
......
......@@ -17,61 +17,13 @@
*/
#include "catalogue/CreateAdminUserCmd.hpp"
#include "catalogue/CreateAdminUserCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
bool cmdLineNotParsed = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::CommandLineNotParsed &ue) {
errorMessage = ue.getMessage().str();
cmdLineNotParsed = true;
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
if(cmdLineNotParsed) {
std::cerr << std::endl;
catalogue::CreateAdminUserCmdLineArgs::printUsage(std::cerr);
}
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::CreateAdminUserCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
cta::catalogue::CreateAdminUserCmd cmd(std::cin, std::cout, std::cerr);
return cmd.main(argc, argv);
}
......@@ -48,7 +48,7 @@ int CreateSchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
const CreateSchemaCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
cmdLineArgs.printUsage(m_out);
printUsage(m_out);
return 0;
}
......@@ -103,5 +103,12 @@ bool CreateSchemaCmd::tableExists(const std::string tableName, rdbms::Conn &conn
return false;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void CreateSchemaCmd::printUsage(std::ostream &os) {
CreateSchemaCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
......@@ -44,6 +44,8 @@ public:
*/
~CreateSchemaCmd() noexcept;
private:
/**
* An exception throwing version of main().
*
......@@ -53,7 +55,12 @@ public:
*/
int exceptionThrowingMain(const int argc, char *const *const argv) override;
private:
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
/**
* Returns true if the table with the specified name exists in the database
......
......@@ -17,61 +17,13 @@
*/
#include "catalogue/CreateSchemaCmd.hpp"
#include "catalogue/CreateSchemaCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
bool cmdLineNotParsed = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::CommandLineNotParsed &ue) {
errorMessage = ue.getMessage().str();
cmdLineNotParsed = true;
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
if(cmdLineNotParsed) {
std::cerr << std::endl;
catalogue::CreateSchemaCmdLineArgs::printUsage(std::cerr);
}
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::CreateSchemaCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
cta::catalogue::CreateSchemaCmd cmd(std::cin, std::cout, std::cerr);
return cmd.main(argc, argv);
}
......@@ -47,7 +47,7 @@ int DeleteAllCatalogueDataCmd::exceptionThrowingMain(const int argc, char *const
const DeleteAllCatalogueDataCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
DeleteAllCatalogueDataCmdLineArgs::printUsage(m_out);
printUsage(m_out);
return 0;
}
......@@ -213,5 +213,12 @@ void DeleteAllCatalogueDataCmd::deleteMountPolicies(Catalogue &catalogue) {
m_out << "Deleted " << mountPolicies.size() << " mount policies" << std::endl;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::printUsage(std::ostream &os) {
DeleteAllCatalogueDataCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
......@@ -45,6 +45,8 @@ public:
*/
~DeleteAllCatalogueDataCmd() noexcept;
private:
/**
* An exception throwing version of main().
*
......@@ -54,7 +56,12 @@ public:
*/
int exceptionThrowingMain(const int argc, char *const *const argv) override;
private:
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
/**
* Deletes all of the rows of all of the tables in the specified catalogue
......
......@@ -17,61 +17,13 @@
*/
#include "catalogue/DeleteAllCatalogueDataCmd.hpp"
#include "catalogue/DeleteAllCatalogueDataCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
bool cmdLineNotParsed = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::CommandLineNotParsed &ue) {
errorMessage = ue.getMessage().str();
cmdLineNotParsed = true;
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
if(cmdLineNotParsed) {
std::cerr << std::endl;
catalogue::DeleteAllCatalogueDataCmdLineArgs::printUsage(std::cerr);
}
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::DeleteAllCatalogueDataCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
cta::catalogue::DeleteAllCatalogueDataCmd cmd(std::cin, std::cout, std::cerr);
return cmd.main(argc, argv);
}
......@@ -50,7 +50,7 @@ int DropSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv
const DropSchemaCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
DropSchemaCmdLineArgs::printUsage(m_out);
printUsage(m_out);
return 0;
}
......@@ -123,5 +123,12 @@ void DropSchemaCmd::dropCatalogueSchema(const rdbms::Login &dbLogin) {
}
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DropSchemaCmd::printUsage(std::ostream &os) {
DropSchemaCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
......@@ -46,6 +46,8 @@ public:
*/
~DropSchemaCmd() noexcept;
private:
/**
* An exception throwing version of main().
*
......@@ -55,7 +57,12 @@ public:
*/
int exceptionThrowingMain(const int argc, char *const *const argv) override;
private:
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
/**
* Unconditionally drops the schema of the catalogue database associated with
......
......@@ -17,61 +17,13 @@
*/
#include "catalogue/DropSchemaCmd.hpp"
#include "catalogue/DropSchemaCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);