Elements  5.12.0
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 <string>
20 #include <vector>
21 #include <boost/program_options.hpp>
22 
25 
26 namespace ElementsServices {
27 namespace DataSync {
28 
29 using std::string;
30 
32  parseConfigurationFile(filename);
33 }
34 
37 }
38 
40  // @TODO clean function
41 
42  namespace po = boost::program_options;
43 
44  /* Declare options */
45  po::options_description options { };
46  options.add_options()
47  ("host", po::value<string>(),
48  "Hosting solution: iRODS or WebDAV (case insensitive)")
49  ("host-url", po::value<string>()->default_value(""),
50  "Host URL if needed")
51  ("user", po::value<string>()->default_value(""),
52  "User name if needed")
53  ("password", po::value<string>()->default_value(""),
54  "Password if needed")
55  ("overwrite", po::value<string>()->default_value("no"),
56  "Allow overwriting local files if they already exist")
57  ("distant-workspace", po::value<string>(),
58  "Path to distant repository workspace")
59  ("local-workspace", po::value<string>(),
60  "Path to local repository workspace")
61  ("tries", po::value<int>()->default_value(4),
62  "Number of download tries");
63 
64  /* Get config file path */
65  path abs_path = confFilePath(filename);
66 
67  /* Read config file */
68  po::variables_map vm;
69  try {
70  po::store(po::parse_config_file<char>(abs_path.c_str(), options), vm);
71  po::notify(vm);
72  } catch (std::exception &e) {
73  throw e.what();
74  }
75 
76  /* Configure object */
77  parseHost(vm["host"].as<string>());
78  hostUrl = vm["host-url"].as<string>();
79  user = vm["user"].as<string>();
80  password = vm["password"].as<string>();
81  parseOverwritingPolicy(vm["overwrite"].as<string>());
82  distantRoot = vm["distant-workspace"].as<string>();
83  localRoot = localWorkspacePrefix() / vm["local-workspace"].as<string>();
84  tries = static_cast<size_t>(vm["tries"].as<int>());
85 
86 }
87 
88 void ConnectionConfiguration::parseHost(const string& name) {
89  const string uncased = lower(name);
90  if (uncased == "irods") {
92  } else if (uncased == "webdav") {
94  } else {
95  throw UnknownHost(name);
96  }
97 }
98 
100 
101  using std::vector;
102 
103  const vector<string> overwriteAllowedOptions = { "true", "yes", "y" };
104  const vector<string> overwriteForbiddenOptions = { "false", "no", "n" };
105 
106  string uncased = lower(policy);
107  if (valueIsListed(uncased, overwriteAllowedOptions)) {
109  } else if (valueIsListed(uncased, overwriteForbiddenOptions)) {
111  } else {
112  throw std::runtime_error("I don't know this overwriting policy: " + policy);
113  }
114 
115 }
116 
117 } // namespace DataSync
118 } // namespace ElementsServices
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::Path::Item path
Definition: DataSyncUtils.h:39
ELEMENTS_API std::string lower(std::string text)
ELEMENTS_API path confFilePath(path filename)
ELEMENTS_API bool valueIsListed(const T &value, const std::vector< T > &list)
Definition: DataSyncUtils.h:64
ELEMENTS_API path localWorkspacePrefix()
constexpr double e
The base of the natural logarithm .
Definition: MathConstants.h:50