Apache Mesos
fetch.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_FLAGS_FETCH_HPP__
14 #define __STOUT_FLAGS_FETCH_HPP__
15 
16 #include <sstream> // For istringstream.
17 #include <string>
18 
19 #include <stout/duration.hpp>
20 #include <stout/error.hpp>
21 #include <stout/json.hpp>
22 #include <stout/path.hpp>
23 #include <stout/strings.hpp>
24 #include <stout/try.hpp>
25 
26 #include <stout/flags/parse.hpp>
27 
28 #include <stout/os/read.hpp>
29 
30 namespace flags {
31 
32 // Allow the value for a flag to be fetched/resolved from a URL such
33 // as file://foo/bar/baz. Use template specialization to add a custom
34 // fetcher for a given type, such as Path from <stout/path.hpp> Path
35 // has a custom fetcher so that it returns the path given rather than
36 // the value read from that path.
37 template <typename T>
38 Try<T> fetch(const std::string& value)
39 {
40  // If the flag value corresponds to a file indicated by file://
41  // fetch and then parse the contents of that file.
42  //
43  // TODO(cmaloney): Introduce fetching for things beyond just file://
44  // such as http:// as well!
45  if (strings::startsWith(value, "file://")) {
46  const std::string path = value.substr(7);
47 
49  if (read.isError()) {
50  return Error("Error reading file '" + path + "': " + read.error());
51  }
52 
53  return parse<T>(read.get());
54  }
55 
56  return parse<T>(value);
57 }
58 
59 
60 template <>
61 inline Try<Path> fetch(const std::string& value)
62 {
63  // Explicitly skip fetching the file if this is for a Path flag!
64  return parse<Path>(value);
65 }
66 
67 
68 template <>
69 inline Try<SecurePathOrValue> fetch(const std::string& value)
70 {
71  // Delegates fetching the value to its `parse` function so the two
72  // values, the path and its contents are available when constructing
73  // and instance of `SecurePathOrValue`.
74  return parse<SecurePathOrValue>(value);
75 }
76 
77 } // namespace flags {
78 
79 #endif // __STOUT_FLAGS_FETCH_HPP__
Definition: path.hpp:29
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
Try< T > fetch(const std::string &value)
Definition: fetch.hpp:38
static Try error(const E &e)
Definition: try.hpp:43
Result< std::string > read(int_fd fd, size_t size)
Definition: read.hpp:55
bool isError() const
Definition: try.hpp:78
Result< Credentials > read(const Path &path)
Definition: credentials.hpp:35
bool startsWith(const std::string &s, const std::string &prefix)
Definition: strings.hpp:381
Definition: parse.hpp:33