Elements 6.3.3
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
ConnectionConfiguration.cpp
Go to the documentation of this file.
1
18
19#include <algorithm> // for find
20#include <cstddef> // for size_t
21#include <exception> // for exception
22#include <stdexcept> // for runtime_error
23#include <string> // for basic_string, allocator, string, operator==, operator+
24#include <vector> // for vector
25
26#include <boost/filesystem/path.hpp> // for operator/, path
27#include <boost/program_options.hpp> // for value, typed_value, variables_map, options_description_easy_init, variable_value, notify, parse_config_file, store, options_description, program_options
28
29#include "ElementsServices/DataSync/ConnectionConfiguration.h" // for ConnectionConfiguration, OverwritingPolicy, DataHost, UnknownHost
30#include "ElementsServices/DataSync/DataSyncUtils.h" // for lower, valueIsListed, confFilePath, localWorkspacePrefix, path
31
32namespace Elements {
33inline namespace Services {
34namespace DataSync {
35
36using std::string;
37
41
45
47 // @TODO clean function
48
49 namespace po = boost::program_options;
50
51 /* Declare options */
52 po::options_description options{};
53 options.add_options()("host", po::value<string>(), "Hosting solution: iRODS or WebDAV (case insensitive)")(
54 "host-url", po::value<string>()->default_value(""),
55 "Host URL if needed")("user", po::value<string>()->default_value(""), "User name if needed")(
56 "password", po::value<string>()->default_value(""), "Password if needed")(
57 "overwrite", po::value<string>()->default_value("no"), "Allow overwriting local files if they already exist")(
58 "distant-workspace", po::value<string>(), "Path to distant repository workspace")(
59 "local-workspace", po::value<string>(),
60 "Path to local repository workspace")("tries", po::value<int>()->default_value(4), "Number of download tries");
61
62 /* Get config file path */
63 path abs_path = confFilePath(filename);
64
65 /* Read config file */
66 po::variables_map vm;
67 try {
68 po::store(po::parse_config_file<char>(abs_path.c_str(), options), vm);
69 po::notify(vm);
70 } catch (std::exception& e) {
71 throw e;
72 }
73
74 /* Configure object */
75 parseHost(vm["host"].as<string>());
76 hostUrl = vm["host-url"].as<string>();
77 user = vm["user"].as<string>();
78 password = vm["password"].as<string>();
79 parseOverwritingPolicy(vm["overwrite"].as<string>());
80 distantRoot = vm["distant-workspace"].as<string>();
81 localRoot = localWorkspacePrefix() / vm["local-workspace"].as<string>();
82 tries = static_cast<std::size_t>(vm["tries"].as<int>());
83}
84
85void ConnectionConfiguration::parseHost(const string& name) {
86 const string uncased = lower(name);
87 if (uncased == "irods") {
89 } else if (uncased == "webdav") {
91 } else {
92 throw UnknownHost(name);
93 }
94}
95
97
98 using std::vector;
99
100 const vector<string> overwrite_allowed_options = {"true", "yes", "y"};
101 const vector<string> overwrite_forbidden_options = {"false", "no", "n"};
102
103 string uncased = lower(policy);
104 if (valueIsListed(uncased, overwrite_allowed_options)) {
106 } else if (valueIsListed(uncased, overwrite_forbidden_options)) {
108 } else {
109 throw std::runtime_error("I don't know this overwriting policy: " + policy);
110 }
111}
112
113} // namespace DataSync
114} // namespace Services
115} // namespace Elements
ConnectionConfiguration(const path &configFile)
Create a dependency configuration by reading a configuration file.
bool overwritingAllowed() const
Check whether existing local files can be overwritten.
Exception raised when a hosting solution is not supported by the tool.
ELEMENTS_API path confFilePath(path filename)
ELEMENTS_API path localWorkspacePrefix()
ELEMENTS_API std::string lower(std::string text)
Path::Item path
importing the path item from ElementsKernel
ELEMENTS_API bool valueIsListed(const T &value, const std::vector< T > &list)