From a7555e0bf39ded8d7c88870b78c11b08b977584f Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 30 Apr 2018 18:33:42 +0200 Subject: [PATCH] make arangovpack work without any config file specified (#5224) --- CHANGELOG | 3 ++ arangosh/VPack/VPackFeature.cpp | 45 ++++++++++++++++------- arangosh/VPack/VPackFeature.h | 1 + arangosh/VPack/arangovpack.cpp | 3 +- lib/ApplicationFeatures/ConfigFeature.cpp | 5 ++- lib/ApplicationFeatures/ConfigFeature.h | 3 +- lib/Basics/FileUtils.cpp | 2 +- 7 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 35ae2d2ff7..c2c28035e3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ devel ----- +* added `--json` option to arangovpack, allowing to treat its input as plain JSON data + make arangovpack work without any configuration file + * added experimental arangodb startup option `--javascript.enabled` to enable/disable the initialization of the V8 JavaScript engine. Only expected to work on single-servers and agency deployments diff --git a/arangosh/VPack/VPackFeature.cpp b/arangosh/VPack/VPackFeature.cpp index 9384badf9d..bbe97e1907 100644 --- a/arangosh/VPack/VPackFeature.cpp +++ b/arangosh/VPack/VPackFeature.cpp @@ -113,6 +113,7 @@ VPackFeature::VPackFeature(application_features::ApplicationServer* server, : ApplicationFeature(server, "VPack"), _result(result), _prettyPrint(true), + _jsonInput(false), _hexInput(false), _printNonJson(true) { requiresElevatedPrivileges(false); @@ -133,6 +134,9 @@ void VPackFeature::collectOptions( options->addOption( "--hex", "read hex-encoded input", new BooleanParameter(&_hexInput)); + options->addOption( + "--json", "treat input as JSON", + new BooleanParameter(&_jsonInput)); options->addOption( "--print-non-json", "print non-JSON types", new BooleanParameter(&_printNonJson)); @@ -169,17 +173,32 @@ void VPackFeature::start() { (_printNonJson ? VPackOptions::ConvertUnsupportedType : VPackOptions::FailOnUnsupportedType); options.customTypeHandler = &customTypeHandler; - try { - VPackValidator validator(&options); - validator.validate(s.c_str(), s.size(), false); - } catch (std::exception const& ex) { - std::cerr << "Invalid VPack input while processing infile '" << _inputFile - << "': " << ex.what() << std::endl; - *_result = TRI_ERROR_INTERNAL; - return; - } + VPackSlice slice; + std::shared_ptr builder; - VPackSlice const slice(s.c_str()); + if (_jsonInput) { + try { + builder = VPackParser::fromJson(s); + slice = builder->slice(); + } catch (std::exception const& ex) { + std::cerr << "Invalid JSON input while processing infile '" << _inputFile + << "': " << ex.what() << std::endl; + *_result = TRI_ERROR_INTERNAL; + return; + } + } else { + try { + VPackValidator validator(&options); + validator.validate(s.c_str(), s.size(), false); + } catch (std::exception const& ex) { + std::cerr << "Invalid VPack input while processing infile '" << _inputFile + << "': " << ex.what() << std::endl; + *_result = TRI_ERROR_INTERNAL; + return; + } + + slice = VPackSlice(s.data()); + } VPackBuffer buffer(4096); VPackCharBufferSink sink(&buffer); @@ -220,10 +239,10 @@ void VPackFeature::start() { // cppcheck-suppress * if (!toStdOut) { - std::cout << "Successfully converted JSON infile '" << _inputFile << "'" + std::cout << "Successfully processed infile '" << _inputFile << "'" << std::endl; - std::cout << "VPack Infile size: " << s.size() << std::endl; - std::cout << "JSON Outfile size: " << buffer.size() << std::endl; + std::cout << "Infile size: " << s.size() << std::endl; + std::cout << "Outfile size: " << buffer.size() << std::endl; } *_result = TRI_ERROR_NO_ERROR; diff --git a/arangosh/VPack/VPackFeature.h b/arangosh/VPack/VPackFeature.h index 4516d659ce..89a295a170 100644 --- a/arangosh/VPack/VPackFeature.h +++ b/arangosh/VPack/VPackFeature.h @@ -44,6 +44,7 @@ class VPackFeature final std::string _inputFile; std::string _outputFile; bool _prettyPrint; + bool _jsonInput; bool _hexInput; bool _printNonJson; }; diff --git a/arangosh/VPack/arangovpack.cpp b/arangosh/VPack/arangovpack.cpp index c54801f9f3..fc08ee277a 100644 --- a/arangosh/VPack/arangovpack.cpp +++ b/arangosh/VPack/arangovpack.cpp @@ -50,7 +50,8 @@ int main(int argc, char* argv[]) { int ret; - server.addFeature(new ConfigFeature(&server, "arangovpack")); + // default is to use no config file + server.addFeature(new ConfigFeature(&server, "arangovpack", "none")); server.addFeature(new LoggerFeature(&server, false)); server.addFeature(new RandomFeature(&server)); server.addFeature(new ShellColorsFeature(&server)); diff --git a/lib/ApplicationFeatures/ConfigFeature.cpp b/lib/ApplicationFeatures/ConfigFeature.cpp index 5b7915d671..bf141746b0 100644 --- a/lib/ApplicationFeatures/ConfigFeature.cpp +++ b/lib/ApplicationFeatures/ConfigFeature.cpp @@ -41,9 +41,10 @@ using namespace arangodb::rest; using namespace arangodb::options; ConfigFeature::ConfigFeature(application_features::ApplicationServer* server, - std::string const& progname) + std::string const& progname, + std::string const& configFilename) : ApplicationFeature(server, "Config"), - _file(""), + _file(configFilename), _checkConfiguration(false), _progname(progname) { setOptional(false); diff --git a/lib/ApplicationFeatures/ConfigFeature.h b/lib/ApplicationFeatures/ConfigFeature.h index cdb1854408..fd8a915fcf 100644 --- a/lib/ApplicationFeatures/ConfigFeature.h +++ b/lib/ApplicationFeatures/ConfigFeature.h @@ -29,7 +29,8 @@ namespace arangodb { class ConfigFeature final : public application_features::ApplicationFeature { public: ConfigFeature(application_features::ApplicationServer* server, - std::string const& progname); + std::string const& progname, + std::string const& configFilename = ""); public: void collectOptions(std::shared_ptr) override final; diff --git a/lib/Basics/FileUtils.cpp b/lib/Basics/FileUtils.cpp index 8782a485f7..0972da987c 100644 --- a/lib/Basics/FileUtils.cpp +++ b/lib/Basics/FileUtils.cpp @@ -130,7 +130,7 @@ static void throwFileReadError(std::string const& filename) { strerror(res)); LOG_TOPIC(TRACE, arangodb::Logger::FIXME) << message; - THROW_ARANGO_EXCEPTION(TRI_ERROR_SYS_ERROR); + THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_SYS_ERROR, message); } static void fillStringBuffer(int fd, std::string const& filename,