|
9 | 9 |
|
10 | 10 | #include <filesystem> |
11 | 11 | #include <fstream> |
12 | | -#include <set> |
13 | 12 | #include <vector> |
14 | 13 |
|
15 | 14 | namespace fs = std::filesystem; |
16 | 15 |
|
17 | | -namespace |
18 | | -{ |
19 | 16 | //---------------------------------------------------------------------------- |
20 | | -/** |
21 | | - * Recover a OS-specific vector of potential config file directories |
22 | | - */ |
23 | | -std::vector<fs::path> GetConfigPaths(const std::string& configSearch) |
| 17 | +std::vector<fs::path> F3DConfigFileTools::GetConfigPaths(const std::string& configSearch) |
24 | 18 | { |
25 | 19 | std::vector<fs::path> paths; |
26 | 20 |
|
27 | | - fs::path configPath; |
28 | 21 | std::vector<fs::path> dirsToCheck = { |
29 | 22 |
|
30 | 23 | #ifdef __APPLE__ |
@@ -60,28 +53,44 @@ std::vector<fs::path> GetConfigPaths(const std::string& configSearch) |
60 | 53 | configNames.emplace_back(configSearch); |
61 | 54 | } |
62 | 55 |
|
63 | | - for (const auto& configName : configNames) |
64 | | - { |
65 | | - configPath = dir / (configName); |
66 | | - if (fs::exists(configPath)) |
67 | | - { |
68 | | - f3d::log::debug("Config file found: ", configPath.string()); |
69 | | - paths.emplace_back(configPath); |
70 | | - } |
71 | | - else |
72 | | - { |
73 | | - f3d::log::debug("Candidate config file not found: ", configPath.string()); |
74 | | - } |
75 | | - } |
| 56 | + std::transform(configNames.begin(), configNames.end(), std::back_inserter(paths), |
| 57 | + [&dir](const auto& configName) { return dir / configName; }); |
76 | 58 | } |
77 | 59 | catch (const fs::filesystem_error&) |
78 | 60 | { |
79 | | - f3d::log::error("Error recovering configuration file path: ", configPath.string()); |
| 61 | + f3d::log::error("Error recovering configuration file path"); |
80 | 62 | } |
81 | 63 | } |
82 | 64 |
|
83 | 65 | return paths; |
84 | 66 | } |
| 67 | + |
| 68 | +//---------------------------------------------------------------------------- |
| 69 | +void F3DConfigFileTools::PrintConfigInfo(const std::vector<fs::path>& configPaths) |
| 70 | +{ |
| 71 | + if (!configPaths.empty()) |
| 72 | + { |
| 73 | + f3d::log::info("Found available config path"); |
| 74 | + } |
| 75 | + |
| 76 | + for (const fs::path& path : configPaths) |
| 77 | + { |
| 78 | + std::error_code ec; |
| 79 | + const bool exists = fs::exists(path, ec); |
| 80 | + |
| 81 | + if (ec) |
| 82 | + { |
| 83 | + f3d::log::info("Error while checking config path: ", path, " : ", ec.message()); |
| 84 | + } |
| 85 | + else if (exists) |
| 86 | + { |
| 87 | + f3d::log::info("Config file found: ", path); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + f3d::log::info("Candidate config file not found: ", path); |
| 92 | + } |
| 93 | + } |
85 | 94 | } |
86 | 95 |
|
87 | 96 | //---------------------------------------------------------------------------- |
@@ -110,44 +119,58 @@ F3DConfigFileTools::ParsedConfigFiles F3DConfigFileTools::ReadConfigFiles( |
110 | 119 | std::vector<fs::path> configPaths; |
111 | 120 | if (!configSearch.empty()) |
112 | 121 | { |
113 | | - configPaths = ::GetConfigPaths(configSearch); |
| 122 | + configPaths = F3DConfigFileTools::GetConfigPaths(configSearch); |
114 | 123 | } |
115 | 124 | else |
116 | 125 | { |
117 | 126 | // Collapse full path into an absolute path |
118 | 127 | configPaths.emplace_back(f3d::utils::collapsePath(userConfig)); |
119 | 128 | } |
120 | 129 |
|
| 130 | + if (f3d::log::getVerboseLevel() == f3d::log::VerboseLevel::DEBUG) |
| 131 | + { |
| 132 | + F3DConfigFileTools::PrintConfigInfo(configPaths); |
| 133 | + } |
| 134 | + |
121 | 135 | // Recover actual individual config file paths |
122 | 136 | std::vector<fs::path> actualConfigFilePaths; |
| 137 | + bool explicitConfigMissing = false; |
123 | 138 | for (auto configPath : configPaths) |
124 | 139 | { |
125 | | - // Recover an absolute canonical path to config file |
126 | 140 | try |
127 | 141 | { |
| 142 | + if (!fs::exists(configPath)) |
| 143 | + { |
| 144 | + if (!explicitConfigMissing && !userConfig.empty() && configSearch.empty()) |
| 145 | + { |
| 146 | + f3d::log::error("Configuration file does not exist"); |
| 147 | + explicitConfigMissing = true; |
| 148 | + } |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + // Recover an absolute canonical path to config file |
128 | 153 | configPath = fs::canonical(fs::path(configPath)).string(); |
129 | | - } |
130 | | - catch (const fs::filesystem_error&) |
131 | | - { |
132 | | - f3d::log::error("Configuration file does not exist: ", configPath.string(), " , ignoring it"); |
133 | | - continue; |
134 | | - } |
135 | 154 |
|
136 | | - // Recover all config files if needed in directories |
137 | | - if (fs::is_directory(configPath)) |
138 | | - { |
139 | | - f3d::log::debug("Using config directory ", configPath.string()); |
140 | | - const size_t oldSize = actualConfigFilePaths.size(); |
141 | | - auto dirIter = fs::directory_iterator(configPath); |
142 | | - std::copy(std::filesystem::begin(dirIter), std::filesystem::end(dirIter), |
143 | | - std::back_inserter(actualConfigFilePaths)); |
144 | | - // directory_iterator is not ordered, enforce alphabetical ordering for the added files. |
145 | | - std::sort(actualConfigFilePaths.begin() + oldSize, actualConfigFilePaths.end()); |
| 155 | + // Recover all config files if needed in directories |
| 156 | + if (fs::is_directory(configPath)) |
| 157 | + { |
| 158 | + f3d::log::debug("Using config directory ", configPath.string()); |
| 159 | + const size_t oldSize = actualConfigFilePaths.size(); |
| 160 | + auto dirIter = fs::directory_iterator(configPath); |
| 161 | + std::copy(fs::begin(dirIter), fs::end(dirIter), std::back_inserter(actualConfigFilePaths)); |
| 162 | + // directory_iterator is not ordered, enforce alphabetical ordering for the added files. |
| 163 | + std::sort(actualConfigFilePaths.begin() + oldSize, actualConfigFilePaths.end()); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + f3d::log::debug("Using config file ", configPath.string()); |
| 168 | + actualConfigFilePaths.emplace_back(configPath); |
| 169 | + } |
146 | 170 | } |
147 | | - else |
| 171 | + catch (const fs::filesystem_error& e) |
148 | 172 | { |
149 | | - f3d::log::debug("Using config file ", configPath.string()); |
150 | | - actualConfigFilePaths.emplace_back(configPath); |
| 173 | + f3d::log::error("Error while locating config path: ", e.what()); |
151 | 174 | } |
152 | 175 | } |
153 | 176 |
|
@@ -337,5 +360,5 @@ F3DConfigFileTools::ParsedConfigFiles F3DConfigFileTools::ReadConfigFiles( |
337 | 360 | } |
338 | 361 | } |
339 | 362 | return F3DConfigFileTools::ParsedConfigFiles{ std::move(optionsEntries), |
340 | | - std::move(imperativeOptionsEntries), std::move(bindingsEntries) }; |
| 363 | + std::move(imperativeOptionsEntries), std::move(bindingsEntries), std::move(configPaths) }; |
341 | 364 | } |
0 commit comments