Elements  6.2
A C++ base framework for the Euclid Software.
ConnectionConfiguration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <boost/program_options.hpp>
20 #include <string>
21 #include <vector>
22 
25 
26 namespace Elements {
27 inline namespace Services {
28 namespace DataSync {
29 
30 using std::string;
31 
33  parseConfigurationFile(filename);
34 }
35 
38 }
39 
41  // @TODO clean function
42 
43  namespace po = boost::program_options;
44 
45  /* Declare options */
46  po::options_description options{};
47  options.add_options()("host", po::value<string>(), "Hosting solution: iRODS or WebDAV (case insensitive)")(
48  "host-url", po::value<string>()->default_value(""),
49  "Host URL if needed")("user", po::value<string>()->default_value(""), "User name if needed")(
50  "password", po::value<string>()->default_value(""), "Password if needed")(
51  "overwrite", po::value<string>()->default_value("no"), "Allow overwriting local files if they already exist")(
52  "distant-workspace", po::value<string>(), "Path to distant repository workspace")(
53  "local-workspace", po::value<string>(),
54  "Path to local repository workspace")("tries", po::value<int>()->default_value(4), "Number of download tries");
55 
56  /* Get config file path */
57  path abs_path = confFilePath(filename);
58 
59  /* Read config file */
60  po::variables_map vm;
61  try {
62  po::store(po::parse_config_file<char>(abs_path.c_str(), options), vm);
63  po::notify(vm);
64  } catch (std::exception& e) {
65  throw e.what();
66  }
67 
68  /* Configure object */
69  parseHost(vm["host"].as<string>());
70  hostUrl = vm["host-url"].as<string>();
71  user = vm["user"].as<string>();
72  password = vm["password"].as<string>();
73  parseOverwritingPolicy(vm["overwrite"].as<string>());
74  distantRoot = vm["distant-workspace"].as<string>();
75  localRoot = localWorkspacePrefix() / vm["local-workspace"].as<string>();
76  tries = static_cast<size_t>(vm["tries"].as<int>());
77 }
78 
79 void ConnectionConfiguration::parseHost(const string& name) {
80  const string uncased = lower(name);
81  if (uncased == "irods") {
83  } else if (uncased == "webdav") {
85  } else {
86  throw UnknownHost(name);
87  }
88 }
89 
91 
92  using std::vector;
93 
94  const vector<string> overwrite_allowed_options = {"true", "yes", "y"};
95  const vector<string> overwrite_forbidden_options = {"false", "no", "n"};
96 
97  string uncased = lower(policy);
98  if (valueIsListed(uncased, overwrite_allowed_options)) {
100  } else if (valueIsListed(uncased, overwrite_forbidden_options)) {
102  } else {
103  throw std::runtime_error("I don't know this overwriting policy: " + policy);
104  }
105 }
106 
107 } // namespace DataSync
108 } // namespace Services
109 } // namespace Elements
Elements::Services::DataSync::WEBDAV
@ WEBDAV
Definition: ConnectionConfiguration.h:43
std::string
STL class.
Elements::Services::DataSync::ConnectionConfiguration::password
std::string password
Definition: ConnectionConfiguration.h:101
std::exception
STL class.
Elements::Services::DataSync::valueIsListed
ELEMENTS_API bool valueIsListed(const T &value, const std::vector< T > &list)
Definition: DataSyncUtils.h:65
std::vector
STL class.
Elements::Services::DataSync::ConnectionConfiguration::tries
size_t tries
Definition: ConnectionConfiguration.h:103
Elements::Services::DataSync::ABORT
@ ABORT
Definition: ConnectionConfiguration.h:50
Elements::Services::DataSync::ConnectionConfiguration::localRoot
path localRoot
Definition: ConnectionConfiguration.h:105
Elements::Services::DataSync::ConnectionConfiguration::user
std::string user
Definition: ConnectionConfiguration.h:100
Elements::Services::DataSync::lower
ELEMENTS_API std::string lower(std::string text)
Definition: DataSyncUtils.cpp:88
Elements::Services::DataSync::OVERWRITE
@ OVERWRITE
Definition: ConnectionConfiguration.h:51
Elements::Services::DataSync::ConnectionConfiguration::overwritingPolicy
OverwritingPolicy overwritingPolicy
Definition: ConnectionConfiguration.h:102
std::runtime_error
STL class.
Elements::Services::DataSync::ConnectionConfiguration::overwritingAllowed
bool overwritingAllowed() const
Check whether existing local files can be overwritten.
Definition: ConnectionConfiguration.cpp:36
Elements::Services::DataSync::ConnectionConfiguration::parseHost
void parseHost(const std::string &name)
Definition: ConnectionConfiguration.cpp:79
Elements::Services::DataSync::ConnectionConfiguration::parseOverwritingPolicy
void parseOverwritingPolicy(const std::string &policy)
Definition: ConnectionConfiguration.cpp:90
Elements::Services::DataSync::localWorkspacePrefix
ELEMENTS_API path localWorkspacePrefix()
Definition: DataSyncUtils.cpp:82
DataSyncUtils.h
Elements::Services::DataSync::ConnectionConfiguration::distantRoot
path distantRoot
Definition: ConnectionConfiguration.h:104
Elements::Services::DataSync::ConnectionConfiguration::parseConfigurationFile
void parseConfigurationFile(const path &filename)
Definition: ConnectionConfiguration.cpp:40
Elements::Services::DataSync::ConnectionConfiguration::hostUrl
std::string hostUrl
Definition: ConnectionConfiguration.h:99
Elements::Services::DataSync::ConnectionConfiguration::host
DataHost host
Definition: ConnectionConfiguration.h:98
Elements::Services::DataSync::IRODS
@ IRODS
Definition: ConnectionConfiguration.h:42
Elements::Services::DataSync::ConnectionConfiguration::ConnectionConfiguration
ConnectionConfiguration(const path &configFile)
Create a dependency configuration by reading a configuration file.
Definition: ConnectionConfiguration.cpp:32
Elements::Services::DataSync::confFilePath
ELEMENTS_API path confFilePath(path filename)
Definition: DataSyncUtils.cpp:39
Elements::Kernel::Units::e
constexpr double e
The base of the natural logarithm .
Definition: MathConstants.h:51
Elements::Services::DataSync::UnknownHost
Exception raised when a hosting solution is not supported by the tool.
Definition: ConnectionConfiguration.h:59
ConnectionConfiguration.h
Elements
Definition: callBackExample.h:35
Elements::Services::DataSync::path
Path::Item path
importing the path item from ElementsKernel
Definition: DataSyncUtils.h:41