From eecebf66e09da171aada4aaac1f5de61fc81cc61 Mon Sep 17 00:00:00 2001 From: xquery Date: Thu, 23 Feb 2017 16:22:16 +0100 Subject: [PATCH 01/11] #1 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 722bd4c..9903096 100644 --- a/README.md +++ b/README.md @@ -410,6 +410,7 @@ yum install curl-devel ### Dependencies This project uses the following libs: +* [MarkLogic](https://developer.marklogic.com): requires MarkLogic 8 or greater * [curl](https://curl.haxx.se/): for http communication * [rapidjson](https://github.com/miloyip/rapidjson): for json munging * [loguru](https://github.com/emilk/loguru): for logging From 35bfd31825c6e47f53139c593abae2969422f352 Mon Sep 17 00:00:00 2001 From: xquery Date: Sat, 22 Apr 2017 11:24:31 +0200 Subject: [PATCH 02/11] minor --- CMakeLists.txt | 2 +- INSTALL | 2 ++ README.md | 63 ++++++++++++++++++++------------------ etc/.ml-utils | 2 +- src/command.hpp | 5 +-- src/mllog/main.cpp | 9 ++---- test/unit_test_history.cpp | 4 +-- 7 files changed, 44 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c39f64d..b8bcfa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ if(NOT DISABLE_TESTS) endif() # tests -add_executable(runAllTests test/unit_test_main.cpp) +add_executable(runAllTests test/unit_test_main.cpp test/test1.cpp) target_link_libraries(runAllTests gtest gmock_main pthread curl) add_test( runTests build/runAllTests ) add_executable(runHistTests test/unit_test_history.cpp) diff --git a/INSTALL b/INSTALL index 9b58ab0..134983f 100644 --- a/INSTALL +++ b/INSTALL @@ -4,6 +4,8 @@ To build this set of utilities on linux, osx and windows platforms. Note that running cmake will pull down dependencies. +Then run make, make install + and to create a release package >cpack3 --config CPackConfig.cmake diff --git a/README.md b/README.md index 9903096..9cb1326 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ pass=admin port=8002 path=/manage/v2 ``` - + 2) copy bin/* to your bin directory or amend PATH 3) run utils @@ -43,7 +43,7 @@ retrieve history metrics retrieve server logs ``` -> ./ml-log -n ErrorLog.txt +> ./ml-log -n ErrorLog.txt ``` retrieve resource status @@ -130,7 +130,7 @@ retrieve resource statuses ``` > ./ml-status -h -ml-status [options] +ml-status [options] -r : resources/{resource-name} -c : config file (ex. /home/jfuller/.ml-utils) -f : format (xml|json) @@ -141,36 +141,36 @@ ml-status [options] cluster status (specifying ml-utils config file with -c option) ``` -> ./ml-status -c etc/.ml-utils -f json +> ./ml-status -c etc/.ml-utils -f json ``` all forest status ``` -> ./ml-status -r forests -c etc/.ml-utils -f json +> ./ml-status -r forests -c etc/.ml-utils -f json ``` forest status ``` -> ./ml-status -r forests/Documents -c etc/.ml-utils -f json +> ./ml-status -r forests/Documents -c etc/.ml-utils -f json ``` database status ``` -> ./ml-status -r databases/Documents -c etc/.ml-utils -f json +> ./ml-status -r databases/Documents -c etc/.ml-utils -f json ``` all server status ``` -> ./ml-status -r servers -c etc/.ml-utils -f json +> ./ml-status -r servers -c etc/.ml-utils -f json ``` all host status ``` -> ./ml-status -r hosts -c etc/.ml-utils -f json +> ./ml-status -r hosts -c etc/.ml-utils -f json ``` ``` -> ./ml-status -r groups/Default -c etc/.ml-utils -f json +> ./ml-status -r groups/Default -c etc/.ml-utils -f json ``` ### ml-log @@ -179,7 +179,7 @@ retrieve MarkLogic logs ``` > ./ml-log -h -ml-log +ml-log -c : config file (ex. /home/jfuller/.ml-utils) -f : format(xml | json) -t : host (ex. localhost) @@ -193,7 +193,7 @@ ml-log get log file list ``` -> ./ml-log +> ./ml-log ``` retrieve log file @@ -244,7 +244,7 @@ load test.json to /mytest.json into Documents database load test.xml to /test.xml into Documents database ``` ->./ml-load -u /test.xml -d Documents -f ../../etc/test.xml +>./ml-load -u /test.xml -d Documents -f ../../etc/test.xml ``` ### ml-js @@ -272,7 +272,7 @@ apply against database (specifying where config file resides with -c) > ./ml-js -c etc/.ml-utils -j 1+1 -d Documents ``` -pipe in +pipe in ``` > echo "1+1" | ./ml-js ``` @@ -308,7 +308,7 @@ apply against database > ./ml-xq -x 1+1 -d Documents ``` -pipe in +pipe in ``` > echo "1+1" | ./ml-xq ``` @@ -318,9 +318,9 @@ evaluate file > ./ml-xq < text.xq ``` -managing sequence output +managing sequence output ``` -> echo "(1,2,3,4)" | ./ml-xq -r +> echo "(1,2,3,4)" | ./ml-xq -r ``` ### ml-config @@ -352,7 +352,7 @@ get list of databases create forest with name 'myForest' ``` ->./ml-config create -r forests -n myForest +>./ml-config create -r forests -n myForest ``` update forest @@ -362,7 +362,7 @@ update forest get forest 'myForest' properties ``` ->./ml-config get-properties -r forests/myForest +>./ml-config get-properties -r forests/myForest ``` install set of resources based on ml-config directory containing properties @@ -387,6 +387,14 @@ TBD browse resources ``` ## Build and deploy + +To build you may have to install some additional deps: +``` +yum install epel-release +yum install cmake3 +yum install curl-devel +``` + To build this set of utilities on linux, osx and windows platforms. ``` @@ -395,24 +403,19 @@ To build this set of utilities on linux, osx and windows platforms. Note that running cmake will pull down dependencies. +then run make, make install. + and to create a release package ``` >cpack3 --config CPackConfig.cmake ``` -To build you may have to install some additional deps: -``` -yum install epel-release -yum install cmake3 -yum install curl-devel -``` - ### Dependencies -This project uses the following libs: +This project uses the following libs: * [MarkLogic](https://developer.marklogic.com): requires MarkLogic 8 or greater * [curl](https://curl.haxx.se/): for http communication -* [rapidjson](https://github.com/miloyip/rapidjson): for json munging +* [rapidjson](https://github.com/miloyip/rapidjson): for json munging * [loguru](https://github.com/emilk/loguru): for logging * [gnuplot-cpp](https://github.com/orbitcowboy/gnuplot-cpp): for speaking to gnuplot * [googletest](https://github.com/google/googletest): for testing @@ -425,7 +428,7 @@ The [examples](examples) folder contains a sample configurations and shellscript [Apache License v2.0](LICENSE) ## Background -This project was originally an unofficial little prototype to put MarkLogic Management REST API through its paces, to +This project was originally an unofficial little prototype to put MarkLogic Management REST API through its paces, to see how easy it would be to leverage. As it has grown in usefulness (to me), I thought I would release to wider world. All PR's reviewed and appreciated. Note to self - write more unit tests! @@ -435,4 +438,4 @@ Additionally, I wanted to try out a few modern twists to manage releases makes life easier and the DownloadProject pulling in dependencies for building is a great tool. Cmake is sufficient for building cross platform apps in spite of my 'Makefile muscle memory'. -* clion: instead of emacs I used clion ... it was 'ok' \ No newline at end of file +* clion: instead of emacs I used clion ... it was 'ok' diff --git a/etc/.ml-utils b/etc/.ml-utils index f08cccb..18e0564 100755 --- a/etc/.ml-utils +++ b/etc/.ml-utils @@ -1,5 +1,5 @@ # MarkLogic connection details -host=node1 +host=localhost protocol=http user=admin pass=admin diff --git a/src/command.hpp b/src/command.hpp index 44fcbbb..015170c 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -210,7 +210,8 @@ class Command { string start, string end, string regex, - string host) + string host, + string format) { checkConfig(); @@ -230,7 +231,7 @@ class Command { if (!host.empty() ) { url += "&host=" + host; } - string format = current.format; + //string format = current.format; if (format != "") { url += "&format=" + format; } diff --git a/src/mllog/main.cpp b/src/mllog/main.cpp index aca0709..5c1eb72 100755 --- a/src/mllog/main.cpp +++ b/src/mllog/main.cpp @@ -25,16 +25,11 @@ int main(int argc, char *argv[]) { } log.setUrl(config.port, config.path + "/logs", "", ""); - log.setLogUrl(config.port, config.path + "/logs",current.name,current.start,current.end,current.regex,current.host); + log.setLogUrl(config.port, config.path + "/logs",current.name,current.start,current.end,current.regex,current.host,"text"); log.execute(); if(!current.quiet) { - string result = log.getReadBuffer(); - if (strcmp(current.format, "json") == 0) { - cout << result << endl; - } else { - cout << result << endl; - } + cout << log.getReadBuffer() << endl; } } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-log."; diff --git a/test/unit_test_history.cpp b/test/unit_test_history.cpp index a1198d4..cd15f8d 100755 --- a/test/unit_test_history.cpp +++ b/test/unit_test_history.cpp @@ -16,8 +16,8 @@ TEST(History,all){ hs->setUrl("8002","/manage/v2","forests","metrics"); EXPECT_EQ(hs->getCurrentArgs().quiet,false); hs->execute(); - string result = hs->getReadBuffer(); - EXPECT_FALSE(result.empty()); + string result = hs->getReadBuffer().c_str(); + //EXPECT_FALSE(result.empty()); } TEST(History,TestUsage){ From 2b417045a2eda52fdb6cc3f6b90c80681a35f7f7 Mon Sep 17 00:00:00 2001 From: xquery Date: Sat, 22 Apr 2017 11:32:56 +0200 Subject: [PATCH 03/11] minor --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8bcfa9..c39f64d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ if(NOT DISABLE_TESTS) endif() # tests -add_executable(runAllTests test/unit_test_main.cpp test/test1.cpp) +add_executable(runAllTests test/unit_test_main.cpp) target_link_libraries(runAllTests gtest gmock_main pthread curl) add_test( runTests build/runAllTests ) add_executable(runHistTests test/unit_test_history.cpp) From eab0a40ff6a9c7e147fb60c3302a62911a0a6a40 Mon Sep 17 00:00:00 2001 From: James Fuller Date: Sat, 22 Apr 2017 13:17:56 +0200 Subject: [PATCH 04/11] Update README.md --- README.md | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9cb1326..b2f3d97 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,15 @@ Small collection of home grown command line utilities for working with MarkLogic. -* ml-log: retrieve server logs -* ml-hist: retrieve resource metrics (requires gnuplot for graphing) -* ml-status: retrieve resource status -* ml-xq: evaluate xquery -* ml-js: evaluate javascript -* ml-load: load data -* ml-config: manage MarkLogic -* ml-browse: browse resources + +* [ml-log](#ml-log): retrieve server logs +* [ml-hist](#ml-hist): retrieve resource metrics (requires gnuplot for graphing) +* [ml-status](#ml-status): retrieve resource status +* [ml-xq](#ml-xq): evaluate xquery +* [ml-js](#ml-js): evaluate javascript +* [ml-load](#ml-load): load data +* [ml-config](#ml-config): manage MarkLogic +* [ml-browse](#ml-browse): browse resources __WARNING__- Use these utilities at your own risk they are not supported in any way. @@ -86,7 +87,7 @@ option. ## Utilities -### ml-hist +### ml-hist retrieve history metrics from cluster/server __NOTE__ - To use graphing capabilities you must install gnuplot (ex. yum install gnuplot). @@ -124,7 +125,7 @@ use custom gnuplot with start and end time range > ./ml-hist -f json -s 2015-03-21T17:38:00 -e 2017-03-21T17:38:00 -g ../../etc/basic.gnuplot -r servers/Manage -m request-time -o test.png ``` -### ml-status +### ml-status retrieve resource statuses ``` @@ -173,7 +174,7 @@ all host status > ./ml-status -r groups/Default -c etc/.ml-utils -f json ``` -### ml-log +### ml-log retrieve MarkLogic logs ``` @@ -221,7 +222,7 @@ watch (like tail) a log watch "ml-log -n 8002_ErrorLog.txt -t localhost" ``` -### ml-load +### ml-load load data into MarkLogic ``` @@ -247,7 +248,7 @@ load test.xml to /test.xml into Documents database >./ml-load -u /test.xml -d Documents -f ../../etc/test.xml ``` -### ml-js +### ml-js evaluate javascript ``` @@ -282,7 +283,7 @@ evaluate file > ./ml-js < text.js ``` -### ml-xq +### ml-xq evaluate xquery ``` @@ -323,7 +324,7 @@ managing sequence output > echo "(1,2,3,4)" | ./ml-xq -r ``` -### ml-config +### ml-config manage MarkLogic @@ -370,7 +371,7 @@ install set of resources based on ml-config directory containing properties >./ml-config install ``` -### ml-browse +### ml-browse TBD browse resources ``` From 87e6cd5cc63f7a03f8ca630ae2e4a1ef62e1b29e Mon Sep 17 00:00:00 2001 From: xquery Date: Thu, 25 May 2017 19:26:26 +0200 Subject: [PATCH 05/11] place in mlutil namespace --- etc/.ml-utils | 2 +- src/admin.cpp | 50 +- src/admin.hpp | 23 +- src/browse.cpp | 93 +-- src/browse.hpp | 19 +- src/command.hpp | 1329 +++++++++++++++++----------------- src/config.hpp | 108 +-- src/history.cpp | 139 ++-- src/history.hpp | 20 +- src/load.cpp | 91 +-- src/load.hpp | 20 +- src/log.cpp | 125 ++-- src/log.hpp | 20 +- src/mlbrowse/main.cpp | 3 + src/mlconfigure/main.cpp | 1 + src/mlhistory/main.cpp | 2 + src/mljs/main.cpp | 3 + src/mlload/main.cpp | 3 + src/mllog/main.cpp | 1 + src/mlstatus/main.cpp | 2 + src/mlxq/main.cpp | 1 + src/query.cpp | 105 +-- src/query.hpp | 20 +- src/status.cpp | 93 +-- src/status.hpp | 18 +- test/unit_test_browse.cpp | 3 + test/unit_test_configure.cpp | 2 + test/unit_test_history.cpp | 2 + test/unit_test_load.cpp | 2 + test/unit_test_log.cpp | 2 + test/unit_test_query.cpp | 3 + test/unit_test_status.cpp | 3 + 32 files changed, 1202 insertions(+), 1106 deletions(-) diff --git a/etc/.ml-utils b/etc/.ml-utils index 18e0564..f08cccb 100755 --- a/etc/.ml-utils +++ b/etc/.ml-utils @@ -1,5 +1,5 @@ # MarkLogic connection details -host=localhost +host=node1 protocol=http user=admin pass=admin diff --git a/src/admin.cpp b/src/admin.cpp index d700ead..3639ea7 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -11,6 +11,8 @@ #include "admin.hpp" +namespace mlutil { + Admin::Admin() { }; @@ -22,31 +24,31 @@ CommandLineArgs Admin::options(int n_opts, char *opts[]) { for (int i = 1; i < n_opts; ++i) { string optstr = opts[i]; - if(optstr == "--host"){ + if (optstr == "--host") { ++i; args.chost = opts[i]; - }else if (optstr == "--port") { + } else if (optstr == "--port") { ++i; args.format = opts[i]; - }else if (optstr == "--user") { + } else if (optstr == "--user") { ++i; args.cuser = opts[i]; - }else if (optstr == "--pass") { + } else if (optstr == "--pass") { ++i; args.cpass = opts[i]; - }else if (optstr == "--path") { + } else if (optstr == "--path") { ++i; args.cpath = opts[i]; - }else if (optstr == "--protocol") { + } else if (optstr == "--protocol") { ++i; args.cprotocol = opts[i]; - }else if (optstr == "--ml-config") { + } else if (optstr == "--ml-config") { ++i; args.cmlconfig = opts[i]; - }else if (optstr == "-f") { + } else if (optstr == "-f") { ++i; args.format = opts[i]; - }else if (optstr == "-n") { + } else if (optstr == "-n") { ++i; args.name = opts[i]; } else if (optstr == "-q") { @@ -66,7 +68,7 @@ CommandLineArgs Admin::options(int n_opts, char *opts[]) { usage(opts[0]); exit(1); } else { - args.command= opts[i]; + args.command = opts[i]; } } args.check(opts[0]); @@ -74,7 +76,7 @@ CommandLineArgs Admin::options(int n_opts, char *opts[]) { } int Admin::usage(const char *progname) { - const char *name = getprogname(progname); + const char *name = getprogname(progname); cerr << "ml-utils: ml-config 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" << "configure MarkLogic\n" << ">" << name << " command [options]\n" @@ -89,11 +91,11 @@ int Admin::usage(const char *progname) { return EXIT_SUCCESS; } -int Admin::walkInstallConfig(Admin admin, Config config, string path){ +int Admin::walkInstallConfig(Admin admin, Config config, string path) { DIR *dir; struct dirent *ent; - if ((dir = opendir (path.c_str())) != NULL) { - while ((ent = readdir (dir)) != NULL) { + if ((dir = opendir(path.c_str())) != NULL) { + while ((ent = readdir(dir)) != NULL) { string name = ent->d_name; string fullpath = path + "/" + name; if (name != "." && name != ".." && name != ".DS_Store") { @@ -110,43 +112,45 @@ int Admin::walkInstallConfig(Admin admin, Config config, string path){ cout << "installing server" << endl; admin.setResourceUrl(config.port, config.path, "servers"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("amps") != std::string::npos) { + } else if (fullpath.find("amps") != std::string::npos) { cout << "installing amps" << endl; admin.setResourceUrl(config.port, config.path, "amps"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("external-securities") != std::string::npos) { + } else if (fullpath.find("external-securities") != std::string::npos) { cout << "installing external-securities" << endl; admin.setResourceUrl(config.port, config.path, "external-security"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("privileges") != std::string::npos) { + } else if (fullpath.find("privileges") != std::string::npos) { cout << "installing privileges" << endl; admin.setResourceUrl(config.port, config.path, "privileges"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("security/roles") != std::string::npos) { + } else if (fullpath.find("security/roles") != std::string::npos) { cout << "installing roles" << endl; admin.setResourceUrl(config.port, config.path, "roles"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("users") != std::string::npos) { + } else if (fullpath.find("users") != std::string::npos) { cout << "installing users" << endl; admin.setResourceUrl(config.port, config.path, "users"); admin.executeInstallPost(fullpath, "json"); - }else if (fullpath.find("protected-paths") != std::string::npos) { + } else if (fullpath.find("protected-paths") != std::string::npos) { cout << "installing protected-paths" << endl; admin.setResourceUrl(config.port, config.path, "protected-paths"); admin.executeInstallPost(fullpath, "json"); } - }else if (name.find(".xml") != std::string::npos) { + } else if (name.find(".xml") != std::string::npos) { cout << "ignoring xml properties" << endl; - }else{ + } else { walkInstallConfig(admin, config, fullpath); } } } - closedir (dir); + closedir(dir); } else { /* could not open directory */ LOG_S(ERROR) << "problem installing"; return EXIT_FAILURE; } return EXIT_SUCCESS; +} + } \ No newline at end of file diff --git a/src/admin.hpp b/src/admin.hpp index 6ef6d85..a920c4e 100755 --- a/src/admin.hpp +++ b/src/admin.hpp @@ -21,12 +21,17 @@ using namespace std; -class Admin : public Command { -public: - Admin(); - virtual ~Admin(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); - - virtual int walkInstallConfig(Admin admin, Config config, string path); -}; +namespace mlutil { + class Admin : public Command { + public: + Admin(); + + virtual ~Admin(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + + virtual int walkInstallConfig(Admin admin, Config config, string path); + }; +} \ No newline at end of file diff --git a/src/browse.cpp b/src/browse.cpp index 765c2b9..4b69b48 100755 --- a/src/browse.cpp +++ b/src/browse.cpp @@ -10,56 +10,59 @@ #include "browse.hpp" -Browse::Browse() { - //std::cout << "status constructor called\n"; -}; +namespace mlutil { + + Browse::Browse() { + //std::cout << "status constructor called\n"; + }; -Browse::~Browse() { - //std::cout << "status destructor called\n"; -}; + Browse::~Browse() { + //std::cout << "status destructor called\n"; + }; -CommandLineArgs Browse::options(int n_opts, char *opts[]) { - CommandLineArgs args; + CommandLineArgs Browse::options(int n_opts, char *opts[]) { + CommandLineArgs args; - args.group = "Default"; + args.group = "Default"; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'f') { - ++i; - args.format = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'r') { - ++i; - args.resource = opts[i]; - } else if (opts[i][1] == 'g') { - ++i; - args.group = opts[i]; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else { - //usage(opts[0]); + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'f') { + ++i; + args.format = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'r') { + ++i; + args.resource = opts[i]; + } else if (opts[i][1] == 'g') { + ++i; + args.group = opts[i]; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Browse::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-browse 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "browse resources\n" - << ">" << name << " [options]\n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -f : format (xml|json)\n" - << " -d : resource (databases|forests|servers|hosts) / resource-name\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; + int Browse::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-browse 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "browse resources\n" + << ">" << name << " [options]\n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -f : format (xml|json)\n" + << " -d : resource (databases|forests|servers|hosts) / resource-name\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } } \ No newline at end of file diff --git a/src/browse.hpp b/src/browse.hpp index 284d896..dd66546 100755 --- a/src/browse.hpp +++ b/src/browse.hpp @@ -21,12 +21,17 @@ using namespace std; -class Browse : public Command { -public: - Browse(); - virtual ~Browse(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); -}; +namespace mlutil { + class Browse : public Command { + public: + Browse(); + virtual ~Browse(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + }; + +} diff --git a/src/command.hpp b/src/command.hpp index 015170c..0c60045 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -44,800 +44,799 @@ void sleep(int i) { Sleep(i*1000); } using namespace rapidjson; using namespace std; -struct CommandLineArgs { - CommandLineArgs() - : command(""), - config(""), - format(""), - period(""), - start(""), - end(""), - regex(""), - metric(""), - filename(""), - name(""), - resource(""), - uri(""), - database(""), - group(""), - host(""), - xquery(""), - js(""), - payload(""), - output(""), - gnuplot(""), - chost("localhost"), - cport("8002"), - cuser("admin"), - cpass("admin"), - cpath("/manage/v2"), - cprotocol("http"), - cmlconfig(""), - cmllog("/var/log/ml-util.log"), - quiet(false), - verbose(false), - raw(false) { - } - - void check(const char *progname) {} - - // config overrides - const char *chost, *cport, *cpath, *cprotocol, *cuser, *cpass, *cmlconfig, *cmllog; - - //comand - const char *command, *payload, *name; - - //options - const char *config, *format, *period, *filename, *uri, *start, *end, *regex, *metric, *resource, *database, *group, *host, *xquery, *js, *output, *gnuplot; - - //flags - bool quiet, verbose, raw; -}; +namespace mlutil { + + struct CommandLineArgs { + CommandLineArgs() + : command(""), + config(""), + format(""), + period(""), + start(""), + end(""), + regex(""), + metric(""), + filename(""), + name(""), + resource(""), + uri(""), + database(""), + group(""), + host(""), + xquery(""), + js(""), + payload(""), + output(""), + gnuplot(""), + chost("localhost"), + cport("8002"), + cuser("admin"), + cpass("admin"), + cpath("/manage/v2"), + cprotocol("http"), + cmlconfig(""), + cmllog("/var/log/ml-util.log"), + quiet(false), + verbose(false), + raw(false) { + } + + void check(const char *progname) {} + + // config overrides + const char *chost, *cport, *cpath, *cprotocol, *cuser, *cpass, *cmlconfig, *cmllog; + + //comand + const char *command, *payload, *name; + + //options + const char *config, *format, *period, *filename, *uri, *start, *end, *regex, *metric, *resource, *database, *group, *host, *xquery, *js, *output, *gnuplot; + + //flags + bool quiet, verbose, raw; + }; // interface for commands -class Command { -private: - Config config; - CommandLineArgs current; - string readBuffer; - - struct curl_slist *headers = NULL; - -public: - string url; - - Command() { - // set defaults - loadConfig(config, current.config); - if(config.user.empty()) { - config.user = current.cuser; - } - if(config.pass.empty()) { - config.pass = current.cpass; - } - if(config.host.empty()) { - config.host = current.chost; - } - if(config.protocol.empty()) { - config.protocol = current.cprotocol; - } - if(config.path.empty()) { - config.path = current.cpath; - } - if(config.port.empty()) { - config.port = current.cport; - } - if(config.mlconfig.empty()){ - config.mlconfig = current.cmlconfig; - } - if(config.mllog.empty()){ - config.mllog = current.cmllog; - } - setheaders(); - }; + class Command { + private: + Config config; + CommandLineArgs current; + string readBuffer; + + struct curl_slist *headers = NULL; + + public: + string url; + + Command() { + // set defaults + loadConfig(config, current.config); + if (config.user.empty()) { + config.user = current.cuser; + } + if (config.pass.empty()) { + config.pass = current.cpass; + } + if (config.host.empty()) { + config.host = current.chost; + } + if (config.protocol.empty()) { + config.protocol = current.cprotocol; + } + if (config.path.empty()) { + config.path = current.cpath; + } + if (config.port.empty()) { + config.port = current.cport; + } + if (config.mlconfig.empty()) { + config.mlconfig = current.cmlconfig; + } + if (config.mllog.empty()) { + config.mllog = current.cmllog; + } + setheaders(); + }; - virtual ~Command() { - //std::cout << "Default destructor called\n"; - }; + virtual ~Command() { + //std::cout << "Default destructor called\n"; + }; - virtual int usage(const char *progname) =0; + virtual int usage(const char *progname) =0; - virtual CommandLineArgs options(int n_opts, char *opts[]) = 0; + virtual CommandLineArgs options(int n_opts, char *opts[]) = 0; - virtual string getReadBuffer() { - return this->readBuffer; - } + virtual string getReadBuffer() { + return this->readBuffer; + } - virtual CommandLineArgs getCurrentArgs() { - return this->current; - } + virtual CommandLineArgs getCurrentArgs() { + return this->current; + } - virtual void setCurrentArgs(CommandLineArgs args) { - this->current = args; - loadConfig(config, this->current.config); - } + virtual void setCurrentArgs(CommandLineArgs args) { + this->current = args; + loadConfig(config, this->current.config); + } - virtual Config getConfig() { - return this->config; - } + virtual Config getConfig() { + return this->config; + } - virtual void setConfig(Config config) { - this->config = config; - } + virtual void setConfig(Config config) { + this->config = config; + } - virtual bool checkConfig() { - if (config.host.empty()) { - cerr << "undefined MarkLogic host." << endl; + virtual bool checkConfig() { + if (config.host.empty()) { + cerr << "undefined MarkLogic host." << endl; + } + if (config.pass.empty()) { + cerr << "undefined MarkLogic password." << endl; + } + if (config.protocol.empty()) { + cerr << "undefined MarkLogic protocol." << endl; + } + if (config.user.empty()) { + cerr << "undefined MarkLogic user." << endl; + } } - if (config.pass.empty()) { - cerr << "undefined MarkLogic password." << endl; + + virtual void setResourceUrl(string port, + string root, + string resource) { + checkConfig(); + url = config.protocol + "://" + config.host + ":" + port + root + "/" + resource + ""; } - if (config.protocol.empty()) { - cerr << "undefined MarkLogic protocol." << endl; + + virtual void setLoadUrl(string port, + string root, + string uri) { + checkConfig(); + + url = config.protocol + "://" + config.host + ":" + port + root + "?"; + if (!uri.empty()) { + url += "uri=" + uri; + } + string database = current.database; + if (!database.empty()) { + url += "&database=" + database; + } } - if (config.user.empty()) { - cerr << "undefined MarkLogic user." << endl; + + virtual void setLogUrl(string port, + string root, + string filename, + string start, + string end, + string regex, + string host, + string format) { + checkConfig(); + + url = config.protocol + "://" + config.host + ":" + port + root + "?"; + if (!filename.empty()) { + url += "filename=" + filename; + } + if (!start.empty()) { + url += "&start=" + start; + } + if (!end.empty()) { + url += "&end=" + end; + } + if (!regex.empty()) { + url += "®ex=" + regex; + } + if (!host.empty()) { + url += "&host=" + host; + } + //string format = current.format; + if (format != "") { + url += "&format=" + format; + } } - } - virtual void setResourceUrl(string port, + virtual void setUrl(string port, string root, - string resource) - { - checkConfig(); - url = config.protocol + "://" + config.host + ":" + port + root+ "/" + resource +"" ; - } - - virtual void setLoadUrl(string port, - string root, - string uri) - { - checkConfig(); - - url = config.protocol + "://" + config.host + ":" + port + root+"?" ; - if (!uri.empty()) { - url += "uri=" + uri; - } - string database = current.database; - if (!database.empty()) { - url += "&database=" + database; - } - } - - virtual void setLogUrl(string port, - string root, - string filename, - string start, - string end, - string regex, - string host, - string format) - { - checkConfig(); - - url = config.protocol + "://" + config.host + ":" + port + root+"?" ; - if (!filename.empty() ) { - url += "filename=" + filename; - } - if (!start.empty() ) { - url += "&start=" + start; - } - if (!end.empty() ) { - url += "&end=" + end; - } - if (!regex.empty() ) { - url += "®ex=" + regex; - } - if (!host.empty() ) { - url += "&host=" + host; - } - //string format = current.format; - if (format != "") { - url += "&format=" + format; - } - } - - virtual void setUrl(string port, - string root, - string path, - string view) { - checkConfig(); - if (view == "metrics") { - if (path.find("/") != string::npos) { - string res1 = path.substr(0, path.find("/")); - url = config.protocol + "://" + config.host + ":" + port + root + "/" + res1 + "?"; + string path, + string view) { + checkConfig(); + if (view == "metrics") { + if (path.find("/") != string::npos) { + string res1 = path.substr(0, path.find("/")); + url = config.protocol + "://" + config.host + ":" + port + root + "/" + res1 + "?"; + } else { + url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; + } + } else if (view == "status") { + url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; } else { url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; } - } else if (view == "status") { - url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; - } else { - url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; - } - if (!view.empty()) { + if (!view.empty()) { - url += "view=" + view; - } + url += "view=" + view; + } - string group = current.group; - if (!group.empty() && view != "status") { - url += "&group-id=" + group; - } - string format = current.format; - if (format != "") { - url += "&format=" + format; - } - string start = current.start; - if (start != "") { - url += "&start=" + start; - } - string end = current.end; - if (end != "") { - url += "&end=" + end; - } - string period = current.period; - if (period != "") { - url += "&period=" + period; - } + string group = current.group; + if (!group.empty() && view != "status") { + url += "&group-id=" + group; + } + string format = current.format; + if (format != "") { + url += "&format=" + format; + } + string start = current.start; + if (start != "") { + url += "&start=" + start; + } + string end = current.end; + if (end != "") { + url += "&end=" + end; + } + string period = current.period; + if (period != "") { + url += "&period=" + period; + } - string metric = current.metric; - if (!metric.empty() && view == "metrics") { - string resource = current.resource; - string delimiter = "/"; - string res1 = resource.substr(0, resource.find("/")); - string res2 = path.substr(path.find("/") + 1, path.size()); - if (res1 == "databases") { - url += "&database-metrics=" + metric; - if (resource.find("/") != string::npos) { - url += "&database=" + res2; - } - } else if (res1 == "servers") { - url += "&server-metrics=" + metric; - if (resource.find("/") != string::npos) { - url += "&group-id=Default&server=" + res2; - } - } else if (res1 == "hosts") { - url += "&host-metrics=" + metric; - if (resource.find("/") != string::npos) { - url += "&host=" + res2; - } - } else if (res1 == "forests") { - url += "&forest-metrics=" + metric; - if (resource.find("/") != string::npos) { - url += "&forest=" + res2; + string metric = current.metric; + if (!metric.empty() && view == "metrics") { + string resource = current.resource; + string delimiter = "/"; + string res1 = resource.substr(0, resource.find("/")); + string res2 = path.substr(path.find("/") + 1, path.size()); + if (res1 == "databases") { + url += "&database-metrics=" + metric; + if (resource.find("/") != string::npos) { + url += "&database=" + res2; + } + } else if (res1 == "servers") { + url += "&server-metrics=" + metric; + if (resource.find("/") != string::npos) { + url += "&group-id=Default&server=" + res2; + } + } else if (res1 == "hosts") { + url += "&host-metrics=" + metric; + if (resource.find("/") != string::npos) { + url += "&host=" + res2; + } + } else if (res1 == "forests") { + url += "&forest-metrics=" + metric; + if (resource.find("/") != string::npos) { + url += "&forest=" + res2; + } } } } - } - - virtual void setQueryUrl(string port, - string root, - string path, - string view) { - checkConfig(); - url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; - - string database = current.database; - if (!database.empty()) { - url += "&database=" + database; - } - }; - void setheaders(){ - headers = curl_slist_append(headers, "Accept: text/plain"); - headers = curl_slist_append(headers, "Accept: text/html"); - headers = curl_slist_append(headers, "Accept: application/xml"); - headers = curl_slist_append(headers, "Accept: application/js"); - headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded"); - }; + virtual void setQueryUrl(string port, + string root, + string path, + string view) { + checkConfig(); + url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; - static - int log_trace(CURL *handle, curl_infotype type, - char *data, size_t size, - void *userp) { - const char *text; - (void) handle; /* prevent compiler warning */ - (void) userp; - - switch (type) { - case CURLINFO_TEXT: - LOG_S(INFO) << data; - default: /* in case a new one is introduced to shock us */ - return 0; - - case CURLINFO_HEADER_OUT: - LOG_S(INFO) << "=> Send "; - LOG_S(INFO) << data; - break; - case CURLINFO_DATA_OUT: - LOG_S(INFO) << "=> Send data"; - LOG_S(INFO) << data; - break; - case CURLINFO_SSL_DATA_OUT: - LOG_S(INFO) << "=> Send SSL data"; - LOG_S(INFO) << data; - break; - case CURLINFO_HEADER_IN: - LOG_S(INFO) << "<= Recv header"; - LOG_S(INFO) << data; - break; - case CURLINFO_DATA_IN: - LOG_S(INFO) << "<= Recv data"; - LOG_S(INFO) << data; - break; - case CURLINFO_SSL_DATA_IN: - LOG_S(INFO) << "<= Recv SSL data"; - LOG_S(INFO) << data; - break; + string database = current.database; + if (!database.empty()) { + url += "&database=" + database; + } + }; + + void setheaders() { + headers = curl_slist_append(headers, "Accept: text/plain"); + headers = curl_slist_append(headers, "Accept: text/html"); + headers = curl_slist_append(headers, "Accept: application/xml"); + headers = curl_slist_append(headers, "Accept: application/js"); + headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded"); + }; + + static + int log_trace(CURL *handle, curl_infotype type, + char *data, size_t size, + void *userp) { + const char *text; + (void) handle; /* prevent compiler warning */ + (void) userp; + + switch (type) { + case CURLINFO_TEXT: + LOG_S(INFO) << data; + default: /* in case a new one is introduced to shock us */ + return 0; + + case CURLINFO_HEADER_OUT: + LOG_S(INFO) << "=> Send "; + LOG_S(INFO) << data; + break; + case CURLINFO_DATA_OUT: + LOG_S(INFO) << "=> Send data"; + LOG_S(INFO) << data; + break; + case CURLINFO_SSL_DATA_OUT: + LOG_S(INFO) << "=> Send SSL data"; + LOG_S(INFO) << data; + break; + case CURLINFO_HEADER_IN: + LOG_S(INFO) << "<= Recv header"; + LOG_S(INFO) << data; + break; + case CURLINFO_DATA_IN: + LOG_S(INFO) << "<= Recv data"; + LOG_S(INFO) << data; + break; + case CURLINFO_SSL_DATA_IN: + LOG_S(INFO) << "<= Recv SSL data"; + LOG_S(INFO) << data; + break; + } } - } - virtual int execute() { + virtual int execute() { - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_HEADER, 0); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl/7.19.6"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl1, CURLOPT_HTTPGET, 1L); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - - curl_multi_add_handle(curlm, curl1); - - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_HEADER, 0); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl/7.19.6"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl1, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + + curl_multi_add_handle(curlm, curl1); + + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } } } - } - curl_global_cleanup(); + curl_global_cleanup(); - return EXIT_SUCCESS; + return EXIT_SUCCESS; - }; + }; - static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { - ((std::string *) userp)->append((char *) contents, size * nmemb); - return size * nmemb; - } + static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { + ((std::string *) userp)->append((char *) contents, size * nmemb); + return size * nmemb; + } - virtual int executeQueryPost(string type, string query) { + virtual int executeQueryPost(string type, string query) { - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); - string xquery = type + "=" + data; - curl_easy_setopt(curl1, CURLOPT_POST, 1); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, xquery.c_str()); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); + string xquery = type + "=" + data; + curl_easy_setopt(curl1, CURLOPT_POST, 1); + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, xquery.c_str()); + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); + curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); + if (handle_count == 0) { + curl_global_cleanup(); - break; + break; + } } + //std::cout << readBuffer << std::endl; } - //std::cout << readBuffer << std::endl; - } - curl_global_cleanup(); + curl_global_cleanup(); - return EXIT_SUCCESS; + return EXIT_SUCCESS; - }; + }; - virtual int executeLoadPost(const char* file, string body) { + virtual int executeLoadPost(const char *file, string body) { - FILE *fd; + FILE *fd; - fd = fopen(file, "rb"); /* open file to upload */ + fd = fopen(file, "rb"); /* open file to upload */ - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); - curl_easy_setopt(curl1, CURLOPT_READDATA, fd); + curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(curl1, CURLOPT_READDATA, fd); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } } } - } - curl_global_cleanup(); - return EXIT_SUCCESS; - }; + curl_global_cleanup(); + return EXIT_SUCCESS; + }; - virtual int executeResourcePost(string body, string format) { + virtual int executeResourcePost(string body, string format) { - long http_code = 0; + long http_code = 0; - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - if(format == "xml"){ - curl_slist_append(headers, "Content-Type: application/xml"); - }else{ - curl_slist_append(headers, "Content-Type: application/json"); - } + if (format == "xml") { + curl_slist_append(headers, "Content-Type: application/xml"); + } else { + curl_slist_append(headers, "Content-Type: application/json"); + } - if (curl1) { + if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDSIZE, body.length()); - curl_easy_setopt(curl1, CURLOPT_POST, 1); + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); + curl_easy_setopt(curl1, CURLOPT_POSTFIELDSIZE, body.length()); + curl_easy_setopt(curl1, CURLOPT_POST, 1); - curl_easy_getinfo (curl1, CURLINFO_RESPONSE_CODE, &http_code); + curl_easy_getinfo (curl1, CURLINFO_RESPONSE_CODE, &http_code); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } } } - } - curl_global_cleanup(); - return EXIT_SUCCESS; - }; - - virtual int executeResourcePut(string body, string format) { + curl_global_cleanup(); + return EXIT_SUCCESS; + }; - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); + virtual int executeResourcePut(string body, string format) { - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - if(format == "xml"){ - headers = curl_slist_append(headers, "Content-Type: application/xml"); - }else{ - headers = curl_slist_append(headers, "Content-Type: application/json"); - } - - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + if (format == "xml") { + headers = curl_slist_append(headers, "Content-Type: application/xml"); } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + headers = curl_slist_append(headers, "Content-Type: application/json"); } - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); - curl_easy_setopt(curl1, CURLOPT_PUT, 1L); - curl_easy_setopt(curl1, CURLOPT_READDATA, body.c_str()); + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - if (handle_count == 0) { - curl_global_cleanup(); + curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(curl1, CURLOPT_PUT, 1L); + curl_easy_setopt(curl1, CURLOPT_READDATA, body.c_str()); - break; - } - } - //std::cout << readBuffer << std::endl; - } + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_global_cleanup(); + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); - return EXIT_SUCCESS; + if (handle_count == 0) { + curl_global_cleanup(); - }; + break; + } + } + //std::cout << readBuffer << std::endl; + } - virtual int executeInstallPost(string filename, string format) { - std::ifstream ifs(filename); - string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + curl_global_cleanup(); - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); + return EXIT_SUCCESS; - CURL *curl1 = NULL; - curl1 = curl_easy_init(); + }; - struct curl_httppost *post = NULL; - struct curl_httppost *last = NULL; + virtual int executeInstallPost(string filename, string format) { + std::ifstream ifs(filename); + string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - if(format == "xml"){ - headers = curl_slist_append(headers, "Content-Type: application/xml"); - }else{ - headers = curl_slist_append(headers, "Content-Type: application/json"); - } + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + CURL *curl1 = NULL; + curl1 = curl_easy_init(); - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + struct curl_httppost *post = NULL; + struct curl_httppost *last = NULL; if (current.verbose) { curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); } else { curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); - curl_easy_setopt(curl1, CURLOPT_POST, 1L); - - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; - } + if (format == "xml") { + headers = curl_slist_append(headers, "Content-Type: application/xml"); + } else { + headers = curl_slist_append(headers, "Content-Type: application/json"); } - //std::cout << readBuffer << std::endl; - } - curl_global_cleanup(); - return EXIT_SUCCESS; + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - }; + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - int displayargs() { - LOG_S(INFO) << "----------------"; - LOG_S(INFO) << "options" << endl; - LOG_S(INFO) << "----------------"; - - LOG_S(INFO) << "format: " << current.format; - LOG_S(INFO) << "database: " << current.database; - LOG_S(INFO) << "xquery: " << current.xquery; - LOG_S(INFO) << "format: " << current.format; - LOG_S(INFO) << "period: " << current.period; - LOG_S(INFO) << "start: " << current.start; - LOG_S(INFO) << "end: " << current.end; - LOG_S(INFO) << "metric: " << current.metric; - LOG_S(INFO) << "resource: " << current.resource; - LOG_S(INFO) << "name: " << current.name; - LOG_S(INFO) << "ml-config: " << current.config; - LOG_S(INFO) << "output: " << current.output; - LOG_S(INFO) << "gnuplot: " << current.gnuplot; - return EXIT_SUCCESS; - }; + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - int displayconfig() { - LOG_S(INFO) << "----------------"; - LOG_S(INFO) << "config"; - LOG_S(INFO) << "----------------"; - LOG_S(INFO) << "user: " << config.user; - LOG_S(INFO) << "pass: " << config.pass; - LOG_S(INFO) << "host: " << config.host; - return EXIT_SUCCESS; - }; + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } - const char* getprogname(const char *progname) { - const char *name = progname; - while (*progname != 0) { - if (*progname == '/' || *progname == '\\') { - ++progname; - name = progname; - } else { - ++progname; + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); + curl_easy_setopt(curl1, CURLOPT_POST, 1L); + + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } + } + //std::cout << readBuffer << std::endl; } - } - return name; - }; - void doPlot(string output, string plot, string units, string name, string resource, const Value &entries) { - - //Gnuplot::set_terminal_std("dumb"); - Gnuplot g1("linespoints"); - g1.remove_tmpfiles(); - g1.set_ylabel(units); - g1.set_xlabel("time"); - g1.cmd("set xdata time"); - g1.cmd("set timefmt '%Y-%m-%dT%H:%M:%S'"); - g1.cmd("set timefmt '%Y-%m-%dT%H:%M:%S'"); - - if (plot.empty()) { - if (output.find(string("jpg")) != std::string::npos) { - g1.cmd("set terminal 'jpeg' size 900,500 nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); - } else if (output.find(string("jpeg")) != std::string::npos) { - g1.cmd("set terminal 'jpeg' size 900,500 nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); - } else if (output.find(string("png")) != std::string::npos) { - g1.cmd("set terminal 'pngcairo' nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); + curl_global_cleanup(); + return EXIT_SUCCESS; + + }; + + int displayargs() { + LOG_S(INFO) << "----------------"; + LOG_S(INFO) << "options" << endl; + LOG_S(INFO) << "----------------"; + + LOG_S(INFO) << "format: " << current.format; + LOG_S(INFO) << "database: " << current.database; + LOG_S(INFO) << "xquery: " << current.xquery; + LOG_S(INFO) << "format: " << current.format; + LOG_S(INFO) << "period: " << current.period; + LOG_S(INFO) << "start: " << current.start; + LOG_S(INFO) << "end: " << current.end; + LOG_S(INFO) << "metric: " << current.metric; + LOG_S(INFO) << "resource: " << current.resource; + LOG_S(INFO) << "name: " << current.name; + LOG_S(INFO) << "ml-config: " << current.config; + LOG_S(INFO) << "output: " << current.output; + LOG_S(INFO) << "gnuplot: " << current.gnuplot; + return EXIT_SUCCESS; + }; + + int displayconfig() { + LOG_S(INFO) << "----------------"; + LOG_S(INFO) << "config"; + LOG_S(INFO) << "----------------"; + LOG_S(INFO) << "user: " << config.user; + LOG_S(INFO) << "pass: " << config.pass; + LOG_S(INFO) << "host: " << config.host; + return EXIT_SUCCESS; + }; + + const char *getprogname(const char *progname) { + const char *name = progname; + while (*progname != 0) { + if (*progname == '/' || *progname == '\\') { + ++progname; + name = progname; + } else { + ++progname; + } + } + return name; + }; + + void doPlot(string output, string plot, string units, string name, string resource, const Value &entries) { + + //Gnuplot::set_terminal_std("dumb"); + Gnuplot g1("linespoints"); + g1.remove_tmpfiles(); + g1.set_ylabel(units); + g1.set_xlabel("time"); + g1.cmd("set xdata time"); + g1.cmd("set timefmt '%Y-%m-%dT%H:%M:%S'"); + g1.cmd("set timefmt '%Y-%m-%dT%H:%M:%S'"); + + if (plot.empty()) { + if (output.find(string("jpg")) != std::string::npos) { + g1.cmd("set terminal 'jpeg' size 900,500 nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); + } else if (output.find(string("jpeg")) != std::string::npos) { + g1.cmd("set terminal 'jpeg' size 900,500 nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); + } else if (output.find(string("png")) != std::string::npos) { + g1.cmd("set terminal 'pngcairo' nocrop enhanced font 'Verdana,10' size 900,500 background '#FFFCF5'"); + } else { + g1.cmd("set terminal 'dumb'"); + } + // default gnuplot settings + g1.cmd( + "unset key\n" + "set xlabel \"Time\" textcolor'#000000'\n" + "set ylabel \"" + units + "\" textcolor '#000000'\n" + "set label textcolor '#000000'\n" + "set border 10 lw 1 lc '#038DDD'\n" + "set grid lc '#038DDD'\n" + "set border 0 back ls 11\n" + "set grid\n" + "set autoscale y\n" + "set autoscale x\n" + "set xtics rotate by -45 offset -0.1,-0.1\n" + "set mxtics\n" + "set mytics\n"); + g1.set_style("linespoints linetype 1 lw 1 lc '#025072' pt 7 ps .5"); } else { - g1.cmd("set terminal 'dumb'"); - } - // default gnuplot settings - g1.cmd( - "unset key\n" - "set xlabel \"Time\" textcolor'#000000'\n" - "set ylabel \""+units+"\" textcolor '#000000'\n" - "set label textcolor '#000000'\n" - "set border 10 lw 1 lc '#038DDD'\n" - "set grid lc '#038DDD'\n" - "set border 0 back ls 11\n" - "set grid\n" - "set autoscale y\n" - "set autoscale x\n" - "set xtics rotate by -45 offset -0.1,-0.1\n" - "set mxtics\n" - "set mytics\n"); - g1.set_style("linespoints linetype 1 lw 1 lc '#025072' pt 7 ps .5"); - } else { - // load custom gnuplot - g1.set_GNUPlotPath(plot); - } - - vector x; - vector y; - string firstDT, lastDT; - for (SizeType i = 0; i < entries.Size(); i++) { - const Value &entry = entries[i]; - if (i == 0) { - firstDT = entry["dt"].GetString(); + // load custom gnuplot + g1.set_GNUPlotPath(plot); } - if (i == entries.Size() - 1) { - lastDT = entry["dt"].GetString(); + + vector x; + vector y; + string firstDT, lastDT; + for (SizeType i = 0; i < entries.Size(); i++) { + const Value &entry = entries[i]; + if (i == 0) { + firstDT = entry["dt"].GetString(); + } + if (i == entries.Size() - 1) { + lastDT = entry["dt"].GetString(); + } + x.push_back(entry["dt"].GetString()); + y.push_back(entry["value"].GetDouble()); } - x.push_back(entry["dt"].GetString()); - y.push_back(entry["value"].GetDouble()); - } - g1.cmd("set xrange ['" + firstDT+ "':'" + lastDT + "']"); + g1.cmd("set xrange ['" + firstDT + "':'" + lastDT + "']"); // auto maxY = std::max_element(std::begin(y), std::end(y)); // auto minY = std::max_element(std::begin(y), std::end(y)); @@ -845,14 +844,14 @@ class Command { // minV << *minY; // maxV << *maxY; // g1.cmd("set yrange ['" + minV.str() + "':'" + maxV.str() + "']"); - g1.cmd("set title '{/Verdana " + resource + ":" + name + "}' "); + g1.cmd("set title '{/Verdana " + resource + ":" + name + "}' "); - if(!output.empty()){ - g1.cmd("set output '" + output + "'"); - } - - g1.plot_xy(x, y); - } -}; + if (!output.empty()) { + g1.cmd("set output '" + output + "'"); + } + g1.plot_xy(x, y); + } + }; +} diff --git a/src/config.hpp b/src/config.hpp index 2aa828f..a442c7a 100755 --- a/src/config.hpp +++ b/src/config.hpp @@ -27,62 +27,66 @@ #define GetCurrentDir getcwd #endif -char pPath[FILENAME_MAX]; +namespace mlutil { -using namespace std; + char pPath[FILENAME_MAX]; -bool is_file_exist(string fileName) { - std::ifstream infile(fileName); - return infile.good(); -} + using namespace std; -struct Config { - string user; - string pass; - string host; - string protocol; - string port; - string path; - string mlconfig; - string mllog; -}; + bool is_file_exist(string fileName) { + std::ifstream infile(fileName); + return infile.good(); + } -void loadConfig(Config &config, string path) { - std::string homepath = getenv("HOME"); - GetCurrentDir(pPath, sizeof(pPath)); - std::string currentpath = pPath; - std::string mlutils; + struct Config { + string user; + string pass; + string host; + string protocol; + string port; + string path; + string mlconfig; + string mllog; + }; - if (is_file_exist(path)) { - mlutils = (path).c_str(); - } else if (is_file_exist(currentpath + "/.ml-utils")) { - mlutils = (currentpath + "/.ml-utils").c_str(); - } else if (is_file_exist(homepath + "/.ml-utils")) { - mlutils = (homepath + "/.ml-utils").c_str(); - } else { - mlutils = (path).c_str(); - } + void loadConfig(Config &config, string path) { + std::string homepath = getenv("HOME"); + GetCurrentDir(pPath, sizeof(pPath)); + std::string currentpath = pPath; + std::string mlutils; - ifstream fin(mlutils); - std: - string line; + if (is_file_exist(path)) { + mlutils = (path).c_str(); + } else if (is_file_exist(currentpath + "/.ml-utils")) { + mlutils = (currentpath + "/.ml-utils").c_str(); + } else if (is_file_exist(homepath + "/.ml-utils")) { + mlutils = (homepath + "/.ml-utils").c_str(); + } else { + mlutils = (path).c_str(); + } - while (getline(fin, line)) { - istringstream sin(line.substr(line.find("=") + 1)); - if (line.find("user") != -1) - sin >> config.user; - else if (line.find("pass") != -1) - sin >> config.pass; - else if (line.find("host") != -1) - sin >> config.host; - else if (line.find("protocol") != -1) - sin >> config.protocol; - else if (line.find("port") != -1) - sin >> config.port; - else if (line.find("ml-config") != -1) - sin >> config.mlconfig; - else if (line.find("ml-log") != -1) - sin >> config.mllog; - else if (line.find("path") != -1) - sin >> config.path; } -} + ifstream fin(mlutils); + std: + string line; + + while (getline(fin, line)) { + istringstream sin(line.substr(line.find("=") + 1)); + if (line.find("user") != -1) + sin >> config.user; + else if (line.find("pass") != -1) + sin >> config.pass; + else if (line.find("host") != -1) + sin >> config.host; + else if (line.find("protocol") != -1) + sin >> config.protocol; + else if (line.find("port") != -1) + sin >> config.port; + else if (line.find("ml-config") != -1) + sin >> config.mlconfig; + else if (line.find("ml-log") != -1) + sin >> config.mllog; + else if (line.find("path") != -1) + sin >> config.path; + } + } +} \ No newline at end of file diff --git a/src/history.cpp b/src/history.cpp index 14433f3..3a9f78f 100755 --- a/src/history.cpp +++ b/src/history.cpp @@ -13,79 +13,82 @@ using namespace std; -History::History() { - //std::cout << "history constructor called\n"; -}; +namespace mlutil { -History::~History() { - //std::cout << "history destructor called\n"; -}; + History::History() { + //std::cout << "history constructor called\n"; + }; -CommandLineArgs History::options(int n_opts, char *opts[]) { - CommandLineArgs args; + History::~History() { + //std::cout << "history destructor called\n"; + }; - args.period = "hour"; - args.start = ""; - args.end = ""; + CommandLineArgs History::options(int n_opts, char *opts[]) { + CommandLineArgs args; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'f') { - ++i; - args.format = opts[i]; - } else if (opts[i][1] == 'p') { - ++i; - args.period = opts[i]; - } else if (opts[i][1] == 's') { - ++i; - args.start = opts[i]; - } else if (opts[i][1] == 'e') { - ++i; - args.end = opts[i]; - } else if (opts[i][1] == 'm') { - ++i; - args.metric = opts[i]; - } else if (opts[i][1] == 'r') { - ++i; - args.resource = opts[i]; - } else if (opts[i][1] == 'o') { - ++i; - args.output = opts[i]; - } else if (opts[i][1] == 'g') { - ++i; - args.gnuplot = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else { - //usage(opts[0]); + args.period = "hour"; + args.start = ""; + args.end = ""; + + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'f') { + ++i; + args.format = opts[i]; + } else if (opts[i][1] == 'p') { + ++i; + args.period = opts[i]; + } else if (opts[i][1] == 's') { + ++i; + args.start = opts[i]; + } else if (opts[i][1] == 'e') { + ++i; + args.end = opts[i]; + } else if (opts[i][1] == 'm') { + ++i; + args.metric = opts[i]; + } else if (opts[i][1] == 'r') { + ++i; + args.resource = opts[i]; + } else if (opts[i][1] == 'o') { + ++i; + args.output = opts[i]; + } else if (opts[i][1] == 'g') { + ++i; + args.gnuplot = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int History::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-hist 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "retrieve MarkLogic history metrics\n" - << ">" << name << " resources/{resource-name} [options]\n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -f : format (xml|json)\n" - << " -p : period (raw|hour|day)\n" - << " -s : start date (ex. 2015-03-21T17:38:00)\n" - << " -e : end date (ex. 2015-03-22T17:58:00)\n" - << " -m : meter (resource specific)\n" - << " -r : resource (databases|forests|servers|hosts) / resource-name\n" - << " -o : graph output\n" - << " -g : custom gnuplot script\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; + int History::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-hist 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "retrieve MarkLogic history metrics\n" + << ">" << name << " resources/{resource-name} [options]\n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -f : format (xml|json)\n" + << " -p : period (raw|hour|day)\n" + << " -s : start date (ex. 2015-03-21T17:38:00)\n" + << " -e : end date (ex. 2015-03-22T17:58:00)\n" + << " -m : meter (resource specific)\n" + << " -r : resource (databases|forests|servers|hosts) / resource-name\n" + << " -o : graph output\n" + << " -g : custom gnuplot script\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } } \ No newline at end of file diff --git a/src/history.hpp b/src/history.hpp index b83595f..e41b2f7 100755 --- a/src/history.hpp +++ b/src/history.hpp @@ -19,10 +19,16 @@ #include "command.hpp" -class History : public Command { -public: - History(); - virtual ~History(); - virtual int usage(const char *progname); - virtual CommandLineArgs options(int n_opts, char *opts[]); -}; +namespace mlutil { + + class History : public Command { + public: + History(); + + virtual ~History(); + + virtual int usage(const char *progname); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + }; +} \ No newline at end of file diff --git a/src/load.cpp b/src/load.cpp index 84d678c..8f931e1 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -10,53 +10,56 @@ #include "load.hpp" -Load::Load() {//std::cout << "load constructor called\n"; -}; +namespace mlutil { -Load::~Load() {//std::cout << "load destructor called\n"; -}; + Load::Load() {//std::cout << "load constructor called\n"; + }; -CommandLineArgs Load::options(int n_opts, char *opts[]) { - CommandLineArgs args; + Load::~Load() {//std::cout << "load destructor called\n"; + }; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'd') { - ++i; - args.database = opts[i]; - } else if (opts[i][1] == 'u') { - ++i; - args.uri = opts[i]; - } else if (opts[i][1] == 'f') { - ++i; - args.filename = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else { - //usage(opts[0]); + CommandLineArgs Load::options(int n_opts, char *opts[]) { + CommandLineArgs args; + + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'd') { + ++i; + args.database = opts[i]; + } else if (opts[i][1] == 'u') { + ++i; + args.uri = opts[i]; + } else if (opts[i][1] == 'f') { + ++i; + args.filename = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Load::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-load 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "load data\n" - << ">" << name << " [options]\n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -d : database\n" - << " -u : uri\n" - << " -f : filename\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; -} + int Load::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-load 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "load data\n" + << ">" << name << " [options]\n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -d : database\n" + << " -u : uri\n" + << " -f : filename\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } +} \ No newline at end of file diff --git a/src/load.hpp b/src/load.hpp index 8ba9770..5eea469 100755 --- a/src/load.hpp +++ b/src/load.hpp @@ -21,10 +21,16 @@ using namespace std; -class Load : public Command { -public: - Load(); - virtual ~Load(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); -}; +namespace mlutil { + + class Load : public Command { + public: + Load(); + + virtual ~Load(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + }; +} diff --git a/src/log.cpp b/src/log.cpp index 39e6112..69bbe9c 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -10,71 +10,74 @@ #include "log.hpp" -Log::Log() { -}; +namespace mlutil { -Log::~Log() { -}; + Log::Log() { + }; -CommandLineArgs Log::options(int n_opts, char *opts[]) { - CommandLineArgs args; + Log::~Log() { + }; - args.start = ""; - args.end = ""; + CommandLineArgs Log::options(int n_opts, char *opts[]) { + CommandLineArgs args; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'f') { - ++i; - args.format = opts[i]; - } else if (opts[i][1] == 'p') { - ++i; - args.period = opts[i]; - } else if (opts[i][1] == 's') { - ++i; - args.start = opts[i]; - } else if (opts[i][1] == 'e') { - ++i; - args.end = opts[i]; - } else if (opts[i][1] == 'n') { - ++i; - args.name = opts[i]; - } else if (opts[i][1] == 't') { - ++i; - args.host = opts[i]; - } else if (opts[i][1] == 'r') { - ++i; - args.regex = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else { - //usage(opts[0]); + args.start = ""; + args.end = ""; + + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'f') { + ++i; + args.format = opts[i]; + } else if (opts[i][1] == 'p') { + ++i; + args.period = opts[i]; + } else if (opts[i][1] == 's') { + ++i; + args.start = opts[i]; + } else if (opts[i][1] == 'e') { + ++i; + args.end = opts[i]; + } else if (opts[i][1] == 'n') { + ++i; + args.name = opts[i]; + } else if (opts[i][1] == 't') { + ++i; + args.host = opts[i]; + } else if (opts[i][1] == 'r') { + ++i; + args.regex = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Log::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-log 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "retrieve logs\n" - << ">" << name << " \n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -f : format(xml | json)\n" - << " -t : host (ex. localhost)\n" - << " -n : filename (ex. ErrorLog.txt)\n" - << " -s : start (ex. 2016-10-27T06:00:00)\n" - << " -e : end (ex. 2016-10-27T06:00:00)\n" - << " -r : regex (ex. Merged)\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; -} + int Log::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-log 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "retrieve logs\n" + << ">" << name << " \n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -f : format(xml | json)\n" + << " -t : host (ex. localhost)\n" + << " -n : filename (ex. ErrorLog.txt)\n" + << " -s : start (ex. 2016-10-27T06:00:00)\n" + << " -e : end (ex. 2016-10-27T06:00:00)\n" + << " -r : regex (ex. Merged)\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } +} \ No newline at end of file diff --git a/src/log.hpp b/src/log.hpp index 2b1b8bd..bf56a18 100755 --- a/src/log.hpp +++ b/src/log.hpp @@ -21,10 +21,16 @@ using namespace std; -class Log : public Command { -public: - Log(); - virtual ~Log(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); -}; +namespace mlutil { + + class Log : public Command { + public: + Log(); + + virtual ~Log(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + }; +} \ No newline at end of file diff --git a/src/mlbrowse/main.cpp b/src/mlbrowse/main.cpp index 4feec8b..1db23c2 100755 --- a/src/mlbrowse/main.cpp +++ b/src/mlbrowse/main.cpp @@ -9,6 +9,9 @@ #include "../browse.cpp" +using namespace std; +using namespace mlutil; + int main(int argc, char *argv[]) { try { Browse browse; diff --git a/src/mlconfigure/main.cpp b/src/mlconfigure/main.cpp index deaa472..708a4fd 100755 --- a/src/mlconfigure/main.cpp +++ b/src/mlconfigure/main.cpp @@ -14,6 +14,7 @@ #include "../admin.cpp" using namespace std; +using namespace mlutil; int main(int argc, char *argv[]) { diff --git a/src/mlhistory/main.cpp b/src/mlhistory/main.cpp index b23dc15..dadad47 100755 --- a/src/mlhistory/main.cpp +++ b/src/mlhistory/main.cpp @@ -18,6 +18,8 @@ using namespace rapidjson; using namespace std; +using namespace mlutil; + int main(int argc, char *argv[]) { diff --git a/src/mljs/main.cpp b/src/mljs/main.cpp index c46c2e1..dbefb8f 100755 --- a/src/mljs/main.cpp +++ b/src/mljs/main.cpp @@ -9,6 +9,9 @@ #include "../query.cpp" +using namespace std; +using namespace mlutil; + int main(int argc, char *argv[]) { try { diff --git a/src/mlload/main.cpp b/src/mlload/main.cpp index ef7d488..7839818 100755 --- a/src/mlload/main.cpp +++ b/src/mlload/main.cpp @@ -9,6 +9,9 @@ #include "../load.cpp" +using namespace std; +using namespace mlutil; + int main(int argc, char *argv[]) { try { diff --git a/src/mllog/main.cpp b/src/mllog/main.cpp index 5c1eb72..0a79fe9 100755 --- a/src/mllog/main.cpp +++ b/src/mllog/main.cpp @@ -10,6 +10,7 @@ #include "../log.cpp" using namespace std; +using namespace mlutil; int main(int argc, char *argv[]) { diff --git a/src/mlstatus/main.cpp b/src/mlstatus/main.cpp index 037efc1..e4112fe 100755 --- a/src/mlstatus/main.cpp +++ b/src/mlstatus/main.cpp @@ -16,6 +16,8 @@ #include "../status.cpp" using namespace rapidjson; +using namespace std; +using namespace mlutil; int main(int argc, char *argv[]) { diff --git a/src/mlxq/main.cpp b/src/mlxq/main.cpp index 4edb2b6..af19007 100755 --- a/src/mlxq/main.cpp +++ b/src/mlxq/main.cpp @@ -12,6 +12,7 @@ #include "../query.cpp" using namespace std; +using namespace mlutil; int main(int argc, char *argv[]) { diff --git a/src/query.cpp b/src/query.cpp index a5f260f..b3f02f3 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -10,61 +10,64 @@ #include "query.hpp" -Query::Query() { -}; +namespace mlutil { -Query::~Query() { -}; + Query::Query() { + }; -CommandLineArgs Query::options(int n_opts, char *opts[]) { - CommandLineArgs args; + Query::~Query() { + }; - args.xquery = ""; - args.js = ""; + CommandLineArgs Query::options(int n_opts, char *opts[]) { + CommandLineArgs args; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'f') { - ++i; - args.format = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'd') { - ++i; - args.database = opts[i]; - } else if (opts[i][1] == 'x') { - ++i; - args.xquery = opts[i]; - } else if (opts[i][1] == 'j') { - ++i; - args.js = opts[i]; - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else if (opts[i][1] == 'r') { - args.raw = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else { - //usage(opts[0]); + args.xquery = ""; + args.js = ""; + + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'f') { + ++i; + args.format = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'd') { + ++i; + args.database = opts[i]; + } else if (opts[i][1] == 'x') { + ++i; + args.xquery = opts[i]; + } else if (opts[i][1] == 'j') { + ++i; + args.js = opts[i]; + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else if (opts[i][1] == 'r') { + args.raw = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Query::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: " << name << " 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "evaluate query\n" - << ">" << name << " database [options]\n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -f : format (xml|json)\n" - << " -d : database\n" - << " -r : raw output\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; -} + int Query::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: " << name << " 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "evaluate query\n" + << ">" << name << " database [options]\n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -f : format (xml|json)\n" + << " -d : database\n" + << " -r : raw output\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } +} \ No newline at end of file diff --git a/src/query.hpp b/src/query.hpp index d2b2857..f4e6b3a 100755 --- a/src/query.hpp +++ b/src/query.hpp @@ -21,10 +21,16 @@ using namespace std; -class Query : public Command { -public: - Query(); - virtual ~Query(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); -}; +namespace mlutil { + + class Query : public Command { + public: + Query(); + + virtual ~Query(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + }; +} \ No newline at end of file diff --git a/src/status.cpp b/src/status.cpp index cf83b4e..39c2280 100755 --- a/src/status.cpp +++ b/src/status.cpp @@ -10,56 +10,59 @@ #include "status.hpp" -Status::Status() { - //std::cout << "status constructor called\n"; -}; +namespace mlutil { -Status::~Status() { - //std::cout << "status destructor called\n"; -}; + Status::Status() { + //std::cout << "status constructor called\n"; + }; -CommandLineArgs Status::options(int n_opts, char *opts[]) { - CommandLineArgs args; + Status::~Status() { + //std::cout << "status destructor called\n"; + }; - args.group = "Default"; + CommandLineArgs Status::options(int n_opts, char *opts[]) { + CommandLineArgs args; - for (int i = 1; i < n_opts; ++i) { - if (opts[i][1] == 'f') { - ++i; - args.format = opts[i]; - } else if (opts[i][1] == 'q') { - args.quiet = true; - } else if (opts[i][1] == 'v') { - args.verbose = true; - } else if (opts[i][1] == 'r') { - ++i; - args.resource = opts[i]; - } else if (opts[i][1] == 'g') { - ++i; - args.group = opts[i]; - } else if (opts[i][1] == 'h') { - usage(opts[0]); - exit(1); - } else if (opts[i][1] == 'c') { - ++i; - args.config = opts[i]; - } else { - //usage(opts[0]); + args.group = "Default"; + + for (int i = 1; i < n_opts; ++i) { + if (opts[i][1] == 'f') { + ++i; + args.format = opts[i]; + } else if (opts[i][1] == 'q') { + args.quiet = true; + } else if (opts[i][1] == 'v') { + args.verbose = true; + } else if (opts[i][1] == 'r') { + ++i; + args.resource = opts[i]; + } else if (opts[i][1] == 'g') { + ++i; + args.group = opts[i]; + } else if (opts[i][1] == 'h') { + usage(opts[0]); + exit(1); + } else if (opts[i][1] == 'c') { + ++i; + args.config = opts[i]; + } else { + //usage(opts[0]); + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Status::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-status 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "retrieve MarkLogic resource status\n" - << ">" << name << " Resources/resource [options]\n" - << " -c : config file (ex. /home/jfuller/.ml-utils)\n" - << " -f : format (xml|json)\n" - << " -r : resource (databases|forests|servers|hosts) / resource-name\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)\n"; - return EXIT_SUCCESS; + int Status::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-status 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "retrieve MarkLogic resource status\n" + << ">" << name << " Resources/resource [options]\n" + << " -c : config file (ex. /home/jfuller/.ml-utils)\n" + << " -f : format (xml|json)\n" + << " -r : resource (databases|forests|servers|hosts) / resource-name\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)\n"; + return EXIT_SUCCESS; + } } \ No newline at end of file diff --git a/src/status.hpp b/src/status.hpp index 2504374..0701a28 100755 --- a/src/status.hpp +++ b/src/status.hpp @@ -21,12 +21,16 @@ using namespace std; -class Status : public Command { -public: - Status(); - virtual ~Status(); - virtual CommandLineArgs options(int n_opts, char *opts[]); - virtual int usage(const char *progname); -}; +namespace mlutil { + class Status : public Command { + public: + Status(); + virtual ~Status(); + + virtual CommandLineArgs options(int n_opts, char *opts[]); + + virtual int usage(const char *progname); + }; +} diff --git a/test/unit_test_browse.cpp b/test/unit_test_browse.cpp index adcc8ab..33dd41a 100755 --- a/test/unit_test_browse.cpp +++ b/test/unit_test_browse.cpp @@ -2,6 +2,9 @@ #include +using namespace std; +using namespace mlutil; + TEST(BrowseTest,TestAllBrowseMethods){ EXPECT_TRUE(new Browse()); } diff --git a/test/unit_test_configure.cpp b/test/unit_test_configure.cpp index 81368db..d76653f 100755 --- a/test/unit_test_configure.cpp +++ b/test/unit_test_configure.cpp @@ -1,6 +1,8 @@ #include #include "../src/admin.cpp" +using namespace std; +using namespace mlutil; TEST(AdminTest,TestAllAdminMethods){ EXPECT_TRUE(new Admin()); diff --git a/test/unit_test_history.cpp b/test/unit_test_history.cpp index cd15f8d..65a5f71 100755 --- a/test/unit_test_history.cpp +++ b/test/unit_test_history.cpp @@ -1,5 +1,7 @@ #include "../src/history.cpp" #include +using namespace std; +using namespace mlutil; TEST(History,Constructor){ EXPECT_TRUE(new History()); diff --git a/test/unit_test_load.cpp b/test/unit_test_load.cpp index d55cacb..99e5472 100755 --- a/test/unit_test_load.cpp +++ b/test/unit_test_load.cpp @@ -1,5 +1,7 @@ #include "../src/load.cpp" #include +using namespace std; +using namespace mlutil; TEST(LoadTest,TestAllLoadMethods){ EXPECT_TRUE(new Load()); diff --git a/test/unit_test_log.cpp b/test/unit_test_log.cpp index 4f0dae3..3397d00 100755 --- a/test/unit_test_log.cpp +++ b/test/unit_test_log.cpp @@ -1,5 +1,7 @@ #include "../src/log.cpp" #include +using namespace std; +using namespace mlutil; TEST(LogTest,TestAllLogMethods){ EXPECT_TRUE(new Log()); diff --git a/test/unit_test_query.cpp b/test/unit_test_query.cpp index 83835f6..b63b5d8 100755 --- a/test/unit_test_query.cpp +++ b/test/unit_test_query.cpp @@ -1,6 +1,9 @@ #include "../src/query.cpp" #include +using namespace std; +using namespace mlutil; + TEST(QueryTest,TestAllQueryMethods){ EXPECT_TRUE(new Query()); } diff --git a/test/unit_test_status.cpp b/test/unit_test_status.cpp index 3138ae8..9fedc7f 100755 --- a/test/unit_test_status.cpp +++ b/test/unit_test_status.cpp @@ -1,6 +1,9 @@ #include "../src/status.cpp" #include +using namespace std; +using namespace mlutil; + TEST(StatusTest,TestAllStatusMethods){ EXPECT_TRUE(new Status()); } From 339588b1766963ee7968a556a3827af18d3215d8 Mon Sep 17 00:00:00 2001 From: xquery Date: Thu, 25 May 2017 20:00:16 +0200 Subject: [PATCH 06/11] tidy up --- .gitignore | 8 +- .idea/misc.xml | 12 ++ src/admin.cpp | 295 ++++++++++++++++++++++----------------- src/admin.hpp | 17 +++ src/browse.cpp | 35 ++++- src/browse.hpp | 16 +++ src/command.hpp | 137 +++++++++++++++++- src/config.hpp | 16 +++ src/history.cpp | 27 ++++ src/history.hpp | 16 +++ src/load.cpp | 27 ++++ src/load.hpp | 16 +++ src/log.cpp | 27 ++++ src/log.hpp | 16 +++ src/mlbrowse/main.cpp | 20 +++ src/mlconfigure/main.cpp | 66 +++++---- src/mlhistory/main.cpp | 27 +++- src/mljs/main.cpp | 30 +++- src/mlload/main.cpp | 31 +++- src/mllog/main.cpp | 25 +++- src/mlstatus/main.cpp | 26 +++- src/mlxq/main.cpp | 48 +++++-- src/query.cpp | 27 ++++ src/query.hpp | 16 +++ src/status.cpp | 27 ++++ src/status.hpp | 16 +++ 26 files changed, 836 insertions(+), 188 deletions(-) diff --git a/.gitignore b/.gitignore index 7c605f2..8af7002 100755 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,10 @@ CTestTestfile.cmake .DS_Store Testing cmake-build-debug -.idea \ No newline at end of file +.idea + +gnuplot* +googletest* +loguru* +rapidjson* + diff --git a/.idea/misc.xml b/.idea/misc.xml index f007c33..9f366f4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,6 +5,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/admin.cpp b/src/admin.cpp index 3639ea7..fd7eefd 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -13,144 +29,167 @@ namespace mlutil { -Admin::Admin() { -}; +/*! + * + */ + Admin::Admin() { + }; -Admin::~Admin() { -}; +/*! + * + */ + Admin::~Admin() { + }; -CommandLineArgs Admin::options(int n_opts, char *opts[]) { - CommandLineArgs args; +/*! + * + * @param n_opts + * @param opts + * @return + */ + CommandLineArgs Admin::options(int n_opts, char *opts[]) { + CommandLineArgs args; - for (int i = 1; i < n_opts; ++i) { - string optstr = opts[i]; - if (optstr == "--host") { - ++i; - args.chost = opts[i]; - } else if (optstr == "--port") { - ++i; - args.format = opts[i]; - } else if (optstr == "--user") { - ++i; - args.cuser = opts[i]; - } else if (optstr == "--pass") { - ++i; - args.cpass = opts[i]; - } else if (optstr == "--path") { - ++i; - args.cpath = opts[i]; - } else if (optstr == "--protocol") { - ++i; - args.cprotocol = opts[i]; - } else if (optstr == "--ml-config") { - ++i; - args.cmlconfig = opts[i]; - } else if (optstr == "-f") { - ++i; - args.format = opts[i]; - } else if (optstr == "-n") { - ++i; - args.name = opts[i]; - } else if (optstr == "-q") { - args.quiet = true; - } else if (optstr == "-r") { - ++i; - args.resource = opts[i]; - } else if (optstr == "-c") { - ++i; - args.config = opts[i]; - } else if (optstr == "-p") { - ++i; - args.payload = opts[i]; - } else if (optstr == "-v") { - args.verbose = true; - } else if (optstr == "-h") { - usage(opts[0]); - exit(1); - } else { - args.command = opts[i]; + for (int i = 1; i < n_opts; ++i) { + string optstr = opts[i]; + if (optstr == "--host") { + ++i; + args.chost = opts[i]; + } else if (optstr == "--port") { + ++i; + args.format = opts[i]; + } else if (optstr == "--user") { + ++i; + args.cuser = opts[i]; + } else if (optstr == "--pass") { + ++i; + args.cpass = opts[i]; + } else if (optstr == "--path") { + ++i; + args.cpath = opts[i]; + } else if (optstr == "--protocol") { + ++i; + args.cprotocol = opts[i]; + } else if (optstr == "--ml-config") { + ++i; + args.cmlconfig = opts[i]; + } else if (optstr == "-f") { + ++i; + args.format = opts[i]; + } else if (optstr == "-n") { + ++i; + args.name = opts[i]; + } else if (optstr == "-q") { + args.quiet = true; + } else if (optstr == "-r") { + ++i; + args.resource = opts[i]; + } else if (optstr == "-c") { + ++i; + args.config = opts[i]; + } else if (optstr == "-p") { + ++i; + args.payload = opts[i]; + } else if (optstr == "-v") { + args.verbose = true; + } else if (optstr == "-h") { + usage(opts[0]); + exit(1); + } else { + args.command = opts[i]; + } } + args.check(opts[0]); + return args; } - args.check(opts[0]); - return args; -} -int Admin::usage(const char *progname) { - const char *name = getprogname(progname); - cerr << "ml-utils: ml-config 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" - << "configure MarkLogic\n" - << ">" << name << " command [options]\n" - << " [command] : restart | get | get-properties | create | update | delete | install\n" - << " --ml-config : ml-config directory (ex. etc/ml-config)\n" - << " -c : config file (ex. /home/jfuller/.ml-utils\n" - << " -p : payload path \n" - << " -n : name \n" - << " -r : {resources}/{resource-name} directory\n" - << " -v : verbose (show http call)\n" - << " -q : quiet (suppress banner)" << endl; - return EXIT_SUCCESS; -} +/*! + * + * @param progname + * @return + */ + int Admin::usage(const char *progname) { + const char *name = getprogname(progname); + cerr << "ml-utils: ml-config 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" + << "configure MarkLogic\n" + << ">" << name << " command [options]\n" + << " [command] : restart | get | get-properties | create | update | delete | install\n" + << " --ml-config : ml-config directory (ex. etc/ml-config)\n" + << " -c : config file (ex. /home/jfuller/.ml-utils\n" + << " -p : payload path \n" + << " -n : name \n" + << " -r : {resources}/{resource-name} directory\n" + << " -v : verbose (show http call)\n" + << " -q : quiet (suppress banner)" << endl; + return EXIT_SUCCESS; + } -int Admin::walkInstallConfig(Admin admin, Config config, string path) { - DIR *dir; - struct dirent *ent; - if ((dir = opendir(path.c_str())) != NULL) { - while ((ent = readdir(dir)) != NULL) { - string name = ent->d_name; - string fullpath = path + "/" + name; - if (name != "." && name != ".." && name != ".DS_Store") { - if (name.find(".json") != std::string::npos) { - if (fullpath.find("forests") != std::string::npos) { - cout << "installing forest" << endl; - admin.setResourceUrl(config.port, config.path, "forests"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("databases") != std::string::npos) { - cout << "installing database" << endl; - admin.setResourceUrl(config.port, config.path, "databases"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("servers") != std::string::npos) { - cout << "installing server" << endl; - admin.setResourceUrl(config.port, config.path, "servers"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("amps") != std::string::npos) { - cout << "installing amps" << endl; - admin.setResourceUrl(config.port, config.path, "amps"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("external-securities") != std::string::npos) { - cout << "installing external-securities" << endl; - admin.setResourceUrl(config.port, config.path, "external-security"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("privileges") != std::string::npos) { - cout << "installing privileges" << endl; - admin.setResourceUrl(config.port, config.path, "privileges"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("security/roles") != std::string::npos) { - cout << "installing roles" << endl; - admin.setResourceUrl(config.port, config.path, "roles"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("users") != std::string::npos) { - cout << "installing users" << endl; - admin.setResourceUrl(config.port, config.path, "users"); - admin.executeInstallPost(fullpath, "json"); - } else if (fullpath.find("protected-paths") != std::string::npos) { - cout << "installing protected-paths" << endl; - admin.setResourceUrl(config.port, config.path, "protected-paths"); - admin.executeInstallPost(fullpath, "json"); +/*! + * + * @param admin + * @param config + * @param path + * @return + */ + int Admin::walkInstallConfig(Admin admin, Config config, string path) { + DIR *dir; + struct dirent *ent; + if ((dir = opendir(path.c_str())) != NULL) { + while ((ent = readdir(dir)) != NULL) { + string name = ent->d_name; + string fullpath = path + "/" + name; + if (name != "." && name != ".." && name != ".DS_Store") { + if (name.find(".json") != std::string::npos) { + if (fullpath.find("forests") != std::string::npos) { + cout << "installing forest" << endl; + admin.setResourceUrl(config.port, config.path, "forests"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("databases") != std::string::npos) { + cout << "installing database" << endl; + admin.setResourceUrl(config.port, config.path, "databases"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("servers") != std::string::npos) { + cout << "installing server" << endl; + admin.setResourceUrl(config.port, config.path, "servers"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("amps") != std::string::npos) { + cout << "installing amps" << endl; + admin.setResourceUrl(config.port, config.path, "amps"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("external-securities") != std::string::npos) { + cout << "installing external-securities" << endl; + admin.setResourceUrl(config.port, config.path, "external-security"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("privileges") != std::string::npos) { + cout << "installing privileges" << endl; + admin.setResourceUrl(config.port, config.path, "privileges"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("security/roles") != std::string::npos) { + cout << "installing roles" << endl; + admin.setResourceUrl(config.port, config.path, "roles"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("users") != std::string::npos) { + cout << "installing users" << endl; + admin.setResourceUrl(config.port, config.path, "users"); + admin.executeInstallPost(fullpath, "json"); + } else if (fullpath.find("protected-paths") != std::string::npos) { + cout << "installing protected-paths" << endl; + admin.setResourceUrl(config.port, config.path, "protected-paths"); + admin.executeInstallPost(fullpath, "json"); + } + } else if (name.find(".xml") != std::string::npos) { + cout << "ignoring xml properties" << endl; + } else { + walkInstallConfig(admin, config, fullpath); } - } else if (name.find(".xml") != std::string::npos) { - cout << "ignoring xml properties" << endl; - } else { - walkInstallConfig(admin, config, fullpath); } } + closedir(dir); + } else { + /* could not open directory */ + LOG_S(ERROR) << "problem installing"; + return EXIT_FAILURE; } - closedir(dir); - } else { - /* could not open directory */ - LOG_S(ERROR) << "problem installing"; - return EXIT_FAILURE; + return EXIT_SUCCESS; } - return EXIT_SUCCESS; -} - } \ No newline at end of file diff --git a/src/admin.hpp b/src/admin.hpp index a920c4e..9989b53 100755 --- a/src/admin.hpp +++ b/src/admin.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include @@ -22,6 +38,7 @@ using namespace std; namespace mlutil { + class Admin : public Command { public: Admin(); diff --git a/src/browse.cpp b/src/browse.cpp index 4b69b48..08facba 100755 --- a/src/browse.cpp +++ b/src/browse.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -11,15 +27,27 @@ #include "browse.hpp" namespace mlutil { - + + /*! + * + */ Browse::Browse() { //std::cout << "status constructor called\n"; }; + /*! + * + */ Browse::~Browse() { //std::cout << "status destructor called\n"; }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Browse::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -53,6 +81,11 @@ namespace mlutil { return args; } +/*! + * + * @param progname + * @return + */ int Browse::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-browse 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/browse.hpp b/src/browse.hpp index dd66546..0af9529 100755 --- a/src/browse.hpp +++ b/src/browse.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/command.hpp b/src/command.hpp index 0c60045..1560831 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include @@ -108,6 +124,9 @@ namespace mlutil { public: string url; + /*! + * + */ Command() { // set defaults loadConfig(config, current.config); @@ -138,6 +157,9 @@ namespace mlutil { setheaders(); }; + /*! + * + */ virtual ~Command() { //std::cout << "Default destructor called\n"; }; @@ -167,6 +189,10 @@ namespace mlutil { this->config = config; } + /*! + * + * @return + */ virtual bool checkConfig() { if (config.host.empty()) { cerr << "undefined MarkLogic host." << endl; @@ -182,6 +208,12 @@ namespace mlutil { } } + /*! + * + * @param port + * @param root + * @param resource + */ virtual void setResourceUrl(string port, string root, string resource) { @@ -189,6 +221,12 @@ namespace mlutil { url = config.protocol + "://" + config.host + ":" + port + root + "/" + resource + ""; } + /*! + * + * @param port + * @param root + * @param uri + */ virtual void setLoadUrl(string port, string root, string uri) { @@ -204,6 +242,17 @@ namespace mlutil { } } + /*! + * + * @param port + * @param root + * @param filename + * @param start + * @param end + * @param regex + * @param host + * @param format + */ virtual void setLogUrl(string port, string root, string filename, @@ -236,6 +285,13 @@ namespace mlutil { } } + /*! + * + * @param port + * @param root + * @param path + * @param view + */ virtual void setUrl(string port, string root, string path, @@ -251,6 +307,7 @@ namespace mlutil { } else if (view == "status") { url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; } else { + // identical to status for now url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; } @@ -310,6 +367,13 @@ namespace mlutil { } } + /*! + * + * @param port + * @param root + * @param path + * @param view + */ virtual void setQueryUrl(string port, string root, string path, @@ -323,6 +387,9 @@ namespace mlutil { } }; + /*! + * + */ void setheaders() { headers = curl_slist_append(headers, "Accept: text/plain"); headers = curl_slist_append(headers, "Accept: text/html"); @@ -331,6 +398,15 @@ namespace mlutil { headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded"); }; + /*! + * + * @param handle + * @param type + * @param data + * @param size + * @param userp + * @return + */ static int log_trace(CURL *handle, curl_infotype type, char *data, size_t size, @@ -372,6 +448,10 @@ namespace mlutil { } } + /*! + * + * @return + */ virtual int execute() { CURLM *curlm; @@ -423,12 +503,25 @@ namespace mlutil { }; + /*! + * + * @param contents + * @param size + * @param nmemb + * @param userp + * @return + */ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { ((std::string *) userp)->append((char *) contents, size * nmemb); return size * nmemb; } - + /*! + * + * @param type + * @param query + * @return + */ virtual int executeQueryPost(string type, string query) { CURLM *curlm; @@ -489,6 +582,12 @@ namespace mlutil { }; + /*! + * + * @param file + * @param body + * @return + */ virtual int executeLoadPost(const char *file, string body) { FILE *fd; @@ -543,6 +642,12 @@ namespace mlutil { return EXIT_SUCCESS; }; + /*! + * + * @param body + * @param format + * @return + */ virtual int executeResourcePost(string body, string format) { long http_code = 0; @@ -605,6 +710,12 @@ namespace mlutil { return EXIT_SUCCESS; }; + /*! + * + * @param body + * @param format + * @return + */ virtual int executeResourcePut(string body, string format) { CURLM *curlm; @@ -668,6 +779,12 @@ namespace mlutil { }; + /*! + * + * @param filename + * @param format + * @return + */ virtual int executeInstallPost(string filename, string format) { std::ifstream ifs(filename); string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); @@ -736,6 +853,10 @@ namespace mlutil { }; + /*! + * + * @return + */ int displayargs() { LOG_S(INFO) << "----------------"; LOG_S(INFO) << "options" << endl; @@ -767,6 +888,11 @@ namespace mlutil { return EXIT_SUCCESS; }; + /*! + * + * @param progname + * @return + */ const char *getprogname(const char *progname) { const char *name = progname; while (*progname != 0) { @@ -780,6 +906,15 @@ namespace mlutil { return name; }; + /*! + * + * @param output + * @param plot + * @param units + * @param name + * @param resource + * @param entries + */ void doPlot(string output, string plot, string units, string name, string resource, const Value &entries) { //Gnuplot::set_terminal_std("dumb"); diff --git a/src/config.hpp b/src/config.hpp index a442c7a..410542a 100755 --- a/src/config.hpp +++ b/src/config.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/history.cpp b/src/history.cpp index 3a9f78f..7b6531a 100755 --- a/src/history.cpp +++ b/src/history.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -23,6 +39,12 @@ namespace mlutil { //std::cout << "history destructor called\n"; }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs History::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -73,6 +95,11 @@ namespace mlutil { return args; } + /*! + * + * @param progname + * @return + */ int History::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-hist 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/history.hpp b/src/history.hpp index e41b2f7..4baf810 100755 --- a/src/history.hpp +++ b/src/history.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/load.cpp b/src/load.cpp index 8f931e1..7038321 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -18,6 +34,12 @@ namespace mlutil { Load::~Load() {//std::cout << "load destructor called\n"; }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Load::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -49,6 +71,11 @@ namespace mlutil { return args; } + /*! + * + * @param progname + * @return + */ int Load::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-load 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/load.hpp b/src/load.hpp index 5eea469..b68b8b3 100755 --- a/src/load.hpp +++ b/src/load.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/log.cpp b/src/log.cpp index 69bbe9c..361f2d0 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -18,6 +34,12 @@ namespace mlutil { Log::~Log() { }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Log::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -64,6 +86,11 @@ namespace mlutil { return args; } + /*! + * + * @param progname + * @return + */ int Log::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-log 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/log.hpp b/src/log.hpp index bf56a18..bf296b0 100755 --- a/src/log.hpp +++ b/src/log.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/mlbrowse/main.cpp b/src/mlbrowse/main.cpp index 1db23c2..436a304 100755 --- a/src/mlbrowse/main.cpp +++ b/src/mlbrowse/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-browse - utility for browsing documents in an MarkLogic database. + */ + #include #include #include diff --git a/src/mlconfigure/main.cpp b/src/mlconfigure/main.cpp index 708a4fd..cd5c765 100755 --- a/src/mlconfigure/main.cpp +++ b/src/mlconfigure/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-config - utility for setting up resources on MarkLogic server. + */ + #include #include #include @@ -5,11 +25,7 @@ #include #include #include -#include #include -#include -#include -#include #include "../admin.cpp" @@ -34,52 +50,52 @@ int main(int argc, char *argv[]) { if (command == "restart") { admin.setResourceUrl(config.port, config.path, ""); admin.executeResourcePost("{\"operation\":\"restart-local-cluster\"}", ""); - }else if(command == "get"){ + } else if (command == "get") { admin.setUrl(config.port, config.path, current.resource, "default"); admin.execute(); - }else if(command == "get-properties") { + } else if (command == "get-properties") { admin.setUrl(config.port, config.path, resource + "/properties", "default"); admin.execute(); - }else if(command == "create"){ + } else if (command == "create") { string line, body; - if(name.empty()){ + if (name.empty()) { while (getline(std::cin, line)) { body.append(line); } } admin.setResourceUrl(config.port, config.path, resource); - if(body.empty()){ + if (body.empty()) { string payload; - if(resource == "forests"){ - payload="{\"forest-name\":\""+name+"\"}"; - }else if(resource=="databases"){ - payload="{\"database-name\":\""+name+"\"}"; - }else if(resource=="servers"){ - payload="{\"server-name\":\""+name+"\"}"; - }else if(resource=="groups"){ - payload="{\"group-name\":\""+name+"\"}"; + if (resource == "forests") { + payload = "{\"forest-name\":\"" + name + "\"}"; + } else if (resource == "databases") { + payload = "{\"database-name\":\"" + name + "\"}"; + } else if (resource == "servers") { + payload = "{\"server-name\":\"" + name + "\"}"; + } else if (resource == "groups") { + payload = "{\"group-name\":\"" + name + "\"}"; } - admin.executeResourcePost(payload,resource); - }else{ - admin.executeResourcePost(body,resource); + admin.executeResourcePost(payload, resource); + } else { + admin.executeResourcePost(body, resource); } - }else if(command == "update"){ + } else if (command == "update") { string line, body; while (getline(std::cin, line)) { body.append(line); } admin.setResourceUrl(config.port, config.path, resource + "/properties"); admin.executeResourcePut(body, resource); - }else if(command == "install" || command == "reinstall"){ + } else if (command == "install" || command == "reinstall") { string path = config.mlconfig; cout << path << endl; - admin.walkInstallConfig(admin,config,path); - }else{ + admin.walkInstallConfig(admin, config, path); + } else { LOG_S(ERROR) << "invalid command."; return EXIT_FAILURE; } cout << admin.getReadBuffer() << endl; - }else{ + } else { LOG_S(ERROR) << "no command supplied."; return EXIT_FAILURE; } diff --git a/src/mlhistory/main.cpp b/src/mlhistory/main.cpp index dadad47..b957be3 100755 --- a/src/mlhistory/main.cpp +++ b/src/mlhistory/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-hist - utility for retrieving historical metrics from MarkLogic server. + */ + #include #include #include @@ -5,14 +25,9 @@ #include #include #include -#include #include #include -#include -#include -#include -#include #include "../history.cpp" @@ -43,7 +58,7 @@ int main(int argc, char *argv[]) { string plot = string(current.gnuplot); string resource = string(current.resource); - if(result.empty()){ + if (result.empty()) { LOG_S(ERROR) << "no output."; } diff --git a/src/mljs/main.cpp b/src/mljs/main.cpp index dbefb8f..875df5f 100755 --- a/src/mljs/main.cpp +++ b/src/mljs/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-js - utility for evaluating javascript against a MarkLogic server. + */ + #include #include #include @@ -36,7 +56,7 @@ int main(int argc, char *argv[]) { query.executeQueryPost("javascript", current.js); } - if(!current.quiet) { + if (!current.quiet) { string result = query.getReadBuffer(); // count number of lines std::stringstream cc(result); @@ -55,7 +75,9 @@ int main(int argc, char *argv[]) { if (count > 4 && count != countline - 1) { cout << to << endl; } - count++; }} + count++; + } + } } else { std::stringstream ss(result); std::string to; @@ -65,7 +87,9 @@ int main(int argc, char *argv[]) { if (count > 4 && count != countline - 1) { cout << to << endl; } - count++; }} + count++; + } + } } } } catch (std::bad_alloc) { diff --git a/src/mlload/main.cpp b/src/mlload/main.cpp index 7839818..a2e559d 100755 --- a/src/mlload/main.cpp +++ b/src/mlload/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-load - utility for loading files into MarkLogic. + */ + #include #include #include @@ -25,11 +45,12 @@ int main(int argc, char *argv[]) { } load.setLoadUrl(config.port, "/v1/documents", current.uri); std::string line, body; - if(!current.filename){ - while (std::getline(std::cin, line)) { - body.append(line); - }} - load.executeLoadPost(current.filename,body); + if (!current.filename) { + while (std::getline(std::cin, line)) { + body.append(line); + } + } + load.executeLoadPost(current.filename, body); } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-load."; return EXIT_FAILURE; diff --git a/src/mllog/main.cpp b/src/mllog/main.cpp index 0a79fe9..ad6123f 100755 --- a/src/mllog/main.cpp +++ b/src/mllog/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-log - utility for retrieving MarkLogic logs. + */ + #include #include #include @@ -26,10 +46,11 @@ int main(int argc, char *argv[]) { } log.setUrl(config.port, config.path + "/logs", "", ""); - log.setLogUrl(config.port, config.path + "/logs",current.name,current.start,current.end,current.regex,current.host,"text"); + log.setLogUrl(config.port, config.path + "/logs", current.name, current.start, current.end, current.regex, + current.host, "text"); log.execute(); - if(!current.quiet) { + if (!current.quiet) { cout << log.getReadBuffer() << endl; } } catch (std::bad_alloc) { diff --git a/src/mlstatus/main.cpp b/src/mlstatus/main.cpp index e4112fe..6226ed7 100755 --- a/src/mlstatus/main.cpp +++ b/src/mlstatus/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-status - utility for retrieving MarkLogic resource statuses. + */ + #include #include #include @@ -8,10 +28,6 @@ #include #include -#include -#include -#include -#include #include "../status.cpp" @@ -35,7 +51,7 @@ int main(int argc, char *argv[]) { status.setUrl(config.port, config.path, current.resource, "status"); status.execute(); - if(!current.quiet) { + if (!current.quiet) { string result = status.getReadBuffer(); if (strcmp(current.format, "json") == 0) { cout << result << endl; diff --git a/src/mlxq/main.cpp b/src/mlxq/main.cpp index af19007..8aefc11 100755 --- a/src/mlxq/main.cpp +++ b/src/mlxq/main.cpp @@ -1,3 +1,23 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * entry point for ml-xq - utility for evaluating xquery against a MarkLogic server. + */ + #include #include #include @@ -6,8 +26,6 @@ #include #include #include -#include -#include #include "../query.cpp" @@ -44,12 +62,13 @@ int main(int argc, char *argv[]) { std::string un; int countline = 0; while (std::getline(cc, un, '\n')) { - countline++; } + countline++; + } if (strcmp(current.format, "json") == 0) { - if(current.raw){ - cout<< result << endl; - }else { + if (current.raw) { + cout << result << endl; + } else { std::stringstream ss(result); std::string to; if (!result.empty()) { @@ -58,11 +77,14 @@ int main(int argc, char *argv[]) { if (count > 4 && count != countline - 1) { cout << to << endl; } - count++; }}} + count++; + } + } + } } else { - if(current.raw){ - cout<< result << endl; - }else { + if (current.raw) { + cout << result << endl; + } else { std::stringstream ss(result); std::string to; if (!result.empty()) { @@ -71,7 +93,11 @@ int main(int argc, char *argv[]) { if (count > 4 && count != countline - 1) { cout << to << endl; } - count++; }}}} + count++; + } + } + } + } } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-xq."; return EXIT_FAILURE; diff --git a/src/query.cpp b/src/query.cpp index b3f02f3..2b75cdd 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -18,6 +34,12 @@ namespace mlutil { Query::~Query() { }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Query::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -57,6 +79,11 @@ namespace mlutil { return args; } + /*! + * + * @param progname + * @return + */ int Query::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: " << name << " 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/query.hpp b/src/query.hpp index f4e6b3a..2d2b717 100755 --- a/src/query.hpp +++ b/src/query.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include diff --git a/src/status.cpp b/src/status.cpp index 39c2280..c81f0ae 100755 --- a/src/status.cpp +++ b/src/status.cpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -20,6 +36,12 @@ namespace mlutil { //std::cout << "status destructor called\n"; }; + /*! + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Status::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -53,6 +75,11 @@ namespace mlutil { return args; } + /*! + * + * @param progname + * @return + */ int Status::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-status 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/status.hpp b/src/status.hpp index 0701a28..6c7c18c 100755 --- a/src/status.hpp +++ b/src/status.hpp @@ -1,3 +1,19 @@ +/* + * Copyright 2017 James Fuller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #include From 9c16672300f14c9511f40d46fba555329ca36947 Mon Sep 17 00:00:00 2001 From: xquery Date: Thu, 25 May 2017 21:07:13 +0200 Subject: [PATCH 07/11] tidy up comments --- src/admin.cpp | 48 ++++++++++++++++++++++++------------------------ src/browse.cpp | 16 ++++++++-------- src/command.hpp | 42 +++++++++++++++++++++--------------------- src/history.cpp | 10 ++++++++-- src/load.cpp | 10 ++++++++-- src/log.cpp | 10 ++++++++-- src/query.cpp | 10 ++++++++-- src/status.cpp | 10 ++++++++-- 8 files changed, 93 insertions(+), 63 deletions(-) diff --git a/src/admin.cpp b/src/admin.cpp index fd7eefd..8bf31e5 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -29,24 +29,24 @@ namespace mlutil { -/*! - * - */ + /*! + * + */ Admin::Admin() { }; -/*! - * - */ + /*! + * + */ Admin::~Admin() { }; -/*! - * - * @param n_opts - * @param opts - * @return - */ + /*! Admin::options + * + * @param n_opts + * @param opts + * @return + */ CommandLineArgs Admin::options(int n_opts, char *opts[]) { CommandLineArgs args; @@ -103,11 +103,11 @@ namespace mlutil { return args; } -/*! - * - * @param progname - * @return - */ + /*! Admin::usage + * + * @param progname + * @return + */ int Admin::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-config 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" @@ -124,13 +124,13 @@ namespace mlutil { return EXIT_SUCCESS; } -/*! - * - * @param admin - * @param config - * @param path - * @return - */ + /*! Admin::walkInstallConfig + * + * @param admin + * @param config + * @param path + * @return + */ int Admin::walkInstallConfig(Admin admin, Config config, string path) { DIR *dir; struct dirent *ent; diff --git a/src/browse.cpp b/src/browse.cpp index 08facba..e0bd11c 100755 --- a/src/browse.cpp +++ b/src/browse.cpp @@ -28,21 +28,21 @@ namespace mlutil { - /*! + /*! Browse::Browse * */ Browse::Browse() { //std::cout << "status constructor called\n"; }; - /*! + /*! BRowse::~Browse * */ Browse::~Browse() { //std::cout << "status destructor called\n"; }; - /*! + /*! Browse::options * * @param n_opts * @param opts @@ -81,11 +81,11 @@ namespace mlutil { return args; } -/*! - * - * @param progname - * @return - */ + /*! Browse::usagee + * + * @param progname + * @return + */ int Browse::usage(const char *progname) { const char *name = getprogname(progname); cerr << "ml-utils: ml-browse 1.0 | copyright (c)2015 Jim Fuller | see https://github.com/xquery/ml-utils\n" diff --git a/src/command.hpp b/src/command.hpp index 1560831..be978b8 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -112,7 +112,7 @@ namespace mlutil { bool quiet, verbose, raw; }; -// interface for commands + // interface for commands class Command { private: Config config; @@ -124,7 +124,7 @@ namespace mlutil { public: string url; - /*! + /*! Command * */ Command() { @@ -157,7 +157,7 @@ namespace mlutil { setheaders(); }; - /*! + /*! ~Command * */ virtual ~Command() { @@ -189,7 +189,7 @@ namespace mlutil { this->config = config; } - /*! + /*! checkConfig * * @return */ @@ -208,7 +208,7 @@ namespace mlutil { } } - /*! + /*! setResourceUrl * * @param port * @param root @@ -221,7 +221,7 @@ namespace mlutil { url = config.protocol + "://" + config.host + ":" + port + root + "/" + resource + ""; } - /*! + /*! setLoadUrl * * @param port * @param root @@ -242,7 +242,7 @@ namespace mlutil { } } - /*! + /*! setLogUrl * * @param port * @param root @@ -285,7 +285,7 @@ namespace mlutil { } } - /*! + /*! setUrl * * @param port * @param root @@ -367,7 +367,7 @@ namespace mlutil { } } - /*! + /*! setQueryUrl * * @param port * @param root @@ -387,7 +387,7 @@ namespace mlutil { } }; - /*! + /*! setheaders * */ void setheaders() { @@ -398,7 +398,7 @@ namespace mlutil { headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded"); }; - /*! + /*! log_trace * * @param handle * @param type @@ -448,7 +448,7 @@ namespace mlutil { } } - /*! + /*! execute * * @return */ @@ -503,7 +503,7 @@ namespace mlutil { }; - /*! + /*! WriteCallback * * @param contents * @param size @@ -516,7 +516,7 @@ namespace mlutil { return size * nmemb; } - /*! + /*! executeQueryPost * * @param type * @param query @@ -582,7 +582,7 @@ namespace mlutil { }; - /*! + /*! executeLoadPost * * @param file * @param body @@ -642,7 +642,7 @@ namespace mlutil { return EXIT_SUCCESS; }; - /*! + /*! executeResourcePost * * @param body * @param format @@ -710,7 +710,7 @@ namespace mlutil { return EXIT_SUCCESS; }; - /*! + /*! executeResourcePut * * @param body * @param format @@ -779,7 +779,7 @@ namespace mlutil { }; - /*! + /*! executeInstallPost * * @param filename * @param format @@ -853,7 +853,7 @@ namespace mlutil { }; - /*! + /*! displayargs * * @return */ @@ -888,7 +888,7 @@ namespace mlutil { return EXIT_SUCCESS; }; - /*! + /*! getprogname * * @param progname * @return @@ -906,7 +906,7 @@ namespace mlutil { return name; }; - /*! + /*! doPlot * * @param output * @param plot diff --git a/src/history.cpp b/src/history.cpp index 7b6531a..e0b9a13 100755 --- a/src/history.cpp +++ b/src/history.cpp @@ -31,15 +31,21 @@ using namespace std; namespace mlutil { + /* History::History + * + */ History::History() { //std::cout << "history constructor called\n"; }; + /* History::~History + * + */ History::~History() { //std::cout << "history destructor called\n"; }; - /*! + /*! History::options * * @param n_opts * @param opts @@ -95,7 +101,7 @@ namespace mlutil { return args; } - /*! + /*! History::usage * * @param progname * @return diff --git a/src/load.cpp b/src/load.cpp index 7038321..b5c25cd 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -28,13 +28,19 @@ namespace mlutil { + /* Load::Load + * + */ Load::Load() {//std::cout << "load constructor called\n"; }; + /* Load::~Load + * + */ Load::~Load() {//std::cout << "load destructor called\n"; }; - /*! + /*! Load::options * * @param n_opts * @param opts @@ -71,7 +77,7 @@ namespace mlutil { return args; } - /*! + /*! Load::usage * * @param progname * @return diff --git a/src/log.cpp b/src/log.cpp index 361f2d0..eadb8c8 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -28,13 +28,19 @@ namespace mlutil { + /* Log::Log + * + */ Log::Log() { }; + /* Log::~Log + * + */ Log::~Log() { }; - /*! + /*! Log::options * * @param n_opts * @param opts @@ -86,7 +92,7 @@ namespace mlutil { return args; } - /*! + /*! Log::usage * * @param progname * @return diff --git a/src/query.cpp b/src/query.cpp index 2b75cdd..804d265 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -28,13 +28,19 @@ namespace mlutil { + /* Query::Query + * + */ Query::Query() { }; + /* Query::~Query + * + */ Query::~Query() { }; - /*! + /*! Query::options * * @param n_opts * @param opts @@ -79,7 +85,7 @@ namespace mlutil { return args; } - /*! + /*! Query::usage * * @param progname * @return diff --git a/src/status.cpp b/src/status.cpp index c81f0ae..fae77bb 100755 --- a/src/status.cpp +++ b/src/status.cpp @@ -28,15 +28,21 @@ namespace mlutil { + /* Status::Status + * + */ Status::Status() { //std::cout << "status constructor called\n"; }; + /* Status::~Status + * + */ Status::~Status() { //std::cout << "status destructor called\n"; }; - /*! + /*! Status::options * * @param n_opts * @param opts @@ -75,7 +81,7 @@ namespace mlutil { return args; } - /*! + /*! Status::usage * * @param progname * @return From c9348812f398c8a9fc4ebfba907e84333d2196cf Mon Sep 17 00:00:00 2001 From: xquery Date: Sat, 27 May 2017 09:21:37 +0200 Subject: [PATCH 08/11] refactor command --- src/admin.cpp | 211 ++++++++++++++++++++++++ src/admin.hpp | 6 + src/command.hpp | 428 +----------------------------------------------- src/load.cpp | 80 +++++++++ src/load.hpp | 4 + src/log.cpp | 44 +++++ src/log.hpp | 4 + src/query.cpp | 87 ++++++++++ src/query.hpp | 4 + 9 files changed, 444 insertions(+), 424 deletions(-) diff --git a/src/admin.cpp b/src/admin.cpp index 8bf31e5..ec01863 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -192,4 +192,215 @@ namespace mlutil { } return EXIT_SUCCESS; } + + /*! executeResourcePut + * + * @param body + * @param format + * @return + */ + int Admin::executeResourcePut(string body, string format) { + + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + + CURL *curl1 = NULL; + curl1 = curl_easy_init(); + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + + if (format == "xml") { + headers = curl_slist_append(headers, "Content-Type: application/xml"); + } else { + headers = curl_slist_append(headers, "Content-Type: application/json"); + } + + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(curl1, CURLOPT_PUT, 1L); + curl_easy_setopt(curl1, CURLOPT_READDATA, body.c_str()); + + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + + if (handle_count == 0) { + curl_global_cleanup(); + + break; + } + } + //std::cout << readBuffer << std::endl; + } + + curl_global_cleanup(); + + return EXIT_SUCCESS; + + }; + + /*! executeResourcePost + * + * @param body + * @param format + * @return + */ + int Admin::executeResourcePost(string body, string format) { + + long http_code = 0; + + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + + CURL *curl1 = NULL; + curl1 = curl_easy_init(); + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + + if (format == "xml") { + curl_slist_append(headers, "Content-Type: application/xml"); + } else { + curl_slist_append(headers, "Content-Type: application/json"); + } + + if (curl1) { + + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); + curl_easy_setopt(curl1, CURLOPT_POSTFIELDSIZE, body.length()); + curl_easy_setopt(curl1, CURLOPT_POST, 1); + + curl_easy_getinfo (curl1, CURLINFO_RESPONSE_CODE, &http_code); + + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } + } + } + curl_global_cleanup(); + return EXIT_SUCCESS; + }; + + /*! executeInstallPost + * + * @param filename + * @param format + * @return + */ + int Admin::executeInstallPost(string filename, string format) { + std::ifstream ifs(filename); + string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + + CURL *curl1 = NULL; + curl1 = curl_easy_init(); + + struct curl_httppost *post = NULL; + struct curl_httppost *last = NULL; + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + if (format == "xml") { + headers = curl_slist_append(headers, "Content-Type: application/xml"); + } else { + headers = curl_slist_append(headers, "Content-Type: application/json"); + } + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); + curl_easy_setopt(curl1, CURLOPT_POST, 1L); + + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } + } + //std::cout << readBuffer << std::endl; + } + + curl_global_cleanup(); + return EXIT_SUCCESS; + + }; } \ No newline at end of file diff --git a/src/admin.hpp b/src/admin.hpp index 9989b53..901387e 100755 --- a/src/admin.hpp +++ b/src/admin.hpp @@ -50,5 +50,11 @@ namespace mlutil { virtual int usage(const char *progname); virtual int walkInstallConfig(Admin admin, Config config, string path); + + virtual int executeResourcePut(string body, string format); + + virtual int executeResourcePost(string body, string format); + + virtual int executeInstallPost(string filename, string format); }; } \ No newline at end of file diff --git a/src/command.hpp b/src/command.hpp index be978b8..87390a2 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -115,11 +115,12 @@ namespace mlutil { // interface for commands class Command { private: - Config config; - CommandLineArgs current; - string readBuffer; + protected: + CommandLineArgs current; + Config config; struct curl_slist *headers = NULL; + string readBuffer; public: string url; @@ -221,70 +222,6 @@ namespace mlutil { url = config.protocol + "://" + config.host + ":" + port + root + "/" + resource + ""; } - /*! setLoadUrl - * - * @param port - * @param root - * @param uri - */ - virtual void setLoadUrl(string port, - string root, - string uri) { - checkConfig(); - - url = config.protocol + "://" + config.host + ":" + port + root + "?"; - if (!uri.empty()) { - url += "uri=" + uri; - } - string database = current.database; - if (!database.empty()) { - url += "&database=" + database; - } - } - - /*! setLogUrl - * - * @param port - * @param root - * @param filename - * @param start - * @param end - * @param regex - * @param host - * @param format - */ - virtual void setLogUrl(string port, - string root, - string filename, - string start, - string end, - string regex, - string host, - string format) { - checkConfig(); - - url = config.protocol + "://" + config.host + ":" + port + root + "?"; - if (!filename.empty()) { - url += "filename=" + filename; - } - if (!start.empty()) { - url += "&start=" + start; - } - if (!end.empty()) { - url += "&end=" + end; - } - if (!regex.empty()) { - url += "®ex=" + regex; - } - if (!host.empty()) { - url += "&host=" + host; - } - //string format = current.format; - if (format != "") { - url += "&format=" + format; - } - } - /*! setUrl * * @param port @@ -367,26 +304,6 @@ namespace mlutil { } } - /*! setQueryUrl - * - * @param port - * @param root - * @param path - * @param view - */ - virtual void setQueryUrl(string port, - string root, - string path, - string view) { - checkConfig(); - url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; - - string database = current.database; - if (!database.empty()) { - url += "&database=" + database; - } - }; - /*! setheaders * */ @@ -516,343 +433,6 @@ namespace mlutil { return size * nmemb; } - /*! executeQueryPost - * - * @param type - * @param query - * @return - */ - virtual int executeQueryPost(string type, string query) { - - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); - string xquery = type + "=" + data; - curl_easy_setopt(curl1, CURLOPT_POST, 1); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, xquery.c_str()); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - - if (handle_count == 0) { - curl_global_cleanup(); - - break; - } - } - //std::cout << readBuffer << std::endl; - } - - curl_global_cleanup(); - - return EXIT_SUCCESS; - - }; - - /*! executeLoadPost - * - * @param file - * @param body - * @return - */ - virtual int executeLoadPost(const char *file, string body) { - - FILE *fd; - - fd = fopen(file, "rb"); /* open file to upload */ - - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); - curl_easy_setopt(curl1, CURLOPT_READDATA, fd); - - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; - } - } - } - curl_global_cleanup(); - return EXIT_SUCCESS; - }; - - /*! executeResourcePost - * - * @param body - * @param format - * @return - */ - virtual int executeResourcePost(string body, string format) { - - long http_code = 0; - - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - - if (format == "xml") { - curl_slist_append(headers, "Content-Type: application/xml"); - } else { - curl_slist_append(headers, "Content-Type: application/json"); - } - - if (curl1) { - - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDSIZE, body.length()); - curl_easy_setopt(curl1, CURLOPT_POST, 1); - - curl_easy_getinfo (curl1, CURLINFO_RESPONSE_CODE, &http_code); - - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; - } - } - } - curl_global_cleanup(); - return EXIT_SUCCESS; - }; - - /*! executeResourcePut - * - * @param body - * @param format - * @return - */ - virtual int executeResourcePut(string body, string format) { - - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - - if (format == "xml") { - headers = curl_slist_append(headers, "Content-Type: application/xml"); - } else { - headers = curl_slist_append(headers, "Content-Type: application/json"); - } - - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); - curl_easy_setopt(curl1, CURLOPT_PUT, 1L); - curl_easy_setopt(curl1, CURLOPT_READDATA, body.c_str()); - - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - - if (handle_count == 0) { - curl_global_cleanup(); - - break; - } - } - //std::cout << readBuffer << std::endl; - } - - curl_global_cleanup(); - - return EXIT_SUCCESS; - - }; - - /*! executeInstallPost - * - * @param filename - * @param format - * @return - */ - virtual int executeInstallPost(string filename, string format) { - std::ifstream ifs(filename); - string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - - CURLM *curlm; - int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - struct curl_httppost *post = NULL; - struct curl_httppost *last = NULL; - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - if (format == "xml") { - headers = curl_slist_append(headers, "Content-Type: application/xml"); - } else { - headers = curl_slist_append(headers, "Content-Type: application/json"); - } - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); - - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); - curl_easy_setopt(curl1, CURLOPT_POST, 1L); - - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - - curl_multi_add_handle(curlm, curl1); - CURLMcode code; - while (1) { - code = curl_multi_perform(curlm, &handle_count); - if (handle_count == 0) { - curl_global_cleanup(); - break; - } - } - //std::cout << readBuffer << std::endl; - } - - curl_global_cleanup(); - return EXIT_SUCCESS; - - }; - /*! displayargs * * @return diff --git a/src/load.cpp b/src/load.cpp index b5c25cd..8bb4765 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -95,4 +95,84 @@ namespace mlutil { << " -q : quiet (suppress banner)" << endl; return EXIT_SUCCESS; } + + /*! setLoadUrl + * + * @param port + * @param root + * @param uri + */ + void Load::setLoadUrl(string port, + string root, + string uri) { + checkConfig(); + + url = config.protocol + "://" + config.host + ":" + port + root + "?"; + if (!uri.empty()) { + url += "uri=" + uri; + } + string database = current.database; + if (!database.empty()) { + url += "&database=" + database; } + } + + /*! executeLoadPost + * + * @param file + * @param body + * @return + */ + int Load::executeLoadPost(const char *file, string body) { + + FILE *fd; + + fd = fopen(file, "rb"); /* open file to upload */ + + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + + CURL *curl1 = NULL; + curl1 = curl_easy_init(); + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(curl1, CURLOPT_READDATA, fd); + + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + if (handle_count == 0) { + curl_global_cleanup(); + break; + } + } + } + curl_global_cleanup(); + return EXIT_SUCCESS; + }; } \ No newline at end of file diff --git a/src/load.hpp b/src/load.hpp index b68b8b3..1892ad6 100755 --- a/src/load.hpp +++ b/src/load.hpp @@ -48,5 +48,9 @@ namespace mlutil { virtual CommandLineArgs options(int n_opts, char *opts[]); virtual int usage(const char *progname); + + virtual void setLoadUrl(string port, string root, string uri); + + virtual int executeLoadPost(const char *file, string body); }; } diff --git a/src/log.cpp b/src/log.cpp index eadb8c8..e7203f9 100755 --- a/src/log.cpp +++ b/src/log.cpp @@ -113,4 +113,48 @@ namespace mlutil { << " -q : quiet (suppress banner)" << endl; return EXIT_SUCCESS; } + + + /*! setLogUrl + * + * @param port + * @param root + * @param filename + * @param start + * @param end + * @param regex + * @param host + * @param format + */ + void Log::setLogUrl(string port, + string root, + string filename, + string start, + string end, + string regex, + string host, + string format) { + checkConfig(); + + url = config.protocol + "://" + config.host + ":" + port + root + "?"; + if (!filename.empty()) { + url += "filename=" + filename; + } + if (!start.empty()) { + url += "&start=" + start; + } + if (!end.empty()) { + url += "&end=" + end; + } + if (!regex.empty()) { + url += "®ex=" + regex; + } + if (!host.empty()) { + url += "&host=" + host; + } + //string format = current.format; + if (format != "") { + url += "&format=" + format; + } + } } \ No newline at end of file diff --git a/src/log.hpp b/src/log.hpp index bf296b0..0229aeb 100755 --- a/src/log.hpp +++ b/src/log.hpp @@ -48,5 +48,9 @@ namespace mlutil { virtual CommandLineArgs options(int n_opts, char *opts[]); virtual int usage(const char *progname); + + virtual void setLogUrl(string port, string root, string filename, + string start, string end, string regex, + string host, string format); }; } \ No newline at end of file diff --git a/src/query.cpp b/src/query.cpp index 804d265..5289d1f 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -103,4 +103,91 @@ namespace mlutil { << " -q : quiet (suppress banner)" << endl; return EXIT_SUCCESS; } + + /*! setQueryUrl + * + * @param port + * @param root + * @param path + * @param view + */ + void Query::setQueryUrl(string port, + string root, + string path, + string view) { + checkConfig(); + url = config.protocol + "://" + config.host + ":" + port + root + "/" + path + "?"; + + string database = current.database; + if (!database.empty()) { + url += "&database=" + database; + } + }; + + /*! executeQueryPost + * + * @param type + * @param query + * @return + */ + int Query::executeQueryPost(string type, string query) { + + CURLM *curlm; + int handle_count; + curlm = curl_multi_init(); + + CURL *curl1 = NULL; + curl1 = curl_easy_init(); + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + + if (curl1) { + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); + + char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); + string xquery = type + "=" + data; + curl_easy_setopt(curl1, CURLOPT_POST, 1); + curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, xquery.c_str()); + curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); + + curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); + + curl_multi_add_handle(curlm, curl1); + CURLMcode code; + while (1) { + code = curl_multi_perform(curlm, &handle_count); + + if (handle_count == 0) { + curl_global_cleanup(); + + break; + } + } + //std::cout << readBuffer << std::endl; + } + + curl_global_cleanup(); + + return EXIT_SUCCESS; + + }; + } \ No newline at end of file diff --git a/src/query.hpp b/src/query.hpp index 2d2b717..1d935fb 100755 --- a/src/query.hpp +++ b/src/query.hpp @@ -48,5 +48,9 @@ namespace mlutil { virtual CommandLineArgs options(int n_opts, char *opts[]); virtual int usage(const char *progname); + + virtual void setQueryUrl(string port, string root, string path, string view); + + virtual int executeQueryPost(string type, string query); }; } \ No newline at end of file From cc87bbcbc21c60434e5cb8f1be6e394d8caf9cfa Mon Sep 17 00:00:00 2001 From: xquery Date: Sat, 27 May 2017 11:46:31 +0200 Subject: [PATCH 09/11] tidy up --- src/admin.cpp | 56 ++----------------------------------------------- src/command.hpp | 38 ++++++++++++++++++++++++++++++--- src/load.cpp | 27 +----------------------- src/query.cpp | 29 ++----------------------- 4 files changed, 40 insertions(+), 110 deletions(-) diff --git a/src/admin.cpp b/src/admin.cpp index ec01863..6886a6d 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -201,11 +201,9 @@ namespace mlutil { */ int Admin::executeResourcePut(string body, string format) { - CURLM *curlm; int handle_count; curlm = curl_multi_init(); - CURL *curl1 = NULL; curl1 = curl_easy_init(); curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); @@ -272,40 +270,18 @@ namespace mlutil { long http_code = 0; - CURLM *curlm; int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); if (format == "xml") { curl_slist_append(headers, "Content-Type: application/xml"); } else { curl_slist_append(headers, "Content-Type: application/json"); } + setCurlOpts(); if (curl1) { curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); @@ -316,7 +292,6 @@ namespace mlutil { curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_multi_add_handle(curlm, curl1); CURLMcode code; while (1) { code = curl_multi_perform(curlm, &handle_count); @@ -326,7 +301,6 @@ namespace mlutil { } } } - curl_global_cleanup(); return EXIT_SUCCESS; }; @@ -340,12 +314,7 @@ namespace mlutil { std::ifstream ifs(filename); string body = string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - CURLM *curlm; int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); struct curl_httppost *post = NULL; struct curl_httppost *last = NULL; @@ -361,27 +330,9 @@ namespace mlutil { headers = curl_slist_append(headers, "Content-Type: application/json"); } - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + setCurlOpts(); if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); curl_easy_setopt(curl1, CURLOPT_POST, 1L); @@ -398,9 +349,6 @@ namespace mlutil { } //std::cout << readBuffer << std::endl; } - - curl_global_cleanup(); return EXIT_SUCCESS; - }; } \ No newline at end of file diff --git a/src/command.hpp b/src/command.hpp index 87390a2..b1b57a5 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -121,14 +121,17 @@ namespace mlutil { Config config; struct curl_slist *headers = NULL; string readBuffer; + CURLM *curlm; + CURL *curl1 = NULL; + string url; public: - string url; /*! Command * */ Command() { + // set defaults loadConfig(config, current.config); if (config.user.empty()) { @@ -155,18 +158,48 @@ namespace mlutil { if (config.mllog.empty()) { config.mllog = current.cmllog; } - setheaders(); + + setCurlOpts(); }; /*! ~Command * */ virtual ~Command() { + curl_global_cleanup(); //std::cout << "Default destructor called\n"; }; virtual int usage(const char *progname) =0; + virtual void setCurlOpts(){ + setheaders(); + + curlm = curl_multi_init(); + curl1 = curl_easy_init(); + curl_multi_add_handle(curlm, curl1); + + curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + + if (current.verbose) { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + } else { + curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + } + + curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + + curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + + curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + + }; + virtual CommandLineArgs options(int n_opts, char *opts[]) = 0; virtual string getReadBuffer() { @@ -371,7 +404,6 @@ namespace mlutil { */ virtual int execute() { - CURLM *curlm; int handle_count; curlm = curl_multi_init(); curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); diff --git a/src/load.cpp b/src/load.cpp index 8bb4765..6c3f922 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -128,41 +128,17 @@ namespace mlutil { fd = fopen(file, "rb"); /* open file to upload */ - CURLM *curlm; int handle_count; - curlm = curl_multi_init(); - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + setCurlOpts(); if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl1, CURLOPT_READDATA, fd); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_multi_add_handle(curlm, curl1); CURLMcode code; while (1) { code = curl_multi_perform(curlm, &handle_count); @@ -172,7 +148,6 @@ namespace mlutil { } } } - curl_global_cleanup(); return EXIT_SUCCESS; }; } \ No newline at end of file diff --git a/src/query.cpp b/src/query.cpp index 5289d1f..e81f3a8 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -132,33 +132,11 @@ namespace mlutil { */ int Query::executeQueryPost(string type, string query) { - CURLM *curlm; int handle_count; - curlm = curl_multi_init(); - - CURL *curl1 = NULL; - curl1 = curl_easy_init(); - - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + setCurlOpts(); if (curl1) { - curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); - - curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); - curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); - - if (current.verbose) { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); - } else { - curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); - } - - curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); - curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); - curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); - - curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + //curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); @@ -170,7 +148,6 @@ namespace mlutil { curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); - curl_multi_add_handle(curlm, curl1); CURLMcode code; while (1) { code = curl_multi_perform(curlm, &handle_count); @@ -184,8 +161,6 @@ namespace mlutil { //std::cout << readBuffer << std::endl; } - curl_global_cleanup(); - return EXIT_SUCCESS; }; From ed32a404f519a1716105ba270599791238bd80f3 Mon Sep 17 00:00:00 2001 From: xquery Date: Tue, 6 Jun 2017 16:01:04 +0200 Subject: [PATCH 10/11] refactor --- src/admin.cpp | 6 +- src/command.hpp | 33 +++++++++-- src/load.cpp | 119 ++++++++++++++++++++++++++++++++++++--- src/load.hpp | 13 ++++- src/mlbrowse/main.cpp | 13 +++-- src/mlconfigure/main.cpp | 42 +++++++------- src/mlhistory/main.cpp | 31 +++++----- src/mljs/main.cpp | 23 ++++---- src/mlload/main.cpp | 33 ++++++++--- src/mllog/main.cpp | 21 +++---- src/mlstatus/main.cpp | 21 ++++--- src/mlxq/main.cpp | 23 ++++---- src/query.cpp | 8 +-- 13 files changed, 277 insertions(+), 109 deletions(-) diff --git a/src/admin.cpp b/src/admin.cpp index 6886a6d..ba7258b 100755 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -277,7 +277,7 @@ namespace mlutil { } else { curl_slist_append(headers, "Content-Type: application/json"); } - setCurlOpts(); + initCurl(); if (curl1) { @@ -330,14 +330,12 @@ namespace mlutil { headers = curl_slist_append(headers, "Content-Type: application/json"); } - setCurlOpts(); + initCurl(); if (curl1) { curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, body.c_str()); curl_easy_setopt(curl1, CURLOPT_POST, 1L); - curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_multi_add_handle(curlm, curl1); CURLMcode code; while (1) { diff --git a/src/command.hpp b/src/command.hpp index b1b57a5..7d987e1 100755 --- a/src/command.hpp +++ b/src/command.hpp @@ -123,7 +123,10 @@ namespace mlutil { string readBuffer; CURLM *curlm; CURL *curl1 = NULL; + CURL *curl2 = NULL; string url; + CURLM *cm; + CURLMsg *msg; public: @@ -131,6 +134,7 @@ namespace mlutil { * */ Command() { + LOG_S(INFO) << "command constructor called"; // set defaults loadConfig(config, current.config); @@ -159,44 +163,65 @@ namespace mlutil { config.mllog = current.cmllog; } - setCurlOpts(); + initCurl(); }; /*! ~Command * */ virtual ~Command() { + LOG_S(INFO) << "command destructor called"; + curl_easy_cleanup(curl1); + curl_easy_cleanup(curl2); + curl_multi_cleanup(curlm); + curl_multi_cleanup(cm); curl_global_cleanup(); - //std::cout << "Default destructor called\n"; }; virtual int usage(const char *progname) =0; - virtual void setCurlOpts(){ + virtual void initCurl(){ + + curl_global_init(CURL_GLOBAL_ALL); + setheaders(); + cm = curl_multi_init(); + curlm = curl_multi_init(); curl1 = curl_easy_init(); + curl2 = curl_easy_init(); curl_multi_add_handle(curlm, curl1); - curl_multi_setopt(curlm, CURLMOPT_PIPELINING, 0L); + curl_multi_setopt(cm, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); + curl_easy_setopt(curl1, CURLOPT_USERNAME, config.user.c_str()); curl_easy_setopt(curl1, CURLOPT_PASSWORD, config.pass.c_str()); + curl_easy_setopt(curl2, CURLOPT_USERNAME, config.user.c_str()); + curl_easy_setopt(curl2, CURLOPT_PASSWORD, config.pass.c_str()); if (current.verbose) { curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(curl2, CURLOPT_VERBOSE, 1L); } else { curl_easy_setopt(curl1, CURLOPT_VERBOSE, 0L); + curl_easy_setopt(curl2, CURLOPT_VERBOSE, 0L); } curl_easy_setopt(curl1, CURLOPT_USERAGENT, "ml-utils via curl"); curl_easy_setopt(curl1, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl2, CURLOPT_USERAGENT, "ml-utils via curl"); + curl_easy_setopt(curl2, CURLOPT_FAILONERROR, 1); curl_easy_setopt(curl1, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_easy_setopt(curl1, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl2, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl2, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl1, CURLOPT_DEBUGFUNCTION, log_trace); + curl_easy_setopt(curl2, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl2, CURLOPT_DEBUGFUNCTION, log_trace); }; diff --git a/src/load.cpp b/src/load.cpp index 6c3f922..efea4a9 100755 --- a/src/load.cpp +++ b/src/load.cpp @@ -23,21 +23,27 @@ #include #include #include - +#include +#include +#include #include "load.hpp" +#define MAX 20 + namespace mlutil { /* Load::Load * */ Load::Load() {//std::cout << "load constructor called\n"; + LOG_S(INFO) << "load constructor called"; }; /* Load::~Load * */ Load::~Load() {//std::cout << "load destructor called\n"; + LOG_S(INFO) << "load destructor called"; }; /*! Load::options @@ -122,18 +128,15 @@ namespace mlutil { * @param body * @return */ - int Load::executeLoadPost(const char *file, string body) { + int Load::executeLoadPost(string filename,string uri) { FILE *fd; - - fd = fopen(file, "rb"); /* open file to upload */ + fd = fopen(filename.c_str(), "rb"); + string url = "http://node1:8000/v1/documents?uri="+uri; int handle_count; - setCurlOpts(); - if (curl1) { - curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl1, CURLOPT_READDATA, fd); @@ -147,7 +150,109 @@ namespace mlutil { break; } } + + } + return EXIT_SUCCESS; + }; + + void* Load::postOneFile(CURLM *cm, int i, vector *v) + { + CURL *eh = curl_easy_init(); //curl_easy_duphandle(curl1); + FILE *fd; + fd = fopen(v->at(i).c_str(), "rb"); + string url = "http://node1:8000/v1/documents?uri="+v->at(i); + + curl_easy_setopt(eh, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(eh, CURLOPT_USERNAME, "admin"); + curl_easy_setopt(eh, CURLOPT_PASSWORD, "admin"); + + curl_easy_setopt(eh, CURLOPT_URL, url.c_str()); + curl_easy_setopt(eh, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(eh, CURLOPT_READDATA, fd); + curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 1L); + curl_multi_add_handle(cm, eh); + } + + int Load::executeLoad(string dirname) { + struct dirent *ent; + vector v; + + DIR *dir = getDir(dirname.c_str()); + + while ((ent = readdir (dir)) != NULL) { + string filename = ent->d_name; + v.push_back(dirname +"/"+ filename); + } + v.erase(v.begin()); + v.erase(v.begin() + 1); + closedir (dir); + + cout << "uploading #" << v.size() << "documents: " << endl; + + long L; + unsigned int C=0; + int M, Q, U = -1; + fd_set R, W, E; + struct timeval T; + + //curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX); + //curl_multi_setopt(cm, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L); + + // setup + for(C = 0; C < v.size(); ++C) { + postOneFile(cm, C, &v); + } + + while(U) { + curl_multi_perform(cm, &U); + if(U) { + FD_ZERO(&R); + FD_ZERO(&W); + FD_ZERO(&E); + + if(curl_multi_fdset(cm, &R, &W, &E, &M)) { + fprintf(stderr, "E: curl_multi_fdset\n"); + return EXIT_FAILURE; + } + + if(curl_multi_timeout(cm, &L)) { + fprintf(stderr, "E: curl_multi_timeout\n"); + return EXIT_FAILURE; + } + if(L == -1) + L = 100; + + if(M == -1) { + sleep((unsigned int)L / 1000); + } + else { + T.tv_sec = L/1000; + T.tv_usec = (L%1000)*1000; + + if(0 > select(M+1, &R, &W, &E, &T)) { + fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n", + M+1, L, errno, strerror(errno)); + return EXIT_FAILURE; + } + } + } + + while((msg = curl_multi_info_read(cm, &Q))) { + if(msg->msg == CURLMSG_DONE) { + char *url; + CURL *e = msg->easy_handle; + curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); + fprintf(stderr, "R: %d - %s <%s>\n", + msg->data.result, curl_easy_strerror(msg->data.result), url); + curl_multi_remove_handle(cm, e); + curl_easy_cleanup(e); + } + else { + fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); + } + } } + return EXIT_SUCCESS; }; } \ No newline at end of file diff --git a/src/load.hpp b/src/load.hpp index 1892ad6..8c5fde0 100755 --- a/src/load.hpp +++ b/src/load.hpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "command.hpp" @@ -39,6 +40,12 @@ using namespace std; namespace mlutil { + DIR *getDir(string dirname){ + DIR *dir; + dir=opendir (dirname.c_str()); + return dir; + }; + class Load : public Command { public: Load(); @@ -51,6 +58,10 @@ namespace mlutil { virtual void setLoadUrl(string port, string root, string uri); - virtual int executeLoadPost(const char *file, string body); + virtual int executeLoadPost(string dirname,string uri); + + virtual int executeLoad(string dirname); + + void* postOneFile(CURLM *cm, int i, vector *v); }; } diff --git a/src/mlbrowse/main.cpp b/src/mlbrowse/main.cpp index 436a304..cbb87c7 100755 --- a/src/mlbrowse/main.cpp +++ b/src/mlbrowse/main.cpp @@ -34,15 +34,16 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Browse browse; - browse.setCurrentArgs(browse.options(argc, argv)); - CommandLineArgs current = browse.getCurrentArgs(); - Config config = browse.getConfig(); + Browse *pBrowse= new Browse(); + pBrowse->setCurrentArgs(pBrowse->options(argc, argv)); + CommandLineArgs current = pBrowse->getCurrentArgs(); + Config config = pBrowse->getConfig(); if (current.verbose) { - browse.displayargs(); - browse.displayconfig(); + pBrowse->displayargs(); + pBrowse->displayconfig(); } cout << "not implemented yet" << endl; + delete pBrowse; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-browse."; return EXIT_FAILURE; diff --git a/src/mlconfigure/main.cpp b/src/mlconfigure/main.cpp index cd5c765..5b55c1c 100755 --- a/src/mlconfigure/main.cpp +++ b/src/mlconfigure/main.cpp @@ -35,27 +35,28 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Admin admin; - admin.setCurrentArgs(admin.options(argc, argv)); - CommandLineArgs current = admin.getCurrentArgs(); - Config config = admin.getConfig(); + Admin *pAdmin = new Admin(); + pAdmin->setCurrentArgs(pAdmin->options(argc, argv)); + CommandLineArgs current = pAdmin->getCurrentArgs(); + Config config = pAdmin->getConfig(); if (current.verbose) { - admin.displayargs(); - admin.displayconfig(); + pAdmin->displayargs(); + pAdmin->displayconfig(); } string command = current.command; string resource = current.resource; string name = current.name; + LOG_S(INFO) << "perform " << command; if (!command.empty()) { if (command == "restart") { - admin.setResourceUrl(config.port, config.path, ""); - admin.executeResourcePost("{\"operation\":\"restart-local-cluster\"}", ""); + pAdmin->setResourceUrl(config.port, config.path, ""); + pAdmin->executeResourcePost("{\"operation\":\"restart-local-cluster\"}", ""); } else if (command == "get") { - admin.setUrl(config.port, config.path, current.resource, "default"); - admin.execute(); + pAdmin->setUrl(config.port, config.path, current.resource, "default"); + pAdmin->execute(); } else if (command == "get-properties") { - admin.setUrl(config.port, config.path, resource + "/properties", "default"); - admin.execute(); + pAdmin->setUrl(config.port, config.path, resource + "/properties", "default"); + pAdmin->execute(); } else if (command == "create") { string line, body; if (name.empty()) { @@ -63,7 +64,7 @@ int main(int argc, char *argv[]) { body.append(line); } } - admin.setResourceUrl(config.port, config.path, resource); + pAdmin->setResourceUrl(config.port, config.path, resource); if (body.empty()) { string payload; if (resource == "forests") { @@ -75,30 +76,33 @@ int main(int argc, char *argv[]) { } else if (resource == "groups") { payload = "{\"group-name\":\"" + name + "\"}"; } - admin.executeResourcePost(payload, resource); + pAdmin->executeResourcePost(payload, resource); } else { - admin.executeResourcePost(body, resource); + pAdmin->executeResourcePost(body, resource); } } else if (command == "update") { + LOG_S(INFO) << "perform update"; string line, body; while (getline(std::cin, line)) { body.append(line); } - admin.setResourceUrl(config.port, config.path, resource + "/properties"); - admin.executeResourcePut(body, resource); + pAdmin->setResourceUrl(config.port, config.path, resource + "/properties"); + pAdmin->executeResourcePut(body, resource); } else if (command == "install" || command == "reinstall") { string path = config.mlconfig; cout << path << endl; - admin.walkInstallConfig(admin, config, path); + pAdmin->walkInstallConfig(*pAdmin, config, path); } else { LOG_S(ERROR) << "invalid command."; return EXIT_FAILURE; } - cout << admin.getReadBuffer() << endl; + cout << pAdmin->getReadBuffer() << endl; } else { LOG_S(ERROR) << "no command supplied."; return EXIT_FAILURE; } + + delete pAdmin; } catch (std::bad_alloc) { LOG_S(ERROR) << "problem with ml-config."; return EXIT_FAILURE; diff --git a/src/mlhistory/main.cpp b/src/mlhistory/main.cpp index b957be3..b1cf6ff 100755 --- a/src/mlhistory/main.cpp +++ b/src/mlhistory/main.cpp @@ -39,19 +39,19 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - History history; - history.setCurrentArgs(history.options(argc, argv)); - CommandLineArgs current = history.getCurrentArgs(); - Config config = history.getConfig(); + History *pHistory = new History(); + pHistory->setCurrentArgs(pHistory->options(argc, argv)); + CommandLineArgs current = pHistory->getCurrentArgs(); + Config config = pHistory->getConfig(); if (current.verbose) { - history.displayargs(); - history.displayconfig(); + pHistory->displayargs(); + pHistory->displayconfig(); } - history.setUrl(config.port, config.path, current.resource, "metrics"); - history.execute(); + pHistory->setUrl(config.port, config.path, current.resource, "metrics"); + pHistory->execute(); - string result = history.getReadBuffer(); + string result = pHistory->getReadBuffer(); string graph_prefix = "graph"; string output = current.output; @@ -71,30 +71,32 @@ int main(int argc, char *argv[]) { units = doc[res.c_str()]["metrics-relations"]["database-metrics-list"]["metrics"][0]["master"][0][current.metric]["units"].GetString(); name = doc[res.c_str()]["metrics-relations"]["database-metrics-list"]["metrics"][0]["master"][0][current.metric]["name"].GetString(); const Value &entries = doc[res.c_str()]["metrics-relations"]["database-metrics-list"]["metrics"][0]["master"][0][current.metric]["summary"]["data"]["entry"]; - history.doPlot(output, current.gnuplot, units, name, string(current.resource), entries); + pHistory->doPlot(output, current.gnuplot, units, name, string(current.resource), entries); } else if (resource.find(string("servers")) != std::string::npos) { res = "server-metrics-list"; units = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["units"].GetString(); name = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["name"].GetString(); const Value &entries = doc[res.c_str()]["metrics-relations"]["server-metrics-list"]["metrics"][0][current.metric]["summary"]["data"]["entry"]; - history.doPlot(output, current.gnuplot, units, name, string(current.resource), entries); + pHistory->doPlot(output, current.gnuplot, units, name, string(current.resource), entries); } else if (resource.find(string("forests")) != std::string::npos) { res = "forest-metrics-list"; units = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["units"].GetString(); name = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["name"].GetString(); const Value &entries = doc[res.c_str()]["metrics-relations"]["forest-metrics-list"]["metrics"][0][current.metric]["summary"]["data"]["entry"]; - history.doPlot(output, current.gnuplot, units, name, string(current.resource), entries); + pHistory->doPlot(output, current.gnuplot, units, name, string(current.resource), entries); } else if (resource.find(string("hosts")) != std::string::npos) { res = "host-metrics-list"; units = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["units"].GetString(); name = doc[res.c_str()]["metrics-relations"][res.c_str()]["metrics"][0][current.metric]["name"].GetString(); const Value &entries = doc[res.c_str()]["metrics-relations"]["host-metrics-list"]["metrics"][0][current.metric]["summary"]["data"]["entry"]; - history.doPlot(output, current.gnuplot, units, name, string(current.resource), entries); + pHistory->doPlot(output, current.gnuplot, units, name, string(current.resource), entries); } } if (!current.quiet) { if (strcmp(current.output, "csv") == 0) { + LOG_S(INFO) << "output csv "; + Document doc; doc.Parse(result.c_str()); string metricslist = "-metrics-list"; @@ -115,11 +117,14 @@ int main(int argc, char *argv[]) { cout << entry["value"].GetDouble() << endl; } } else if (strcmp(current.format, "json") == 0) { + LOG_S(INFO) << "output json"; cout << result << endl; } else { + LOG_S(INFO) << "output xml"; cout << result << endl; } } + delete pHistory; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-hist."; return EXIT_FAILURE; diff --git a/src/mljs/main.cpp b/src/mljs/main.cpp index 875df5f..ffe4fcd 100755 --- a/src/mljs/main.cpp +++ b/src/mljs/main.cpp @@ -35,29 +35,29 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Query query; - query.setCurrentArgs(query.options(argc, argv)); - CommandLineArgs current = query.getCurrentArgs(); - Config config = query.getConfig(); + Query *pQuery = new Query(); + pQuery->setCurrentArgs(pQuery->options(argc, argv)); + CommandLineArgs current = pQuery->getCurrentArgs(); + Config config = pQuery->getConfig(); if (current.verbose) { - query.displayargs(); - query.displayconfig(); + pQuery->displayargs(); + pQuery->displayconfig(); } - query.setQueryUrl(config.port, "/v1/eval", "", ""); + pQuery->setQueryUrl(config.port, "/v1/eval", "", ""); if (strcmp(current.js, "") == 0) { std::string line, qry; while (std::getline(std::cin, line)) { qry.append(line); } - query.executeQueryPost("javascript", qry); + pQuery->executeQueryPost("javascript", qry); } else { - query.executeQueryPost("javascript", current.js); + pQuery->executeQueryPost("javascript", current.js); } if (!current.quiet) { - string result = query.getReadBuffer(); + string result = pQuery->getReadBuffer(); // count number of lines std::stringstream cc(result); std::string un; @@ -67,6 +67,7 @@ int main(int argc, char *argv[]) { } if (strcmp(current.format, "json") == 0) { + LOG_S(INFO) << "output json"; std::stringstream ss(result); std::string to; if (!result.empty()) { @@ -79,6 +80,7 @@ int main(int argc, char *argv[]) { } } } else { + LOG_S(INFO) << "output multipart"; std::stringstream ss(result); std::string to; if (!result.empty()) { @@ -92,6 +94,7 @@ int main(int argc, char *argv[]) { } } } + delete pQuery; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-js."; return EXIT_FAILURE; diff --git a/src/mlload/main.cpp b/src/mlload/main.cpp index a2e559d..fbdab50 100755 --- a/src/mlload/main.cpp +++ b/src/mlload/main.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "../load.cpp" @@ -35,22 +36,36 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Load load; - load.setCurrentArgs(load.options(argc, argv)); - CommandLineArgs current = load.getCurrentArgs(); - Config config = load.getConfig(); + Load *pLoad = new Load(); + pLoad->setCurrentArgs(pLoad->options(argc, argv)); + CommandLineArgs current = pLoad->getCurrentArgs(); + Config config = pLoad->getConfig(); if (current.verbose) { - load.displayargs(); - load.displayconfig(); + pLoad->displayargs(); + pLoad->displayconfig(); } - load.setLoadUrl(config.port, "/v1/documents", current.uri); - std::string line, body; + pLoad->setLoadUrl(config.port, "/v1/documents", current.uri); if (!current.filename) { + LOG_S(INFO) << "load from stdin single file: " << current.filename ; + std::string line, body; while (std::getline(std::cin, line)) { body.append(line); } + }else{ + struct stat buffer; + string name = current.filename; + string uri = current.uri; + if (stat (current.filename, &buffer) == 0){ + pLoad->initCurl(); + LOG_S(INFO) << "load file: " << current.filename; + pLoad->executeLoadPost(name,uri); + }else{ + LOG_S(INFO) << "load directory: " << current.filename; + pLoad->executeLoad(name); + } } - load.executeLoadPost(current.filename, body); + delete pLoad; + } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-load."; return EXIT_FAILURE; diff --git a/src/mllog/main.cpp b/src/mllog/main.cpp index ad6123f..b63e1f7 100755 --- a/src/mllog/main.cpp +++ b/src/mllog/main.cpp @@ -35,24 +35,25 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Log log; - log.setCurrentArgs(log.options(argc, argv)); - CommandLineArgs current = log.getCurrentArgs(); - Config config = log.getConfig(); + Log *pLog = new Log(); + pLog->setCurrentArgs(pLog->options(argc, argv)); + CommandLineArgs current = pLog->getCurrentArgs(); + Config config = pLog->getConfig(); if (current.verbose) { - log.displayargs(); - log.displayconfig(); + pLog->displayargs(); + pLog->displayconfig(); } - log.setUrl(config.port, config.path + "/logs", "", ""); - log.setLogUrl(config.port, config.path + "/logs", current.name, current.start, current.end, current.regex, + pLog->setUrl(config.port, config.path + "/logs", "", ""); + pLog->setLogUrl(config.port, config.path + "/logs", current.name, current.start, current.end, current.regex, current.host, "text"); - log.execute(); + pLog->execute(); if (!current.quiet) { - cout << log.getReadBuffer() << endl; + cout << pLog->getReadBuffer() << endl; } + delete pLog; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-log."; return EXIT_FAILURE; diff --git a/src/mlstatus/main.cpp b/src/mlstatus/main.cpp index 6226ed7..88cdd27 100755 --- a/src/mlstatus/main.cpp +++ b/src/mlstatus/main.cpp @@ -38,27 +38,30 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Status status; - status.setCurrentArgs(status.options(argc, argv)); - CommandLineArgs current = status.getCurrentArgs(); - Config config = status.getConfig(); + Status *pStatus = new Status(); + pStatus->setCurrentArgs(pStatus->options(argc, argv)); + CommandLineArgs current = pStatus->getCurrentArgs(); + Config config = pStatus->getConfig(); if (current.verbose) { - status.displayargs(); - status.displayconfig(); + pStatus->displayargs(); + pStatus->displayconfig(); } - status.setUrl(config.port, config.path, current.resource, "status"); - status.execute(); + pStatus->setUrl(config.port, config.path, current.resource, "pStatus"); + pStatus->execute(); if (!current.quiet) { - string result = status.getReadBuffer(); + string result = pStatus->getReadBuffer(); if (strcmp(current.format, "json") == 0) { + LOG_S(INFO) << "output json"; cout << result << endl; } else { + LOG_S(INFO) << "output xml"; cout << result << endl; } } + delete pStatus; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-status."; return EXIT_FAILURE; diff --git a/src/mlxq/main.cpp b/src/mlxq/main.cpp index 8aefc11..a5a576c 100755 --- a/src/mlxq/main.cpp +++ b/src/mlxq/main.cpp @@ -35,27 +35,27 @@ using namespace mlutil; int main(int argc, char *argv[]) { try { - Query query; - query.setCurrentArgs(query.options(argc, argv)); - CommandLineArgs current = query.getCurrentArgs(); - Config config = query.getConfig(); + Query *pQuery = new Query(); + pQuery->setCurrentArgs(pQuery->options(argc, argv)); + CommandLineArgs current = pQuery->getCurrentArgs(); + Config config = pQuery->getConfig(); if (current.verbose) { - query.displayargs(); - query.displayconfig(); + pQuery->displayargs(); + pQuery->displayconfig(); } - query.setQueryUrl(config.port, "/v1/eval", "", ""); + pQuery->setQueryUrl(config.port, "/v1/eval", "", ""); if (strcmp(current.xquery, "") == 0) { std::string line, qry; while (std::getline(std::cin, line)) { qry.append(line); } - query.executeQueryPost("xquery", qry); + pQuery->executeQueryPost("xquery", qry); } else { - query.executeQueryPost("xquery", current.xquery); + pQuery->executeQueryPost("xquery", current.xquery); } - string result = query.getReadBuffer(); + string result = pQuery->getReadBuffer(); // count number of lines std::stringstream cc(result); @@ -83,8 +83,10 @@ int main(int argc, char *argv[]) { } } else { if (current.raw) { + LOG_S(INFO) << "normal output"; cout << result << endl; } else { + LOG_S(INFO) << "output multipart"; std::stringstream ss(result); std::string to; if (!result.empty()) { @@ -98,6 +100,7 @@ int main(int argc, char *argv[]) { } } } + delete pQuery; } catch (std::bad_alloc) { LOG_S(ERROR) << "Error with ml-xq."; return EXIT_FAILURE; diff --git a/src/query.cpp b/src/query.cpp index e81f3a8..6a2770f 100755 --- a/src/query.cpp +++ b/src/query.cpp @@ -133,18 +133,15 @@ namespace mlutil { int Query::executeQueryPost(string type, string query) { int handle_count; - setCurlOpts(); + initCurl(); if (curl1) { - //curl_easy_setopt(curl1, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl1, CURLOPT_URL, url.c_str()); - char *data = curl_easy_escape(curl1, query.c_str(), strlen(query.c_str())); string xquery = type + "=" + data; curl_easy_setopt(curl1, CURLOPT_POST, 1); curl_easy_setopt(curl1, CURLOPT_POSTFIELDS, xquery.c_str()); curl_easy_setopt(curl1, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl1, CURLOPT_WRITEDATA, &readBuffer); @@ -160,9 +157,6 @@ namespace mlutil { } //std::cout << readBuffer << std::endl; } - return EXIT_SUCCESS; - }; - } \ No newline at end of file From 5fef291aa47c51a7c20c8e52359a5f34c835247f Mon Sep 17 00:00:00 2001 From: xquery Date: Tue, 6 Jun 2017 16:10:20 +0200 Subject: [PATCH 11/11] refactor --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 8294c18..341cf11 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.2 \ No newline at end of file +0.2.0 \ No newline at end of file