55
66#include " utils.h"
77
8+ #include < algorithm> // std::any_of
89#include < cassert> // assert
910#include < fstream> // std::ofstream
1011#include < iostream> // std::cerr
1112#include < optional> // std::optional, std::nullopt
13+ #include < set> // std::set
1214#include < sstream> // std::ostringstream
1315#include < stdexcept> // std::runtime_error
1416
1517namespace {
1618
1719auto handle_json_entry (
1820 const std::filesystem::path &entry_path,
21+ const std::set<std::string> &extensions,
1922 std::vector<std::pair<std::filesystem::path, sourcemeta::jsontoolkit::JSON>>
2023 &result) -> void {
2124 if (std::filesystem::is_directory (entry_path)) {
2225 for (auto const &entry :
2326 std::filesystem::recursive_directory_iterator{entry_path}) {
2427 if (!std::filesystem::is_directory (entry) &&
25- entry.path ().extension () == " .json" ) {
28+ std::any_of (extensions.cbegin (), extensions.cend (),
29+ [&entry](const auto &extension) {
30+ return entry.path ().string ().ends_with (extension);
31+ })) {
2632 result.emplace_back (entry.path (),
2733 sourcemeta::jsontoolkit::from_file (entry.path ()));
2834 }
@@ -34,26 +40,42 @@ auto handle_json_entry(
3440 throw std::runtime_error (error.str ());
3541 }
3642
37- result.emplace_back (entry_path,
38- sourcemeta::jsontoolkit::from_file (entry_path));
43+ if (std::any_of (extensions.cbegin (), extensions.cend (),
44+ [&entry_path](const auto &extension) {
45+ return entry_path.string ().ends_with (extension);
46+ })) {
47+ result.emplace_back (entry_path,
48+ sourcemeta::jsontoolkit::from_file (entry_path));
49+ }
3950 }
4051}
4152
53+ auto normalize_extension (const std::string &extension) -> std::string {
54+ if (extension.starts_with (' .' )) {
55+ return extension;
56+ }
57+
58+ std::ostringstream result;
59+ result << ' .' << extension;
60+ return result.str ();
61+ }
62+
4263} // namespace
4364
4465namespace intelligence ::jsonschema::cli {
4566
46- auto for_each_json (const std::vector<std::string> &arguments)
67+ auto for_each_json (const std::vector<std::string> &arguments,
68+ const std::set<std::string> &extensions)
4769 -> std::vector<
4870 std::pair<std::filesystem::path, sourcemeta::jsontoolkit::JSON>> {
4971 std::vector<std::pair<std::filesystem::path, sourcemeta::jsontoolkit::JSON>>
5072 result;
5173
5274 if (arguments.empty ()) {
53- handle_json_entry (std::filesystem::current_path (), result);
75+ handle_json_entry (std::filesystem::current_path (), extensions, result);
5476 } else {
5577 for (const auto &entry : arguments) {
56- handle_json_entry (entry, result);
78+ handle_json_entry (entry, extensions, result);
5779 }
5880 }
5981
@@ -206,4 +228,29 @@ auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
206228 return null_stream;
207229}
208230
231+ auto parse_extensions (const std::map<std::string, std::vector<std::string>>
232+ &options) -> std::set<std::string> {
233+ std::set<std::string> result;
234+
235+ if (options.contains (" extension" )) {
236+ for (const auto &extension : options.at (" extension" )) {
237+ log_verbose (options) << " Using extension: " << extension << " \n " ;
238+ result.insert (normalize_extension (extension));
239+ }
240+ }
241+
242+ if (options.contains (" e" )) {
243+ for (const auto &extension : options.at (" e" )) {
244+ log_verbose (options) << " Using extension: " << extension << " \n " ;
245+ result.insert (normalize_extension (extension));
246+ }
247+ }
248+
249+ if (result.empty ()) {
250+ result.insert ({" .json" });
251+ }
252+
253+ return result;
254+ }
255+
209256} // namespace intelligence::jsonschema::cli
0 commit comments